LoginSignup
1
0

CloudFormation実行

※各環境の実行したい処理単位のフォルダに入る

title: CloudFormation実行
$ cd <実行したい処理単位のフォルダ> 
$ pull
$ ../../build
build.sh実行

CloudFormation手動実行

手動実行(WAFの例)

title: 手動実行(WAFの例)
StackID=141-WAF
aws cloudformation deploy --template-file ./cfn/${StackID}.yml --stack-name DEV-${StackID} --parameter-overrides $(grep -Evh ^# ./cfn/common_param.ini ./cfn/${StackID}.ini | tr '\n' ' ') StackName=DEV-${StackID} MaintenanceMode=true TestMode=false

CloudFormationテンプレート検証

title: CloudFormationテンプレート検証
find . -name '*.yml' -exec aws cloudformation validate-template --template-body file://{} --query 'Description' --no-cli-pager \;                                                                                                                                                                
null
null
null
 snip.
An error occurred (ValidationError) when calling the ValidateTemplate operation: Unresolved resource dependencies [NGW02] in the Outputs block of the template
null
"Lambda-ExportLog"
"Lambda-xxx"
"Lambda-yyy"
null

※ 「An error occurred (ValidationError) when calling the ValidateTemplate operation: ~」が出力されたら、テンプレートの書式エラー

CloudFormationスタック実行結果参照

マネコンチックにスタックの実行結果を参照

title: マネコンチックにスタックの実行結果を参照する
$ aws cloudformation describe-stack-events --stack-name $STACK_NAME --output table --query 'reverse(sort_by(StackEvents[*].{Timestamp:Timestamp, LogicalID:LogicalResourceId, Status:ResourceStatus, Reason:ResourceStatusReason}, &Timestamp))'

CloudFormationスタック ステータスクリア

スタックステータスがROLLBACK_XXXXになった時のクリア方法

title: スタックステータスがROLLBACK_XXXXになった時のクリア方法
$ STACK_NAME=CloudFormationスタック名>
$ DATADOG_TAG=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --output text --query '(Stacks[0].Parameters[?contains(ParameterValue, `datadog`)].ParameterKey)[0]')
$ aws cloudformation update-stack --stack-name $STACK_NAME --use-previous-template --parameters $(aws cloudformation describe-stacks --stack-name $STACK_NAME --output text --query "Stacks[0].Parameters[?ParameterKey!=\`${DATADOG_TAG}\`].ParameterKey" | tr '\t' '\n' | sed -E 's/(^.*$)/ParameterKey=\1,UsePreviousValue=true/' | tr '\n' ' ') ParameterKey=${DATADOG_TAG},ParameterValue=$(date +datadog%s)
$ aws cloudformation wait stack-update-complete --stack-name $STACK_NAME
$ aws cloudformation describe-stacks --stack-name $STACK_NAME --query 'Stacks[*].StackStatus'

CloudFormationスタック 削除系

スタックプレフィックス指定で一括削除

title: スタックプレフィックス指定で一括削除
while read s ; do echo "### $s ###" ; aws cloudformation delete-stack --stack-name "$s" ; done < <(aws cloudformation list-stacks --query 'StackSummaries[?contains(StackName,`STG-`)].StackName' --output text | tr "\t" "\n")

CloudFormationのエラーになったスタックを一括で削除

title: CloudFormationエラースタック一括削除
while read s ; do echo "### Stack:${s} ###" ; aws cloudformation delete-stack --stack-name "$s" ; done < <(aws cloudformation list-stacks --stack-status-filter ROLLBACK_COMPLETE --query 'StackSummaries[*].StackName' --output text | tr "\t" "\n")

まとめ

本稿が、CloudFormation実行を行いたい方の参考になれば嬉しいです。

ITエンジニア向け、AWS資格取得を目指す方向けに個人ブログで記事も投稿していますので、
合わせて読んでもらえると嬉しいです。
ことなの学校(個人ブログ)

更によりレベル感の高そうなAWS関連の発信はZennの方で行います。
こちらももしよければフォロー、ご確認いただけると嬉しいです。
Zenn個人アカウント

Twitterでも役立つ情報を発信しますのでフォローしてもらえると嬉しいです
@tomo_ITBizBlog

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0