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

Merged
merged 25 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1c53bb0
feat: add grant method for the role is used replication
hassaku63 Apr 13, 2025
32bb56c
test: grant replication permission for custom iam role
hassaku63 Apr 13, 2025
ce97233
test: replace grant procedure for custom iam role
hassaku63 Apr 13, 2025
86fe648
docs: how to use grantReplicationPermission
hassaku63 Apr 13, 2025
901cbbc
fix: add JSDocs for prop
hassaku63 Apr 13, 2025
4f0ae47
fix: linter and remove old comments
hassaku63 Apr 13, 2025
6e1d9dc
docs: fix example variable name to reflect right intent
hassaku63 Apr 14, 2025
2e65d3d
docs: describe clearly when the grant replication permission method s…
hassaku63 Apr 14, 2025
231ff71
fix: JSII3008
hassaku63 Apr 14, 2025
57721e6
fix: remove debug statement
hassaku63 Apr 15, 2025
a3c715a
fix: add jsdoc
hassaku63 Apr 15, 2025
40b6e2b
test: add test case for invalid args
hassaku63 Apr 16, 2025
9aa8929
Merge branch 'main' into feat/grant-permission-for-replication-role
hassaku63 Apr 27, 2025
2837964
refactor: use private grant method
hassaku63 Apr 27, 2025
b970f0a
refactor: return iam.Grant
hassaku63 Apr 27, 2025
ad18d1d
fix: fit interface of grantReplicationPermission method
hassaku63 Apr 27, 2025
d2c434e
fix: lint error
hassaku63 Apr 27, 2025
3fedd55
Merge branch 'main' into feat/grant-permission-for-replication-role
hassaku63 May 3, 2025
bdbfd76
Merge branch 'main' into feat/grant-permission-for-replication-role
hassaku63 May 24, 2025
419d893
Update packages/aws-cdk-lib/aws-s3/lib/bucket.ts
hassaku63 May 24, 2025
a23abfa
test: improve error message
hassaku63 May 24, 2025
4c7c9d7
docs: improve JSDoc for clearly
hassaku63 May 24, 2025
83aa959
test: remove overriding logical id
hassaku63 May 24, 2025
30990b4
test: remove overriding logical id
hassaku63 May 24, 2025
a0df50e
Merge branch 'main' into feat/grant-permission-for-replication-role
shikha372 May 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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