Aws-cdk: How to specify DeletionPolicy to Delete?

Created on 30 Jan 2019  路  4Comments  路  Source: aws/aws-cdk

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')
@aws-cdcore bug

Most helpful comment

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);

All 4 comments

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.

Solution for setting the DeletionPolicy

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.

Things that did not work

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.

Was this page helpful?
0 / 5 - 0 ratings