CDK destroy is not deleting DynamoDB table created by CDK

0

Hi All,

I deployed a serverless-api https://github.com/fortejas/example-serverless-python-api on a cloud9 environment using CDK. When I run cdk destroy, I noticed that the DynamoDB table are not being deleted. I was wondering if this could be a CDK defect? or advise some tips on how to resolve this?

Thank you

profile picture
Sri
질문됨 2년 전3325회 조회
1개 답변
4
수락된 답변

By default, CDK will (generally) apply a RemovalPolicy value of RETAIN to stateful resources which could contain data in order to avoid deleting it when destroying the stack.

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

const table = new ddb.Table(this, "testTable", {
	partitionKey: { type: AttributeType.STRING, name: 'id' },
});

the synthesised template will contain the following:

    "testTableFD9E8557": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        [snip]
      },
      "UpdateReplacePolicy": "Retain",
      "DeletionPolicy": "Retain",
      [snip]
    },

The CloudFormation UpdateReplacePolicy and DeletionPolicy attributes being Retain will cause CloudFormation to leave the resource intact when deleting the stack.

To have the resource deleted when the stack is deleted (when you run cdk destroy), specify a RemovalPolicy of DESTROY or SNAPSHOT in your code:

const table = new ddb.Table(this, "testTable", {
	partitionKey: { type: AttributeType.STRING, name: 'id' },
	removalPolicy: RemovalPolicy.DESTROY,
});

giving this CFN template:

    "testTableFD9E8557": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        [snip]
      },
      "UpdateReplacePolicy": "Delete",
      "DeletionPolicy": "Delete",
      [snip]
    },
profile pictureAWS
전문가
James_S
답변함 2년 전
profile pictureAWS
전문가
Toni_S
검토됨 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠