3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

aws cloudformationコマンドをMakefileにまとめたらチョットダケ便利になった

Posted at

CloudFormationをAWS CLIで直接操作すると色々辛いし、そもそもコマンドやオプションが難しい。

rain という優れたツールもあるが、そこまで多機能でなくても良いのでサッと使えるものが欲しかった。

Usage

# テンプレートのバリデーション
make validate
# スタックの新規作成
make create
# スタックの更新
make update
# スタックの削除
make delete

Makefile

Markdownからコピペするとインデントがスペースになるので、タブに変換してください。

STACK_NAME=スタック名
CHANGE_SET_NAME=変更セット名

help: ## Display this help.
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

validate: ## Validate the template.
	aws cloudformation validate-template \
	--template-body file://$(STACK_NAME).yml

create: ## Create the stack and wait it.
	aws cloudformation create-change-set \
	--change-set-type CREATE \
	--stack-name $(STACK_NAME) \
	--change-set-name $(CHANGE_SET_NAME) \
	--template-body file://$(STACK_NAME).yml \
	--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

	aws cloudformation wait change-set-create-complete \
	--stack-name $(STACK_NAME) \
	--change-set-name $(CHANGE_SET_NAME)

	aws cloudformation execute-change-set \
	--stack-name $(STACK_NAME) \
	--change-set-name $(CHANGE_SET_NAME)

	aws cloudformation wait stack-create-complete \
	--stack-name $(STACK_NAME)

update: ## Update the stack and wait it.
	aws cloudformation create-change-set \
	--change-set-type UPDATE \
	--stack-name $(STACK_NAME) \
	--change-set-name $(CHANGE_SET_NAME) \
	--template-body file://$(STACK_NAME).yml \
	--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

	aws cloudformation wait change-set-create-complete \
	--stack-name $(STACK_NAME) \
	--change-set-name $(CHANGE_SET_NAME)

	aws cloudformation execute-change-set \
	--stack-name $(STACK_NAME) \
	--change-set-name $(CHANGE_SET_NAME)

	aws cloudformation wait stack-update-complete \
	--stack-name $(STACK_NAME)

delete: ## Delete the stack and wait it.
	aws cloudformation delete-stack \
	--stack-name $(STACK_NAME)

	aws cloudformation wait stack-delete-complete \
	--stack-name $(STACK_NAME)

感想

  • はじめてMakefileを書いたので、上記の品質はとても自信がない。(改善点のコメントお待ちしております)
    • そもそもMakefileはこういう使い方をするものなのか?
  • aws cloudformation waitは30秒ごとにチェックする仕様なので、最低でも30秒待たされるのがイケてない(シェルで5秒間隔でdescribe-stacksを叩いてチェックするのが良さそう)

参考文献

3
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?