Skip to content

Commit b6a92f3

Browse files
committed
feat(aws-s3-deployment) readme update
1 parent db0d1d0 commit b6a92f3

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

packages/aws-cdk-lib/aws-s3-deployment/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,35 @@ in which case, we recommend to remove `retainOnDelete: false`, and instead, conf
184184
[`autoDeleteObjects`](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-s3-readme.html#bucket-deletion)
185185
property on the destination bucket. This will avoid the logical ID problem mentioned above.
186186

187+
## Retain on Delete
188+
189+
By default, the contents of the destination bucket will **not** be deleted when the
190+
`BucketDeployment` resource is removed from the stack or when the destination is
191+
changed. You can use the option `retainOnDelete: false` to disable this behavior,
192+
in which case the contents will be deleted.
193+
194+
## IAM Permissions and Security
195+
196+
The `BucketDeployment` construct automatically configures IAM permissions following the principle of least privilege. When you specify a `destinationKeyPrefix`, the Lambda function's execution role will only receive permissions to access objects under that specific prefix, rather than the entire bucket.
197+
198+
```ts
199+
declare const destinationBucket: s3.Bucket;
200+
201+
// The Lambda will only have access to objects under 'app/static/*'
202+
new s3deploy.BucketDeployment(this, 'DeployWebsite', {
203+
sources: [s3deploy.Source.asset('./website-dist')],
204+
destinationBucket,
205+
destinationKeyPrefix: 'app/static',
206+
});
207+
```
208+
209+
This scoped permission approach provides several security benefits:
210+
Limits the blast radius if deployment credentials are compromised
211+
Enables multiple independent deployments to the same bucket with isolation
212+
Prevents accidental modification of objects outside the intended prefix
213+
214+
If no `destinationKeyPrefix` is specified, the Lambda will have access to all objects in the bucket (/*), maintaining backward compatibility with existing deployments.
215+
187216
## Prune
188217

189218
By default, files in the destination bucket that don't exist in the source will be deleted

packages/aws-cdk-lib/aws-s3-deployment/lib/bucket-deployment.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,15 @@ export class BucketDeployment extends Construct {
656656
*/
657657
normalizePrefix(prefix?: string): string {
658658
if (!prefix) return '';
659-
return prefix.replace(/^\/+/, '').replace(/\/+$/, '') + '/';
659+
let normalized = prefix;
660+
while (normalized.startsWith('/')) {
661+
normalized = normalized.slice(1);
662+
}
663+
while (normalized.endsWith('/')) {
664+
normalized = normalized.slice(0, -1);
665+
}
666+
667+
return normalized ? normalized + '/' : '';
660668
}
661669
}
662670

0 commit comments

Comments
 (0)