CloudForamtion のテンプレート検証や、構築作業で使うコマンドをMakefileに定義して実行します。
テンプレート検証は、cfn-lint を使用します。
cfn-lintの詳細は下記URLを参照してください。
Makefileの作成
今回は、Makefile を下記のように記載します。
- cfn で始まるテンプレート(cfn_*.yml) を検証
- VPC, ECRを作成するaws-cliコマンドを実行(create-stack, update-stack)
- Stackのリストを参照
CFNLINTCMD := cfn-lint CFNCMD := create-stack AWSPROFILE := blue21 all: cfnlint cfnlint: $(CFNLINTCMD) cfn_*.yml clean: rm -f README.html *~ # VPC d00vpc: aws cloudformation $(CFNCMD) \ --profile $(AWSPROFILE) \ --stack-name Blue21Ops00Vpc00D00 \ --template-body file://`pwd`/cfn_vpc.yml # ECR d00ecr: aws cloudformation $(CFNCMD) \ --profile $(AWSPROFILE) \ --stack-name Blue21Ops00Ecr00 \ --template-body file://`pwd`/cfn_ecr.yml # LIST cfnlist: aws cloudformation describe-stacks \ --profile $(AWSPROFILE) \ --query 'Stacks[].{StackName:StackName,StackStatus:StackStatus,Desc:Description}' \ --output table
cfnテンプレートの検証
下記のようにmakeコマンドを実行すると cfn-lint コマンドが実行されます。
エラーがなければ下記のようにメッセージは表示されません。
$ make cfn-lint cfn_*.yml
下記は未使用のパラメータ(Dummy)が定義された場合のエラーメッセージです。
エラーのあるテンプレートファイル名、行、カラムが表示されます。
$ make cfn-lint cfn_*.yml W2001 Parameter Dummy not used. cfn_ecr.yml:17:3 make: *** [Makefile:8: cfnlint] Error 4
create-stack/update-stack の実行
Makefileに記載した awscli コマンドを実行して CloudFormationのスタックを作成したり更新することができます。
ECRを create-stackする場合は、下記のようにmakeコマンドを実行します。
$ make d00ecr
ECRを update-stackする場合は、下記のようにmakeコマンドを実行します。
$ make d00ecr CFNCMD=update-stack
stack一覧の参照
CloudFormationのstack一覧を見たいときは、下記のようにmakeコマンドを実行します。
$ make cfnlist aws cloudformation describe-stacks \ --profile blue21 \ --query 'Stacks[].{StackName:StackName,StackStatus:StackStatus,Desc:Description}' \ --output table ----------------------------------------------------------------------------- | DescribeStacks | +---------------------------+---------------------------+-------------------+ | Desc | StackName | StackStatus | +---------------------------+---------------------------+-------------------+ | ECR | Blue21Ops00Ecr00 | CREATE_COMPLETE | | VPC & subnet | Blue21Ops00Vpc00D00 | UPDATE_COMPLETE | +---------------------------+---------------------------+-------------------+
emacs から make コマンドを実行
emacs から make コマンドを実行したい場合は、~/.emacs.d/init.el に下記を記載します。
(define-key global-map "\C-xc" 'compile)
Ctl+x のあと c キーで下記のように make コマンドが表示されるので、このままENTERすると検証コマンド(cfn-lint)が実行されます。
cfn-lint の実行結果は、下記のようにして参照できます。