aws-s3: RemovalPolicy.RETAIN not functioning properly in CDK v2 #26276
Labels
@aws-cdk/aws-s3
Related to Amazon S3
bug
This issue is a bug.
effort/medium
Medium work item – several days of effort
p2
response-requested
Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Describe the bug
I am using CDK v2 with Typescript to create a S3 bucket. I want the bucket to be orphaned from the stack and retain it even if the stack gets deleted. For that, I am trying to use one of the property of BucketProps that is RemovalPolicy. By default its value is DESTROY and if I override that to RETAIN, it doesn't execute this change and still I'm getting DESTROY as its value.
It is working fine in CDK v1, but after migrating to v2, the RemovalPolicy property is not accepting any override values.
Expected Behavior
In CDK v1 --
this.myBucket = new Bucket(this, "my-bucket-name", {
bucketName: "my-bucket-name",
blockPublicAccess: {
blockPublicAcls: true,
blockPublicPolicy: true,
ignorePublicAcls: true,
restrictPublicBuckets: true
},
versioned: false,
removalPolicy: RemovalPolicy.RETAIN
});
After building the stack, its corresponding JSON template gives the output --
"BucketName": "my-bucket-name",
"PublicAccessBlockConfiguration": {
"BlockPublicAcls": true,
"BlockPublicPolicy": true,
"IgnorePublicAcls": true,
"RestrictPublicBuckets": true
}
},
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain",
It overrides the default value of DESTROY and sets the DeletionPolicy as "Retain" correctly in its JSON. But this doesn't happen in case of CDK v2.
Current Behavior
In CDK v2 --
this.myBucket = new Bucket(this, "my-bucket-name", {
bucketName: "my-bucket-name",
blockPublicAccess: {
blockPublicAcls: true,
blockPublicPolicy: true,
ignorePublicAcls: true,
restrictPublicBuckets: true
},
versioned: false,
removalPolicy: RemovalPolicy.RETAIN
});
After building the stack, its corresponding JSON template gives the output --
"BucketName": "my-bucket-name",
"PublicAccessBlockConfiguration": {
"BlockPublicAcls": true,
"BlockPublicPolicy": true,
"IgnorePublicAcls": true,
"RestrictPublicBuckets": true
}
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
Even after setting the RemovalPolicy property to RETAIN, its corresponding JSON template has the value of "Delete".
Reproduction Steps
Code snippet --
this.myBucket = new Bucket(this, "my-bucket-name", {
bucketName: "my-bucket-name",
blockPublicAccess: {
blockPublicAcls: true,
blockPublicPolicy: true,
ignorePublicAcls: true,
restrictPublicBuckets: true
},
versioned: false,
removalPolicy: RemovalPolicy.RETAIN
});
After building the stack, check for its JSON file to see what are the values of each property.
Possible Solution
Allowing override to the default value that has been set for the RemovalPolicy property of BucketProps
Additional Information/Context
CDK v2 documentation has mentioned the values for RemovalPolicy- RETAIN, DESTROY etc.
(https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3.Bucket.html)
(https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicy.html)
CDK CLI Version
2.55.1
Framework Version
No response
Node.js Version
18.16.0
OS
macOS/Linux
Language
Typescript
Language Version
TypeScript (4.9.5)
Other information
No response
The text was updated successfully, but these errors were encountered: