概要
2023/3/8にDynamoDBテーブルの削除保護の機能が追加になったとAWSブログでアナウンスがありました。
DynamoDBデベロッパーガイドにも掲載されており、
CloudFormationによる削除保護設定もサポートしていると記載されていますが、
2023/3/9時点ではCloudFormationリファレンスには掲載されていませんでしたが、
APIリファレンスには掲載されていました。
今回はCloudFormationでDynamoDBテーブルの削除保護を有効化する設定を検証しました。
CloudFormationで設定してみる
1. DynamoDBのテンプレートを作成(テスト用なのでテーブルの中身は気にしないでください。)
Description: DynamoDB Table
Parameters:
Project:
Type: String
Landscape:
Type: String
Resources:
TestTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
TableName: !Sub ${Project}-${Landscape}-table
AttributeDefinitions:
- AttributeName: client_id
AttributeType: S
- AttributeName: time
AttributeType: N
KeySchema:
- AttributeName: client_id
KeyType: HASH
- AttributeName: time
KeyType: RANGE
Tags:
- Key: env
Value: !Sub ${Landscape}
- Key: name
Value: !Sub ${Project}
BillingMode: PAY_PER_REQUEST
2. CloudFormationのスタックにテンプレートを登録して、DynamoDBテーブルを作成
3. 削除保護(Deletion protection)はoffになっています。

4. CloudFormationのテンプレートのProperties配下にDeletionProtectionEnabled: trueを追記
Description: DynamoDB Table
Parameters:
Project:
Type: String
Landscape:
Type: String
Resources:
TestTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
TableName: !Sub ${Project}-${Landscape}-table
AttributeDefinitions:
- AttributeName: client_id
AttributeType: S
- AttributeName: time
AttributeType: N
KeySchema:
- AttributeName: client_id
KeyType: HASH
- AttributeName: time
KeyType: RANGE
Tags:
- Key: env
Value: !Sub ${Landscape}
- Key: name
Value: !Sub ${Project}
BillingMode: PAY_PER_REQUEST
DeletionProtectionEnabled: true
5. CloudFormationのスタックでチェンジセットを作成




6. チェンジセットのステータスがCREATE_COMPLETEになったことを確認し、Execute Change Setをクリック

7. 更新に失敗した時にロールバックするかどうかを聞かれるので、選択してExecute Change Setをクリック

8. スタックがUPDATE_COMPLETEになったことを確認

9. DynamoDBテーブルの削除保護(DeletionProtection)がOnになったことを確認

以上