When attempting to copy an object between S3 buckets using AWS KMS for encryption, you encounter the following error:
copy failed: s3://atestbucket2022-with-kms/CPM Configurations.pdf to s3://s3sync-cross-account-destination-with-kms/CPM Configurations.pdf An error occurred (AccessDenied) when calling the CopyObject operation: User: arn:aws:sts::123456789123:assumed-role/N2WS_Role_backup/i-00cf8874bfd4c8c0f is not authorized to perform: kms:GenerateDataKey on this resource because the resource does not exist in this Region, no resource-based policies allow access, or a resource-based policy explicitly denies access
This error occurs because the user or role performing the copy operation does not have the necessary permissions to use the kms:GenerateDataKey
action on the specified KMS key. This can happen if the KMS key is not accessible from the target account or if the key is not available in the specified region.
To resolve this issue, you need to allow access from the target account to the KMS key in the source account. Follow these steps:
Modify the KMS Key Policy:
Example policy statement:
{
"Version": "2012-10-17",
"Id": "key-policy",
"Statement": [
{
"Sid": "Allow use of the key",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789123:role/N2WS_Role_backup"
},
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
],
"Resource": "*"
},
{
"Sid": "Allow cross-account access",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::target-account-id:root"
},
"Action": [
"kms:GenerateDataKey",
"kms:Decrypt"
],
"Resource": "*"
}
]
}
Add the Source Account Under Other AWS Accounts:
Verify IAM Role Permissions:
N2WS_Role_backup
has the necessary permissions to perform the kms:GenerateDataKey
action.{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kms:GenerateDataKey",
"kms:Decrypt"
],
"Resource": "arn:aws:kms:region:account-id:key/key-id"
}
]
}
Check Region Configuration:
Test the Configuration:
By following these steps, you should be able to resolve the AccessDenied
(kms:GenerateDataKey) error and successfully copy objects between S3 buckets using AWS KMS for encryption.