aws / aws/aws-cdk

BucketDeployment: retainOnDelete: false not working when stack deleted

Open
#33,397 3 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-s3 bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

With this example stack I would expect the `BucketDeployment` to delete contents of the associated bucket when the stack is deleted because `retainOnDelete` is set to false, allowing the bucket itself to be deleted. However the contents of the bucket are not deleted, and so the stack fails to delete because the bucket can't be deleted:

```
class Example extends Stack {
constructor(scope: Construct, id: string, props: StackProps) {
super(scope, id, props);

const bucket = new Bucket(this, 'SiteBucket', {
removalPolicy: RemovalPolicy.DESTROY
});

new BucketDeployment(this, 'Deploy', {
sources: [Source.asset('../site')],
destinationBucket: bucket,
retainOnDelete: false,
logGroup: new LogGroup(this, 'bucketDeploymentLogs', {retention: RetentionDays.ONE_WEEK}),
});
}
}
```

### Regression Issue

- [ ] Select this option if this issue appears to be a regression.

### Last Known Working CDK Version

_No response_

### Expected Behavior

I expected the contents of the bucket to be deleted to allow the stack to be deleted successfully

### Current Behavior

The contents of the bucket are not deleted. In the logs for the underlying Custom Resource Lambda function I see this:

```
[INFO] 2025-02-11T18:18:02.567Z f730c674-c871-4396-ace6-6eba722d8b7a {'RequestType': 'Delete', 'ServiceToken': 'arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:coffee-store-web-CustomCDKBucketDeployment8693BB64-Xoo2NONG8SqU', 'StackId': 'arn:aws:cloudformation:us-east-1: XXXXXXXXXXXX:stack/coffee-store-web/fbed4940-e8a3-11ef-b96b-123141793963', 'RequestId': '61374908-00e5-4cdd-a5ff-d5a3919514c4', 'LogicalResourceId': 'DeployCustomResource218AF6A4', 'PhysicalResourceId': 'aws.cdk.s3deployment.e0df1d06-479a-4083-80a1-589ca71e6f6f', 'ResourceType': 'Custom::CDKBucketDeployment', 'ResourceProperties': {'ServiceToken': 'arn:aws:lambda:us-east-1: XXXXXXXXXXXX:function:coffee-store-web-CustomCDKBucketDeployment8693BB64-Xoo2NONG8SqU', 'RetainOnDelete': 'false', 'Prune': 'true', 'OutputObjectKeys': 'true', 'SourceBucketNames': ['cdk-hnb659fds-assets-XXXXXXXXXXXX-us-east-1'], 'DestinationBucketName': 'coffee-store-web-sitebucket397a1860-s6537k3vma3v', 'SourceObjectKeys': ['d3967fb79345a78bec13705c7c17d25b9e319ac215814492f4c64a15db5601dd.zip']}}

[INFO] 2025-02-11T18:18:02.567Z f730c674-c871-4396-ace6-6eba722d8b7a | s3_dest: s3%3A//coffee-store-web-sitebucket397a1860-s6537k3vma3v/

[INFO] 2025-02-11T18:18:02.567Z f730c674-c871-4396-ace6-6eba722d8b7a | old_s3_dest: None

[INFO] 2025-02-11T18:18:02.839Z f730c674-c871-4396-ace6-6eba722d8b7a | response body:
{
"Status": "SUCCESS",
"Reason": "See the details in CloudWatch Log Stream: 2025/02/11/coffee-store-web-CustomCDKBucketDeployment8693BB64-Xoo2NONG8SqU[$LATEST]3c51c64ca11e4b51bb85bb0f26eb3708",
"PhysicalResourceId": "aws.cdk.s3deployment.e0df1d06-479a-4083-80a1-589ca71e6f6f",
"StackId": "arn:aws:cloudformation:us-east-1: XXXXXXXXXXXX:stack/coffee-store-web/fbed4940-e8a3-11ef-b96b-123141793963",
"RequestId": "61374908-00e5-4cdd-a5ff-d5a3919514c4",
"LogicalResourceId": "DeployCustomResource218AF6A4",
"NoEcho": false,
"Data": {
"DestinationBucketArn": null,
"SourceObjectKeys": [
"d3967fb79345a78bec13705c7c17d25b9e319ac215814492f4c64a15db5601dd.zip"
]
}
}

```

### Reproduction Steps

See example in summary

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.178.1 (build ae342cb)

### Framework Version

_No response_

### Node.js Version

22

### OS

Mac OS 15

### Language

TypeScript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the BucketDeployment configuration and the underlying Custom::CDKBucketDeployment Lambda delete path described in the CloudWatch logs. Reproduce the example stack with retainOnDelete set to false, inspect the delete event handling, and verify that deployment contents are removed so the bucket and stack can be deleted successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.