やっつけ不定記

好きなときに好きなことをちゃっちゃと書いてます

AWS CloudFormation Guardを入れてみた

AWSさんがCloudFormation(cFn)テンプレートを検証するcloudformation-guard
https://github.com/aws-cloudformation/cloudformation-guard
https://aws.amazon.com/jp/about-aws/whats-new/2020/06/introducing-aws-cloudformation-guard-preview/
https://www.infoq.com/jp/news/2020/07/cloudformation-guard/
というツールをプレビュー公開していたので試してみました。
cFn自体は最近触り始めたところでほとんど知らんという周回遅れ状態ですが、
Example動くところまでいけたので、ツール動かす分には何とかなっていると思います。

流れとしては、Rustインストール→cfn-guardインストール。
公式サイトではUbuntuでやっているようですが、
Rustのコンテナイメージでやるのもありかもしれません(Rustさえ動けばどうとでもなりそう)。
自分はAWSつながりで、あえてVirtualBox上のEC2のVMに入れたDocker上でやってみて、
以下はそのメモです。ここではExample実行前にcfn-guard-rulegenを入れていますが、
Exampleを動かすだけならなくてもいいかも。

(1)コンテナ作成

$ sudo docker pull amazonlinux
$ sudo docker run -it --name cfnguard amazonlinux:latest /bin/bash

(2)必要モジュールの導入

bash-4.2# sudo yum install -y wget unzip gcc make

(3)Rustの導入

bash-4.2# cd /root
bash-4.2# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
bash-4.2# source $HOME/.cargo/env

(4)cfn-guardの導入

bash-4.2# wget https://github.com/aws-cloudformation/cloudformation-guard/archive/master.zip
bash-4.2# unzip master.zip
bash-4.2# cd cloudformation-guard-master
bash-4.2# make
 →/root/cloudformation-guard-master/binディレクトリにcfn-guardができる。
bash-4.2# /root/cloudformation-guard-master/bin/cfn-guard --version
CloudFormation Guard 0.5.2

(5)cfn-guard-rulegenの導入

bash-4.2# cd /root/cloudformation-guard-master/cfn-guard-rulegen
bash-4.2# make
 →/root/cloudformation-guard-master/cfn-guard-rulegen/binディレクトリにcfn-guard-rulegenができる。
bash-4.2# /root/cloudformation-guard-master/cfn-guard-rulegen/bin/cfn-guard-rulegen --version
CloudFormation Guard RuleGen 0.5.2

(6)Exampleの確認および実行

bash-4.2# ls /root/cloudformation-guard-master/Examples/
README.md                 ebs-volume-template.ruleset  waf-template.yaml
ebs-volume-template.json  waf-template.ruleset
bash-4.2# cd /root/cloudformation-guard-master
bash-4.2# ./bin/cfn-guard -t Examples/ebs-volume-template.json -r Examples/ebs-volume-template.ruleset
 →公式ドキュメントは各ファイル名中の「-」が「_」になっており、たぶん誤植
[NewVolume2] failed because [Encrypted] is [false] and the permitted value is [true]
[NewVolume2] failed because [Size] is [99] and the permitted value is [100]
[NewVolume2] failed because [us-west-2c] is not in [us-east-1a,us-east-1b,us-east-1c] for [AvailabilityZone]
[NewVolume] failed because [Encrypted] is [false] and the permitted value is [true]
[NewVolume] failed because [Size] is [101] and the permitted value is [100]
[NewVolume] failed because [us-west-2b] is not in [us-east-1a,us-east-1b,us-east-1c] for [AvailabilityZone]
Number of failures: 6