この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
そろそろ寝ようかと思ってスマホを眺めていたところ、AWS SAM CLI で Delete 機能が追加されたリリースを見つけました。
Happy Friday #serverless AWS SAM users! Here's a little present for you. I am pretty excited about this. You can now tear down your stacks from within SAM. Time to update! https://t.co/wy6MmUkunN
— Eric Johnson | vacationing with the fam til 8/30 (@edjgeek) August 20, 2021
普段、SAM CLI を使っているとき、スタックを削除しようと思うと Cloudformation からスタックを削除する必要があり、「SAM CLI で作るだけじゃなく削除もできると楽なのになぁ」と思っていました。
今回のアップデートでは「スタック」と「S3 にあるアーティファクト」と「テンプレート」を削除することができるようになりました。それでは早速試してみたいと思います。
事前準備
まずは SAM CLI をアップデートします。私は Mac を使っているので brew
コマンドでアップデートしました。
$ brew upgrade aws-sam-cli
リリースノートにある最新バージョンになりました。
$ sam --version
SAM CLI, version 1.29.0
サンプルスタックを作成する
次に、削除するためのサンプルを作成していきましょう。
sam init
$ sam init \
--runtime python3.7 \
--name sam-update-test \
--app-template hello-world \
--package-type Zip
ビルド
$ sam build
デプロイ
$ sam package \
--output-template-file packaged.yaml \
--s3-bucket my-artifact-bucket
$ sam deploy \
--template-file packaged.yaml \
--stack-name sam-update-test-stack \
--s3-bucket my-artifact-bucket \
--capabilities CAPABILITY_NAMED_IAM \
--no-fail-on-empty-changeset
削除前の確認
デプロイできたので、Delete コマンドを実行する前にいろいろ確認しておきましょう。API Gateway が正常にデプロイできていることが分かります。
% curl https://max8jaipi6.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/
{"message": "hello world"}
S3 上にアーティファクトとしてコードやテンプレートがあることも確認します。
(下記の 2 オブジェクトが今回のデプロイで生成されたものになります)
% aws s3 ls s3://my-artifact-bucket |egrep '88bd6722efd39f95c2a8ca00aad6e623|9942300952373ba4b0a6b168e60ddb70.template'
2021-08-21 02:10:03 443062 88bd6722efd39f95c2a8ca00aad6e623
2021-08-21 02:14:46 1061 9942300952373ba4b0a6b168e60ddb70.template
デプロイしたスタックも確認しておきましょう。ステータスが"StackStatus": "CREATE_COMPLETE"
となっていることが確認できました。
% aws cloudformation list-stacks --query 'StackSummaries[?StackName==`sam-update-test-stack`]'
[
{
"StackId": "arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxx:stack/sam-update-test-stack/1df38680-01da-11ec-9dc1-0a93a4237141",
"StackName": "sam-update-test-stack",
"TemplateDescription": "sam-update-test\nSample SAM Template for sam-update-test\n",
"CreationTime": "2021-08-20T17:14:45.969000+00:00",
"LastUpdatedTime": "2021-08-20T17:14:56.632000+00:00",
"StackStatus": "CREATE_COMPLETE",
"DriftInformation": {
"StackDriftStatus": "NOT_CHECKED"
}
}
]
Delete コマンドを実行
いよいよ Delete コマンドを実行します。
$ sam delete \
--stack-name sam-update-test-stack
実行すると削除していいか確認されるので、肯定して先に進めます。
Are you sure you want to delete the stack sam-update-test-stack in the region ap-northeast-1 ? [y/N]: y
Do you want to delete the template file 9942300952373ba4b0a6b168e60ddb70.template in S3? [y/N]: y
アーティファクト、テンプレート、スタックのそれぞれを削除するメッセージが表示されて終了です。
- Deleting S3 object with key 88bd6722efd39f95c2a8ca00aad6e623
- Deleting S3 object with key 9942300952373ba4b0a6b168e60ddb70.template
- Deleting Cloudformation stack sam-update-test-stack
Deleted successfully
Delete 結果の確認
最後に削除されたことを確認しておきましょう。
まずは、S3 上のオブジェクトを確認してみます。リストの結果に何も返ってこないので削除されたことが分かります。
aws s3 ls s3://my-artifact-bucket |egrep '88bd6722efd39f95c2a8ca00aad6e623|9942300952373ba4b0a6b168e60ddb70.template'
(何も返ってこない)
CloudFormation のスタックも"StackStatus": "DELETE_COMPLETE"
となっていて削除されていますね。
% aws cloudformation list-stacks --query 'StackSummaries[?StackName==`sam-update-test-stack`]'
[
{
"StackId": "arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxx:stack/sam-update-test-stack/1df38680-01da-11ec-9dc1-0a93a4237141",
"StackName": "sam-update-test-stack",
"TemplateDescription": "sam-update-test\nSample SAM Template for sam-update-test\n",
"CreationTime": "2021-08-20T17:14:45.969000+00:00",
"LastUpdatedTime": "2021-08-20T17:14:56.632000+00:00",
"DeletionTime": "2021-08-20T17:34:02.692000+00:00",
"StackStatus": "DELETE_COMPLETE",
"DriftInformation": {
"StackDriftStatus": "NOT_CHECKED"
}
}
]
リソースも削除済みです。
% curl https://max8jaipi6.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/
curl: (6) Could not resolve host: max8jaipi6.execute-api.ap-northeast-1.amazonaws.com
最後に
SAM CLI で削除まで一貫して対応できるようになりました。AWS SAM を愛用している方はぜひご利用ください!
以上です。