cfn-lint

Learn how the starter kit uses cfn-lint to validate CloudFormation templates and enforce AWS best practices.


Overview

cfn-lint is an open-source tool from AWS that validates CloudFormation templates against the CloudFormation resource specification. It catches errors before deployment and enforces best practices.

How the starter kit uses cfn-lint

Integration PointDescription
Local validationRun cfn-lint templates/*.yml directly before committing
CI enforcementDeploy workflow runs cfn-lint before Rain deployment; failing checks block the pipeline
VS Code integrationPair with the CloudFormation Linter extension for real-time validation

Note: The validate-templates.sh script runs Checkov only. Run cfn-lint directly for template syntax validation.

Running cfn-lint

Directly

# Validate all templates
cfn-lint templates/*.yml

# Validate a single template
cfn-lint templates/oidc-provider.yml

# With specific configuration
cfn-lint --config .cfnlintrc templates/*.yml

Common issues detected

Missing required properties

Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          # Missing required Rules property

Error: E3002: Invalid property for resource

Invalid property values

Resources:
  Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.invalid  # Invalid instance type

Error: E3030: Value must be one of the allowed values

Incorrect intrinsic functions

Resources:
  BucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref Bucket
      PolicyDocument:
        Statement:
          - Effect: Allow
            Resource: !Ref Bucket  # Should use !GetAtt Bucket.Arn

Error: E1012: Ref is not valid for resource type

Configuration

The starter kit includes a .cfnlintrc file. See the linting configuration section for customization details.

Configuration file format

# .cfnlintrc
templates:
  - templates/*.yml
  - templates/*.yaml

ignore_templates:
  - templates/experimental/*.yml

regions:
  - us-east-1
  - eu-west-1

ignore_checks:
  - E3001  # Skip specific rule
  - W*     # Skip all warnings

configure_rules:
  E3012:
    strict: false

Configuration options

OptionDescription
templatesGlob patterns for templates to validate
ignore_templatesExclude specific templates
regionsValidate against specific regions
ignore_checksSkip specific rules by ID
include_checksInclude additional check types
configure_rulesFine-tune specific rules

Rule categories

CategoryDescription
E1xxxBasic CloudFormation errors (syntax, schema)
E2xxxTemplate structure errors (limits, formatting)
E3xxxResource specification errors (properties, values)
E4xxxFunction errors (Ref, GetAtt, Sub, etc.)
W1xxxBasic warnings
W2xxxTemplate structure warnings
W3xxxResource specification warnings
I1xxxInformational messages

Suppressing rules

Inline metadata suppression

Suppress rules for specific resources:

Resources:
  LegacyBucket:
    Type: AWS::S3::Bucket
    Metadata:
      cfn-lint:
        config:
          ignore_checks:
            - E3001
            - W3045
    Properties:
      BucketName: legacy-bucket

Template-level suppression

Suppress rules for the entire template:

Metadata:
  cfn-lint:
    config:
      ignore_checks:
        - W3045

Output formats

FormatCommandUse Case
Defaultcfn-lint templates/*.ymlHuman-readable output
JSONcfn-lint --format json templates/*.ymlParsing in scripts
JUnitcfn-lint --format junit templates/*.yml > results.xmlTest reporting
Parseablecfn-lint --format parseable templates/*.ymlTool integration

VS Code integration

Install the CloudFormation Linter extension for real-time validation:

  1. Install CloudFormation Linter extension
  2. Install cfn-lint: pip install cfn-lint
  3. Open any CloudFormation template
  4. See errors and warnings inline as you type

For more tips, read our guide on leveling up CloudFormation authoring in VS Code.

Best practices

  • Run locally first - Use ./scripts/validate-templates.sh before committing
  • Keep cfn-lint updated - Run pip install --upgrade cfn-lint regularly
  • Review all errors - Don't ignore errors; they usually indicate real problems
  • Use VS Code integration - Get real-time feedback while editing
  • Configure for your regions - Set the regions option to validate against target regions

Troubleshooting

Template not found

Check file paths in .cfnlintrc or command line arguments.

Python version issues

cfn-lint requires Python 3.8+:

python --version
pip install --upgrade cfn-lint

Outdated resource specs

If cfn-lint doesn't recognize new AWS resources:

pip install --upgrade cfn-lint

Next steps