Skip to content

feat(s3): add grantReplicationPermission for IAM Role permissions #34138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -304,25 +304,25 @@
}
},
{
"Action": "kms:Decrypt",
"Action": [
"kms:Encrypt",
"kms:GenerateDataKey*",
"kms:ReEncrypt*"
],
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
"SourceKmsKeyFE472F1C",
"DestinationKmsKey0D94AA3C",
"Arn"
]
}
},
{
"Action": [
"kms:Encrypt",
"kms:GenerateDataKey*",
"kms:ReEncrypt*"
],
"Action": "kms:Decrypt",
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
"DestinationKmsKey0D94AA3C",
"SourceKmsKeyFE472F1C",
"Arn"
]
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,12 @@ class TestStack extends Stack {
],
});

this.replicationRole.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['s3:GetReplicationConfiguration', 's3:ListBucket'],
resources: [this.sourceBucket.bucketArn],
effect: iam.Effect.ALLOW,
}));
this.replicationRole.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['s3:GetObjectVersionForReplication', 's3:GetObjectVersionAcl', 's3:GetObjectVersionTagging'],
resources: [this.sourceBucket.arnForObjects('*')],
effect: iam.Effect.ALLOW,
}));
this.replicationRole.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['s3:ReplicateObject', 's3:ReplicateDelete', 's3:ReplicateTags', 's3:ObjectOwnerOverrideToBucketOwner'],
resources: [this.destinationBucket.arnForObjects('*')],
effect: iam.Effect.ALLOW,
}));
sourceKmsKey.grantDecrypt(this.replicationRole);
destinationKmsKey.grantEncrypt(this.replicationRole);
this.sourceBucket.grantReplicationPermission(this.replicationRole, {
sourceDecryptionKey: sourceKmsKey,
destinations: [
{ encryptionKey: destinationKmsKey, bucket: this.destinationBucket },
],
});
}
}

Expand Down
21 changes: 19 additions & 2 deletions packages/aws-cdk-lib/aws-s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -941,11 +941,14 @@ To replicate objects to a destination bucket, you can specify the `replicationRu
declare const destinationBucket1: s3.IBucket;
declare const destinationBucket2: s3.IBucket;
declare const replicationRole: iam.IRole;
declare const kmsKey: kms.IKey;
declare const encryptionKey: kms.IKey;
declare const destinationEncryptionKey: kms.IKey;

const sourceBucket = new s3.Bucket(this, 'SourceBucket', {
// Versioning must be enabled on both the source and destination bucket
versioned: true,
// Optional. Specify the KMS key to use for encrypts objects in the source bucket.
encryptionKey,
// Optional. If not specified, a new role will be created.
replicationRole,
replicationRules: [
Expand All @@ -970,7 +973,7 @@ const sourceBucket = new s3.Bucket(this, 'SourceBucket', {
// If set, metrics will be output to indicate whether replication by S3 RTC took longer than the configured time.
metrics: s3.ReplicationTimeValue.FIFTEEN_MINUTES,
// The kms key to use for the destination bucket.
kmsKey,
kmsKey: destinationEncryptionKey,
// The storage class to use for the destination bucket.
storageClass: s3.StorageClass.INFREQUENT_ACCESS,
// Whether to replicate objects with SSE-KMS encryption.
Expand All @@ -997,6 +1000,20 @@ const sourceBucket = new s3.Bucket(this, 'SourceBucket', {
},
],
});

// Grant permissions to the replication role.
// This method is not required if you choose to use an auto-generated replication role or manually grant permissions.
sourceBucket.grantReplicationPermission(replicationRole, {
// Optional. Specify the KMS key to use for decrypting objects in the source bucket.
sourceDecryptionKey: encryptionKey,
destinations: [
{ bucket: destinationBucket1 },
{ bucket: destinationBucket2, encryptionKey: destinationEncryptionKey },
],
// The 'encryptionKey' property within the 'destinations' array is optional.
// If not specified for a destination bucket, this method assumes that
// given destination bucket is not encrypted.
});
```

### Cross Account Replication
Expand Down
Loading
Loading