テンプレートの検証
# テンプレートの構文チェック
aws cloudformation validate-template \
--template-body file://template.yaml
スタックの作成
# 基本的なスタック作成
aws cloudformation create-stack \
--stack-name my-stack \
--template-body file://template.yaml
# 基本的なS3 URL指定
aws cloudformation create-stack \
--stack-name my-stack \
--template-url https://s3.amazonaws.com/my-bucket/template.yaml
# パラメータを指定してスタック作成
aws cloudformation create-stack \
--stack-name my-stack \
--template-body file://template.yaml \
--parameters ParameterKey=InstanceType,ParameterValue=t3.micro
スタックの状態確認
# スタックの一覧表示
aws cloudformation list-stacks
# 特定のスタック詳細を確認
aws cloudformation describe-stacks \
--stack-name my-stack
# スタック内のリソース一覧
aws cloudformation list-stack-resources \
--stack-name my-stack
スタックの更新
# スタックの更新
aws cloudformation update-stack \
--stack-name my-stack \
--template-body file://template.yaml
# パラメータを変更して更新
aws cloudformation update-stack \
--stack-name my-stack \
--template-body file://template.yaml \
--parameters ParameterKey=InstanceType,ParameterValue=t3.small
スタックイベントの確認
# スタックで何が起きているか確認
aws cloudformation describe-stack-events \
--stack-name my-stack
スタックの削除
# スタックの削除
aws cloudformation delete-stack \
--stack-name my-stack
実行完了まで待機
# スタック作成完了まで待つ
aws cloudformation wait stack-create-complete \
--stack-name my-stack
# スタック更新完了まで待つ
aws cloudformation wait stack-update-complete \
--stack-name my-stack
# スタック削除完了まで待つ
aws cloudformation wait stack-delete-complete \
--stack-name my-stack
例)EC2を作成する基本フロー
# 1. テンプレートを検証
aws cloudformation validate-template \
--template-body file://ec2-template.yaml
# 2. スタックを作成
aws cloudformation create-stack \
--stack-name my-ec2-stack \
--template-body file://ec2-template.yaml
# 3. 作成完了を待つ
aws cloudformation wait stack-create-complete \
--stack-name my-ec2-stack
# 4. 作成されたリソースを確認
aws cloudformation list-stack-resources \
--stack-name my-ec2-stack
# 5. スタックの状態を確認
aws cloudformation describe-stacks \
--stack-name my-ec2-stack
# 6. 不要になったら削除
aws cloudformation delete-stack \
--stack-name my-ec2-stack