ちょっと話題の記事

皆さん待望の削除保護機能が Amazon DynamoDB でサポートされました

ようやく Amazon DynamoDB でも削除保護が実装されました

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

ウィスキー、シガー、パイプをこよなく愛する大栗です。

Amazon DynamoDB のオペレーションミスを抑止するためにテーブルの削除保護機能がサポートされましたのでご紹介します。

Amazon DynamoDB now supports table deletion protection

Amazon DynamoDB の削除保護

EC2 や RDS には削除保護機能が合ったのですが、今まで DynamoDB にはありませんでした。そのためオペレーションミスなどで DynamoDB のテーブルを削除してしまう事故などが発生しがちでした。

DynamoDB の削除保護機能は、他のサービスと同様に IAM などの権限を持っていても機能が有効になっている限り削除を行えず、機能を無効にして初めて削除できるものです。

なお、ドキュメント上は AWS CloudFormation でも削除保護がサポートされているとの記述がありますが、2023年3月9日7時37分 JST 現在では CloudFormation のドキュメントに記述は無いようです。

やってみる

Console でやってみる

まずはコンソールの操作からです。普通にテーブルを作成しますが、テーブル設定(Table settiongs)で設定をカスタマイズ(Customize settings)を選択します。

Table settiongs

削除保護(Deletion protection)の欄が増えているので、削除保護をオンにする(Turn on deletion protection)をチェックしてテーブルを作成します。

既存のテーブルを変更する場合には、テーブルを選択してアクションから削除保護を有効にする(Turn on deletion protection)を選択します。

確認画面がでてくるので確認(Confirm)をクリックします。

削除保護が有効な状態で削除を行おうとすると、以下のようなプロンプトが出て削除できません。

削除保護を無効にすると削除が可能になります。

CLI でやってみる

次に CLI です。現時点では AWS CLI は v1 系のバージョン 1.27.87 以上のものをご使用ください。

CLI で削除保護を有効にする時は--deletion-protection-enabledオプション、無効にする時には--no-deletion-protection-enabledオプションを設定します。

テーブル作成時は以下のようになります。

$ aws dynamodb create-table --region ap-northeast-1 \
    --deletion-protection-enabled \
    --table-name test2 \
    --attribute-definitions \
        AttributeName=partition,AttributeType=S \
        AttributeName=sort,AttributeType=S \
    --key-schema \
        AttributeName=partition,KeyType=HASH \
        AttributeName=sort,KeyType=RANGE \
    --provisioned-throughput \
        ReadCapacityUnits=5,WriteCapacityUnits=5 \
    --table-class STANDARD
{
    "TableDescription": {
        "AttributeDefinitions": [
            {
                "AttributeName": "partition",
                "AttributeType": "S"
            },
            {
                "AttributeName": "sort",
                "AttributeType": "S"
            }
        ],
        "TableName": "test2",
        "KeySchema": [
            {
                "AttributeName": "partition",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "sort",
                "KeyType": "RANGE"
            }
        ],
        "TableStatus": "CREATING",
        "CreationDateTime": 1678321299.489,
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:ap-northeast-1:123456789012:table/test2",
        "TableId": "12345678-1234-1234-1234-123456789012",
        "TableClassSummary": {
            "TableClass": "STANDARD"
        },
        "DeletionProtectionEnabled": true
    }
}

既存テーブルの変更では

$ aws dynamodb update-table --table-name test3 --region ap-northeast-1 \
    --deletion-protection-enabled
{
    "TableDescription": {
        "AttributeDefinitions": [
            {
                "AttributeName": "partition",
                "AttributeType": "S"
            },
            {
                "AttributeName": "sort",
                "AttributeType": "S"
            }
        ],
        "TableName": "test3",
        "KeySchema": [
            {
                "AttributeName": "partition",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "sort",
                "KeyType": "RANGE"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": 1678321648.456,
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:ap-northeast-1:123456789012:table/test3",
        "TableId": "12345678-1234-1234-1234-123456789012",
        "TableClassSummary": {
            "TableClass": "STANDARD"
        },
        "DeletionProtectionEnabled": true
    }
}

この状態で削除しようとするとエラーになります。

$ aws dynamodb delete-table --table-name test2 --region ap-northeast-1

An error occurred (ValidationException) when calling the DeleteTable operation: Resource cannot be deleted as it is currently protected against deletion. Disable deletion protection first.

削除保護を無効にしてから実行すると正常に削除できます。

$ aws dynamodb update-table --table-name test2 --region ap-northeast-1 \
    --no-deletion-protection-enabled

An error occurred (ValidationException) when calling the DeleteTable operation: Resource cannot be deleted as it is currently protected against deletion. Disable deletion protection first.
{
    "TableDescription": {
        "AttributeDefinitions": [
            {
                "AttributeName": "partition",
                "AttributeType": "S"
            },
            {
                "AttributeName": "sort",
                "AttributeType": "S"
            }
        ],
        "TableName": "test3",
        "KeySchema": [
            {
                "AttributeName": "partition",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "sort",
                "KeyType": "RANGE"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": 1678321648.456,
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:ap-northeast-1:123456789012:table/test2",
        "TableId": "12345678-1234-1234-1234-123456789012",
        "TableClassSummary": {
            "TableClass": "STANDARD"
        },
        "DeletionProtectionEnabled": false
    }
}

削除保護を無効にしたので、もう一度削除してみます。

$ aws dynamodb delete-table --table-name test2 --region ap-northeast-1
{
    "TableDescription": {
        "TableName": "test2",
        "TableStatus": "DELETING",
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:ap-northeast-1:123456789012:table/test2",
        "TableId": "12345678-1234-1234-1234-123456789012",
        "TableClassSummary": {
            "TableClass": "STANDARD"
        },
        "DeletionProtectionEnabled": false
    }
}

さいごに

EC2 の termination protection が2011年2月にリリースされ、RDS の deletion protection が 2018年9月にリリースされました。また、DynamoDB より後にリリースされた Neptune の deletion protection が2020年1月にリリースさました。何故かずっと削除保護が実装されなかった DynamoDB ですが、遅れること数年ようやく実装されました。基本的にはデータベースサービスには全て実装して欲しいので新しいデータベースサービスでは最初から実装されてほしいです。