CloudFormationが削除失敗スタックの強制削除(FORCE_DELETE_STACK)をサポートしました

CloudFormationが削除失敗スタックの強制削除(FORCE_DELETE_STACK)をサポートしました

CloudFormationスタックの撤去時、依存関係などで削除に失敗したスタックの強制削除が簡単になりました。
Clock Icon2024.05.24 16:32

2024年5月23日、AWS CloudFormation のアップデートで、 依存関係などにより削除に失敗した CloudFormationスタックの強制削除が可能になりました。

今回のアップデートでサポートされた強制削除、 AWSコンソール(GUI)と、 AWSCLI(CloudShell)で試す機会がありましたので、紹介させていただきます。

事前準備

S3バケットを作成する CloudFormation スタックを作成しました。

cat << EoF > s3.yaml
Description: S3 test
Resources:
  S3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub '\${AWS::StackName}-\${AWS::Region}-\${AWS::AccountId}'
EoF

aws cloudformation deploy --template-file s3.yaml --stack-name s3test

作成されたS3バケットにファイルを転送、 CloudFormationテンプレートの削除が失敗する環境を用意しました。

aws cloudformation describe-stacks --stack-name s3test | jq -c '.Stacks[0]|[.StackName, .StackStatus] '
["s3test","CREATE_COMPLETE"]
S3BUCKET=`aws cloudformation list-stack-resources --stack-name s3test | jq -r .StackResourceSummaries[].PhysicalResourceId`
aws s3 cp s3.yaml s3://${S3BUCKET}
aws s3 ls s3://${S3BUCKET}

AWSコンソール

CloudFormationテンプレートの「削除」を実施。 The bucket you tried to delete is not empty で 削除失敗となった状態で、「削除を再試行」を試みました。

CFNイベント

「このスタック全体を強制削除」の選択が可能になりました。

強制削除

DeletionMode FORCE_DELETE_STACK option turned on. により、S3バケットの削除が省略された状態で、 CloudFormationテンプレートの撤去が完了しました。

削除完了

CLI

CLIの実行環境はCloudshellを利用しました。

awscli更新

CloudFormation の新機能をサポートする最新バージョンに更新しました。

curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip -q awscliv2.zip
sudo ./aws/install --update
aws --version
$ aws --version
aws-cli/2.15.57 Python/3.11.8 Linux/5.10.215-203.850.amzn2.x86_64 exec-env/CloudShell exe/x86_64.amzn.2023

STANDARD

DeletionModeの指定を省略(デフォルト)時、 テンプレートの削除に失敗しました。

$ aws cloudformation delete-stack --stack-name s3test 

$ aws cloudformation describe-stacks --stack-name s3test --query 'Stacks[0].[StackStatus,StackStatusReason]'
[
    "DELETE_FAILED",
    "The following resource(s) failed to delete: [S3Bucket]. "
]

FORCE_DELETE_STACK

削除に失敗、ステータスが「DELETE_FAILED」となったスタックの削除時、 --deletion-mode FORCE_DELETE_STACK を指定する事で、テンプレートの強制削除が完了しました。

$ aws cloudformation delete-stack --stack-name s3test --deletion-mode FORCE_DELETE_STACK
$ aws cloudformation describe-stacks --stack-name s3test 

An error occurred (ValidationError) when calling the DescribeStacks operation: Stack with id s3test does not exist

削除に失敗したスタックのみが、強制削除「FORCE_DELETE_STACK」の処理対象です。

削除操作を実施していない、正常ステータスのスタックを対象に「FORCE_DELETE_STACK」 を指定した削除を試みると、バリデーションエラーが発生します。

$ aws cloudformation delete-stack --stack-name s3test --deletion-mode FORCE_DELETE_STACK

An error occurred (ValidationError) when calling the DeleteStack operation: Invalid operation on stack [arn:aws:cloudformation:ap-northeast-3:000000000000:stack/s3test/000000000000-19db-11ef-8ef0-000000000000]. You can activate DeletionMode FORCE_DELETE_STACK in a delete stack operation only when the stack is in the DELETE_FAILED state.

後片付け

強制削除により、削除をスキップした S3上のオブジェクトとバケットの削除を実施しました。

$ aws s3 rm s3://${S3BUCKET}/s3.yaml
delete: s3://s3test-ap-northeast-3-000000000000/s3.yaml

$ aws s3 rb s3://${S3BUCKET}
remove_bucket: s3test-ap-northeast-3-000000000000

まとめ

削除に失敗した CloudFormationスタック、 従来も残すリソースを明示する事で削除を再試行する事は可能でしたが、今回のアップデートでより簡単にスタックの撤去が可能となりました。

リソース保持するスタック削除

CloudFormationスタックの撤去時、 異なるテンプレートで管理されるリソース間の依存関係や、削除タイミングなどで削除失敗する事がありますが、 スタックの撤去を完了させる手段の一つとして、今回サポートされた強制削除、FORCE_DELETE_STACKオプションをお試しください。

なお、 CloudFormationスタックの強制削除を実施した場合、削除をスキップしたAWSリソースについて確認を実施し、 課金対象となるリソースや、名称などに一意性が求められるリソースが残されていた場合、手動撤去を実施頂くことをお勧めします。

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.