LoginSignup
1
0

More than 5 years have passed since last update.

CloudFormationの実験をラクにするMyスクリプト

Last updated at Posted at 2018-12-04

AWS CloudFormationの挙動確認をするために、自分のアカウントで実験して見ることってないですか? 私は週に3回くらいあります。

CloudFormationのデプロイを簡単にするMyスクリプトを作成してみました。

CloudFomationのデプロイを簡単にするスクリプト

こんな感じでデプロイできる、AWS CLIのラッパースクリプトです。
cfn update hirogaVpc ./vpc.yaml ./vpc.json

aws cloudformation ...と都度打たなくて済みます。また、指定するオプションは決まっているので都度打たないでいいようにしました。

以下がスクリプト本体です。

cfn.sh

cmd=$1
stack_name=$2

if [ -z $cmd ]; then
    echo "Error: Command is required"
    exit -1
elif [ $cmd = "create" -o $cmd = "update" ]; then
    case $3 in
        "") echo "Error: Please give a template file path!"; exit 0;;
        /*) absolute=$3 ;;
        *) absolute=$PWD/$3 ;;
        # S3にはこれから対応する、かも
    esac
    case $4 in
        /*) absolutejson=$4 ;;
        *) absolutejson=$PWD/$4 ;;
    esac
fi
if [ $cmd = "create" ]; then
    aws cloudformation create-stack --stack-name $stack_name --template-body file://$absolute --capabilities CAPABILITY_IAM --cli-input-json file://$absolutejson
elif [ $cmd = "update" ]; then
    aws cloudformation update-stack --stack-name $stack_name --template-body file://$absolute --capabilities CAPABILITY_IAM  --cli-input-json file://$absolutejson
elif [ $cmd = "delete" ]; then
    aws cloudformation delete-stack --stack-name $stack_name
elif [ $cmd = "status" ]; then
    aws cloudformation describe-stacks --stack-name $stack_name | jq .Stacks[] | jq '.StackId, .StackStatus'
fi

改善点もいくつかあって、
* create/updateを別にしているのがダサい。 aws cloudformation deployのように、スタックがあるかどうかに関わらずデプロイできるようにしたい。
* "直前に実行したデプロイ" のステータスが一発でわかるようにしたい(デプロイコマンドの結果を自動的に環境変数に代入する)
* stack-nameとtemplate-urlはjsonの中にまとめてしまっていい気がする。

という感じです。いずれバージョン2を出しますのでお楽しみに!

お約束

こういう自炊スクリプト書くの好き!どんどんラクしたい!という方、justInCaseではSREメンバーを募集中です!ぜひ一緒にお仕事させてください!
保険APIで保険フィンテック先駆けを作るサーバーサイドエンジニア!

余談

※本当はStepFunctionsでRDS→Dynamoにデータ連携した話を書きたかったんですが、そもそもRDSを立てるVPCがCFnで定義されていない→VPCをCFnに立てるトライ&エラーが面倒→じゃあそれをラクにするスクリプトを作ろう、という流れでこの記事を書きました。

1
0
2

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