Using CFN-LINT to Validate IAM Resources in AWS

When running a continuous delivery pipeline to reliably deploy AWS CloudFormation infrastructure, you want to run tests on your code to validate it is written well before it gets deployed.

aws cloudformation validate, the AWS command line interface command, is the most common solution used to verify your stacks, but since the command doesn’t have the ability to check IAM_CAPABILITY’, it can’t effectively validate IAM resources.


For instance, the example below will pass a validation check using aws cloudformation validate, even though property ‘BadField’ doesn’t exist.

AWSTemplateFormatVersion: '2010-09-09'
Description: IAM Role example
    Type: AWS::IAM::Role
      RoleName: cfn-lint-example
  *** BadField: ThisWillNotWork ***
        Version: '2012-10-17'
          - Effect: Allow
              - sts:AssumeRole
        - arn:aws:iam::aws:policy/S3ReadOnlyAccess

The response when running aws cloudformation validate-template --template-body file://iam.yaml will output:

    "Parameters": [],
    "Description": "IAM Roles for example cfn-lint",
    "Capabilities": [
    "CapabilitiesReason": "The following resource(s) require capabilities: [AWS::IAM::Role]"

The output above will not tell if you the IAM is validated or not, and will continue along unaware the IAM role in the CloudFormation stack will fail during deployment.


cfn-lint, a tool available from the awslabs open source github repository, is used to analyse Cloudformation code for errors, stylistic errors and bugs, and can be used as an alternative method to aws cloudformation validate to validate all Cloudformation, including IAM resources.


By using cfn-lint you will be able to effectively validate IAM resources inside CloudFormation stacks before deployment, instead of waiting to get to the deployment stages of continuous delivery pipeline to  have to guess the reason why your stacks are failing.

Validating the example above running cfn-lint iam.yaml will produce the following output:

iE3002 Invalid Property Resources/CFNLintexample/Properties/RandomField

This will give an error and fail the build, since the property doesn’t exist.


Validating Cloudformation code with aws cloudformation validate, does not validate IAM resources due to missing CAPABILITY_IAM switch in the command. One way to get around this limitation is to use cfn-lint, which not only checks Cloudformation for bad formatting, but also checks for resource creation errors, including IAM. This makes it handy to integrate Cloudformation into a continuous delivery pipeline so code can be tested before it gets deployed.

cfn-lint is available from the aws-labs repository at the following link where it can be installed by running pip install cfn-lint.

Share on facebook
Share on twitter
Share on linkedin