# [PREVIEW] AWS CloudFormation Guard A command line tool for validating AWS CloudFormation resources against policy. ## Table of Contents * [About](#about) * [Writing Rules](#writing-rules) * [Troubleshooting](#troubleshooting) * [Building And Running](#to-build-and-run) * [Testing Code Changes](#to-test) # About `cfn-guard` is a tool for checking CloudFormation resources for properties using a light-weight, firewall-rule-like syntax. As an example of how to use it, given a CloudFormation template: ``` > cat ebs_volume_template.json { "Resources": { "NewVolume" : { "Type" : "AWS::EC2::Volume", "Properties" : { "Size" : 100, "Encrypted": false, "AvailabilityZone" : "us-east-1b" } }, "NewVolume2" : { "Type" : "AWS::EC2::Volume", "Properties" : { "Size" : 99, "Encrypted": true, "AvailabilityZone" : "us-east-1b" } } } } ``` And a Rules file ``` > cat ebs_volume_rule_set let encryption_flag = true let disallowed_azs = [us-east-1a,us-east-1b,us-east-1c] AWS::EC2::Volume AvailabilityZone NOT_IN %disallowed_azs AWS::EC2::Volume Encrypted != %encryption_flag AWS::EC2::Volume Size == 101 |OR| AWS::EC2::Volume Size == 99 |OR| AWS::EC2::Volume Size >= 101 AWS::IAM::Role AssumeRolePolicyDocument.Version == 2012-10-18 AWS::EC2::Volume AvailabilityZone != /us-east-.*/ ``` You can check the compliance of that template with those rules: ``` > cfn-guard check -t ebs_volume_template.json -r ebs_volume_rule_set "[NewVolume2] failed because [AvailabilityZone] is [us-east-1b] and the pattern [us-east-.*] is not permitted" "[NewVolume2] failed because [Encrypted] is [true] and that value is not permitted" "[NewVolume2] failed because [us-east-1b] is in [us-east-1a,us-east-1b,us-east-1c] which is not permitted for [AvailabilityZone]" "[NewVolume] failed because [AvailabilityZone] is [us-east-1b] and the pattern [us-east-.*] is not permitted" "[NewVolume] failed because [Size] is [100] and the permitted value is [101]" "[NewVolume] failed because [Size] is [100] and the permitted value is [99]" "[NewVolume] failed because [Size] is [100] and the permitted value is [>= 101]" "[NewVolume] failed because [us-east-1b] is in [us-east-1a,us-east-1b,us-east-1c] which is not permitted for [AvailabilityZone]" Number of failures: 7 ``` We designed `cfn-guard` to be plugged into your build processes. If CloudFormation Guard validates the CloudFormation templates successfully, it gives you no output and an exit status (`$?` in bash) of `0`. If CloudFormation Guard identifies a rule violation, it gives you a count of the rule violations, an explanation for why the rules failed, and an exit status of `2`. If there's a runtime error with the rule set or processing, it will exit with a `1`. If you want CloudFormation Guard to get the result of the rule check but still get an exit value of `0`, use the `-w` Warn flag. ## Check vs Rulegen `cfn-guard` has two modes: ### Check `check` (like the example above) checks templates against rulesets. ``` cfn-guard-check Check CloudFormation templates against rules USAGE: cfn-guard check [FLAGS] --rule_set --template FLAGS: -h, --help Prints help information -s, --strict-checks Fail resources if they're missing the property that a rule checks -v Sets the level of verbosity - add v's to increase output -V, --version Prints version information -w, --warn_only Show results but return an exit code of 0 regardless of rule violations OPTIONS: -r, --rule_set Rules to check the template against -t, --template CloudFormation Template ``` ### Rulegen `rulegen` takes a CloudFormation template and autogenerates a set of `cfn-guard` rules that match the properties of its resources. This is a useful way to get started rule-writing or just create ready-to-use rulesets from known-good templates. ``` cfn-guard-rulegen Autogenerate rules from an existing CloudFormation template USAGE: cfn-guard rulegen [FLAGS]