Why it is important to use RemovalPolicy in CDK/CloudFormation.
Tags: aws, cloud, community
Published: 2022-02-13 11:08:16


## Table of Contents
1. [Introduction](#intro)
2. [RemovalPolicy Problem](#problem)
3. [RemovalPolicy Solution](#solution)
4. [Summary](#summary)
5. [Reference](#reference)

<a name="intro"></a>
### 1. Introduction

I attend [Coffee & Cloud: The Local Developer Experience](https://aws-startuploft-emea.com/events) organised by Mirabela Dan & Jason Forte on Wednesdays every fortnight.

During one of the sessions, Jason showcased [Deploy a Serverless API in 30 mins](https://github.com/fortejas/example-serverless-python-api).

While I was practising deploying Jason's code, I  noticed that the DynamoDB was not being deleted when I ran `cdk destroy`

This blog is about the importance of [RemovalPolicy](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicy.html) with CDK.
 

<a name="problem"></a>
### 2. RemovalPolicy Problem


I deployed the app using `cdk deploy` and then cleaned up by using `csk destroy`
 
Upon raising this in [repost:](https://repost.aws/questions/QUh-EPJIs7TauIEKDply9YIw/cdk-destroy-is-not-deleting-dynamo-db-table-created-by-cdk)

:point_right: AWS advised that the table defintion was missing `RemovalPolicy`. 
By default, CDK will (generally) apply a [RemovalPolicy](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicy.html) value of RETAIN to stateful resources which can contain data in order to avoid deleting it when destroying the stack.

We can see this in the synthesised template. For example, with this resource declared in CDK:

```typescript
const table = new Table(this, 'Table', {
    partitionKey: { type: AttributeType.STRING, name: 'id' },
})
```

The synthesised template is under `/home/ec2-user/environment/example-serverless-python-api/cdk.out/ExampleServerlessPythonApiStack.template.json` and will contain the following:

```json
"Resources": {
    "TableCD117FA1": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        ...
      },
      "UpdateReplacePolicy": "Retain",
      "DeletionPolicy": "Retain",
      "Metadata": {
        "aws:cdk:path": "ExampleServerlessPythonApiStack/Table/Resource"
      }
    },
 ...   
```
_Note: I am using Cloud 9_

The CloudFormation [UpdateReplacePolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatereplacepolicy.html) and [DeletionPolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html) attributes being Retain will cause CloudFormation to leave the resource intact when deleting the stack.

![DynamoDB not deleted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2b6h4szccrbqmks7o9f.png)

<a name="solution"></a>
### 3. RemovalPolicy Solution

Let's update `/example-serverless-python-api/lib/example-serverless-python-api-stack.ts` with `removalPolicy: RemovalPolicy.DESTROY`

```typescript
const table = new Table(this, 'Table', {
      partitionKey: { type: AttributeType.STRING, name: 'id' },
      removalPolicy: RemovalPolicy.DESTROY,
    })
```

:point_right: To have the resource deleted when we run `cdk destroy`, we need to specify a RemovalPolicy of `DESTROY` or `SNAPSHOT` in the code.

The synthesised template under `/home/ec2-user/environment/example-serverless-python-api/cdk.out/ExampleServerlessPythonApiStack.template.json` will contain the following:

```json
"Resources": {
    "TableCD117FA1": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        ...
      },
      "UpdateReplacePolicy": "Delete",
      "DeletionPolicy": "Delete",
      "Metadata": {
        "aws:cdk:path": "ExampleServerlessPythonApiStack/Table/Resource"
      }
    },
 ...   
```

![DynamoDB deleted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7pl11ow04j8otpgnif6.png)

<a name="summary"></a>
### 4. Summary

:point_right: Remember to add [RemovalPolicy](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicy.html) when using CDK for databases to avoid unexpected costs.

```typescript
removalPolicy: RemovalPolicy.DESTROY,
```

<a name="reference"></a>
### 5. Reference

[CDK destroy is not deleting DynamoDB table created by CDK](https://repost.aws/questions/QUh-EPJIs7TauIEKDply9YIw/cdk-destroy-is-not-deleting-dynamo-db-table-created-by-cdk)