Clean up your stack #
When destroying a stack, resources may be deleted, retained, or snapshotted according to their deletion policy.
By default, most resources will get deleted upon stack deletion, however that’s not the case for all resources.
The DynamoDB table will be retained by default. If you don’t want to retain this table, we can set this in CDK
code by using RemovalPolicy
:
Set the DynamoDB table to be deleted upon stack deletion #
Edit src/CdkWorkshop/HitCounter.java
and add the removalPolicy
prop to the table
package com.myorg;
import java.util.HashMap;
import java.util.Map;
import software.constructs.Construct;
import software.amazon.awscdk.RemovalPolicy;
import software.amazon.awscdk.services.dynamodb.Attribute;
import software.amazon.awscdk.services.dynamodb.AttributeType;
import software.amazon.awscdk.services.dynamodb.Table;
import software.amazon.awscdk.services.lambda.Code;
import software.amazon.awscdk.services.lambda.Function;
import software.amazon.awscdk.services.lambda.Runtime;
public class HitCounter extends Construct {
private final Function handler;
private final Table table;
public HitCounter(final Construct scope, final String id, final HitCounterProps props) {
super(scope, id);
this.table = Table.Builder.create(this, "Hits")
.partitionKey(Attribute.builder()
.name("path")
.type(AttributeType.STRING)
.build())
.removalPolicy(RemovalPolicy.DESTROY)
.build();
final Map<String, String> environment = new HashMap<>();
environment.put("DOWNSTREAM_FUNCTION_NAME", props.getDownstream().getFunctionName());
environment.put("HITS_TABLE_NAME", this.table.getTableName());
this.handler = Function.Builder.create(this, "HitCounterHandler")
.runtime(Runtime.NODEJS_14_X)
.handler("hitcounter.handler")
.code(Code.fromAsset("lambda"))
.environment(environment)
.build();
// Grants the lambda function read/write permissions to our table
this.table.grantReadWriteData(this.handler);
// Grants the lambda function invoke permissions to the downstream function
props.getDownstream().grantInvoke(this.handler);
}
/**
* @return the counter definition
*/
public Function getHandler() {
return this.handler;
}
/**
* @return the counter table
*/
public Table getTable() {
return this.table;
}
}
Since we made a change in the construct file, we need to redeploy the stack to put the changes into effect. Use cdk deploy
:
cdk deploy
Additionally, the Lambda function created will generate CloudWatch logs that are permanently retained. These will not be tracked by CloudFormation since they are not part of the stack, so the logs will still persist. You will have to manually delete these in the console if desired.
Now that we know which resources will be deleted, we can proceed with deleting the
stack. You can either delete the stack through the AWS CloudFormation console or use
cdk destroy
:
cdk destroy
You’ll be asked:
Are you sure you want to delete: CdkWorkshopStack (y/n)?
Hit “y” and you’ll see your stack being destroyed.
The bootstrapping stack created through cdk bootstrap
still exists. If you plan
on using the CDK in the future (we hope you do!) do not delete this stack.
If you would like to delete this stack, it will have to be done through the CloudFormation
console. Head over to the CloudFormation console and delete the CDKToolkit
stack. The S3
bucket created will be retained by default, so if you want to avoid any unexpected charges,
be sure to head to the S3 console and empty + delete the bucket generated from bootstrapping.