はじめに
AWS CodeBuildを使用して、CloudFormationテンプレートのチェックを自動化してみました。
全体像
CodeCommit
│
│ ソースコードの変更を保存
▼
AWS CodeBuild
│
├─ 1. json.tool
│ CFnテンプレート変数ファイル(JSON)の構文をチェック
│
├─ 2. cfn-lint
│ CFnテンプレートの構文やリソース定義をチェック
│
└─ 3. validate-template
AWS CloudFormation APIを使用して
CFnテンプレートをチェック
│
├─ 1つでも失敗
│ └─ ビルド失敗
│ CFnのデプロイは実行しない
│
└─ すべて成功
│
▼
ビルド成功
│
▼
AWS CloudFormation
│
│ CFnテンプレートと
│ 変数ファイルを使用してデプロイ
▼
CloudFormationスタック
│
▼
AWSリソースを作成・更新
ディレクトリ構成
.
├── buildspec/
│ └── validate.yml
└── cloudformation/
└── test/
├── vpc.yml
├── vpc-parameters.json
├── sg.yml
├── sg-parameters.json
├── eni.yml
├── eni-parameters.json
├── ec2.yml
└── ec2-parameters.json
buildspec
version: 0.2
phases:
install:
commands:
- python3 -m pip install --quiet cfn-lint
build:
commands:
- echo Validating parameter JSON files
- find cloudformation -type f -name '*.json' -print0 | xargs -0 -n1 python3 -m json.tool > /dev/null
- echo Running cfn-lint
- find cloudformation -type f \( -name '*.yml' -o -name '*.yaml' \) -print0 | xargs -0 cfn-lint
- echo Running CloudFormation ValidateTemplate
- for template in $(find cloudformation -type f \( -name '*.yml' -o -name '*.yaml' \)); do echo "Validating ${template}"; aws cloudformation validate-template --template-body "file://${template}" > /dev/null; done
検証
validate成功時
- ビルドログ
[Container] Entering phase BUILD
[Container] Running command echo Validating parameter JSON files
Validating parameter JSON files
[Container] Running command find cloudformation -type f -name '*.json' -print0 |
xargs -0 -n1 python3 -m json.tool > /dev/null
[Container] Running command echo Running cfn-lint
Running cfn-lint
[Container] Running command find cloudformation -type f \( -name '*.yml' -o -name '*.yaml' \) \
-print0 | xargs -0 cfn-lint
[Container] Running command echo Running CloudFormation ValidateTemplate
Running CloudFormation ValidateTemplate
[Container] Running command for template in $(find cloudformation -type f \
\( -name '*.yml' -o -name '*.yaml' \)); do
echo "Validating ${template}"
aws cloudformation validate-template --template-body "file://${template}" > /dev/null
done
Validating cloudformation/mgt/sg.yml
Validating cloudformation/mgt/ec2.yml
Validating cloudformation/mgt/eni.yml
Validating cloudformation/mgt/vpc.yml
Validating cloudformation/shared/ssm-endpoint.yml
Validating cloudformation/shared/cmn-sg.yml
Validating cloudformation/test/vpc.yml
Validating cloudformation/test/eni.yml
Validating cloudformation/test/ec2.yml
Validating cloudformation/test/sg.yml
[Container] Phase complete: BUILD State: SUCCEEDED
validate失敗時
ec2.ymlに下記のタグ誤りを追加して、検証
Kay: AutoStop
- ビルドログ
cfn-lintにて、コマンド失敗し、パイプラインのCodeBuildフェーズがFAILEDになっていることを確認
[Container] Running command find cloudformation -type f \( -name '*.yml' -o -name '*.yaml' \) -print0 | xargs -0 cfn-lint
E3002 Additional properties are not allowed ('Kay' was unexpected)
cloudformation/test/ec2.yml:76:11
E3003 'Key' is a required property
cloudformation/test/ec2.yml:76:11
E3024 'Key' is a required property
cloudformation/test/ec2.yml:76:11
[Container] Command did not exit successfully find cloudformation -type f \( -name '*.yml' -o -name '*.yaml' \) -print0 | xargs -0 cfn-lint exit status 123
[Container] Phase complete: BUILD State: FAILED
[Container] Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: find cloudformation -type f \( -name '*.yml' -o -name '*.yaml' \) -print0 | xargs -0 cfn-lint. Reason: exit status 123

