Skip to content

Instantly share code, notes, and snippets.

@jewelsjacobs
Last active September 3, 2019 22:14
Show Gist options
  • Select an option

  • Save jewelsjacobs/69dcad5e398075f60628322a591b105d to your computer and use it in GitHub Desktop.

Select an option

Save jewelsjacobs/69dcad5e398075f60628322a591b105d to your computer and use it in GitHub Desktop.
CDK EnableTerminationProtection AWSCustomResource Example
import cdk = require('@aws-cdk/core');
import { Bucket } from '@aws-cdk/aws-s3';
import { AwsCustomResource } from '@aws-cdk/custom-resources';
export class TerminationProtectionStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Enable stack termination protection
// using the AWS SDK CloudFormation updateTerminationProtection API:
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFormation.html#updateTerminationProtection-property
const enableTerminationProtection = new AwsCustomResource(this, 'UpdateTerminationProtection', {
onUpdate: { // will also be called for a CREATE event
service: 'CloudFormation',
action: 'updateTerminationProtection',
parameters: {
EnableTerminationProtection: true,
StackName: this.stackName
},
physicalResourceId: Date.now().toString() // Update physical id to always fetch the latest version
}
});
const StackId = `${enableTerminationProtection.getData("StackId")}` as string;
if (StackId) console.error(StackId); // console.error is what is used to print to stdout during the synth stage
new Bucket(this, 'MyBucket');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment