CDK destroy不会删除通过CDK创建的 DynamoDB 表

0

【以下的问题经过翻译处理】 大家好,

我在Cloud9环境中使用CDK部署了一个无服务器API https://github.com/fortejas/example-serverless-python-api。当我运行 cdk destroy 时,我注意到 DynamoDB 表没有被删除。

请问这是CDK设计如此吗? 是否有办法删除通过CDK创建的DynamoDB表?

谢谢

profile picture
EXPERTE
gefragt vor 8 Monaten77 Aufrufe
1 Antwort
0

【以下的回答经过翻译处理】 默认情况下,CDK通常为有状态的资源(比如DynamoDB的表)的RemovalPolicy 设置为RETAIN, 来避免在销毁堆栈时将其删除。

您可以在CDK合成模板中看到这一点。例如,使用 CDK 中声明的资源:

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

合成的CloudFormation模板将包含以下内容:

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

CloudFormation UpdateReplacePolicy 和 [DeletionPolicy](https://docs.aws.amazon.com/AWSCloudFormation /latest/UserGuide/aws-attribute-deletionpolicy.html) 为“保留”的属性将导致 CloudFormation 在删除堆栈时保持资源完好无损。

要在删除堆栈时(运行 cdk destroy 时)删除资源,请在代码中将RemovalPolicy的值设置为 DESTROYSNAPSHOT

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

以下是以上代码合成的CloudFormation模板, 请参考.

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

profile picture
EXPERTE
beantwortet vor 8 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen