I don't see any non-hacky* way to define DeletionPolicy with a Delete as a value. In cdk I noticed there is a cdk.applyRemovalPolicy function which does have cdk.RemovalPolicy.Destroy, which on first glance I thought it attaches that removal policy but no, it does not.
Stack definition:
import s3 = require('@aws-cdk/aws-s3')
import cdk = require('@aws-cdk/cdk')
export class SomeStack extends cdk.Stack {
protected PrefixName: cdk.Parameter
protected BucketName: cdk.Parameter
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props)
this.addResources()
}
public addResources() {
const bucket = new s3.CfnBucket(this, 'ArtifactBucket', {
bucketName: 'hello-world',
})
cdk.applyRemovalPolicy(bucket, cdk.RemovalPolicy.Destroy)
}
}
Synthesized:
Resources:
ArtifactBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: hello-world
Metadata:
aws:cdk:path: some-stack/ArtifactBucket
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Modules: aws-cdk=0.22.0...
Expected:
Resources:
ArtifactBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: "hello-world"
DeletionPolicy: Delete
// * workaround:
bucket.addOverride('DeletionPolicy', 'Delete')
As described here, you should be able to use options on CfnBucket:
bucket.options.deletionPolicy = cdk.DeletionPolicy.Delete;
@Doug-AWS probably worth adding a HOW TO about this.
The code above is outdated. Nowadays, you must do:
import cdk = require('@aws-cdk/core');
const resource = bucket.node.findChild('Resource') as cdk.CfnResource;
resource.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY);
Just a friendly note, that this issue is about CfnBucket. For Bucket, this feature is supported.
In Python, after some tinkering:
handler = aws_lambda.Function(self, ..) # whatever, your CFN resource
handler.node.find_child('Resource').cfn_options.deletion_policy = core.CfnDeletionPolicy.RETAIN # this sets the deletion policy
handler.node.find_child('Resource').cfn_options.update_replace_policy = core.CfnDeletionPolicy.RETAIN # This setting is to stop CFN from complaining because if deletion policy is set, then update replace policy should be set too..
Note: I tried this out with an AWS Lambda resource and not an S3 Bucket.
handler.node.find_child('Resource').apply_removal_policy(core.CfnDeletionPolicy.RETAIN)
This threw an error "Invalid removal policy: Retain"
I am not sure that the Removal and Deletion policy are the same for any/all CFN resources.
Leaving this comment also for the benefit of people coming via search engines.
Most helpful comment
The code above is outdated. Nowadays, you must do: