Skip to main content
Welcome. This lesson explains how to validate and enforce policies for AWS CloudFormation templates using cfn-lint and CloudFormation Guard (CFN Guard), and how to integrate these checks into CI/CD pipelines (for example, CodeBuild/CodePipeline). Validating templates early prevents syntax mistakes, service-specific property issues, security misconfigurations, and governance violations from reaching production—think of it as a pre-deployment inspection checklist for your infrastructure-as-code.
A diagram titled "CloudFormation Best Practices" showing an AWS architecture with a VPC spanning two availability zones that includes NAT Gateways, EC2 Auto Scaling, auto scaling groups and instances. To the right are tool suggestions like cfn-lint, CloudFormation Guard, and automated checks.

cfn-lint

cfn-lint is the primary syntactic and service-aware linter for CloudFormation templates. It catches:
  • Syntax errors and invalid YAML/JSON structure
  • Missing or incorrect resource properties and types
  • Service-specific constraints (S3, EC2, IAM, etc.)
  • Region or property-specific validation issues
When cfn-lint finds a problem it reports the file and line numbers so you can fix the template immediately. Typical workflow:
  • Install and run cfn-lint locally during authoring.
  • Run cfn-lint in CI (for pull requests and main branch builds).
  • Fix reported issues (property/type errors, missing required attributes, etc.) before moving on to policy checks.
The image is a slide titled "cfn-lint – CloudFormation Template Validation" showing a command-line icon feeding a CloudFormation template graphic. To the right are three checked items labeled "Syntax errors," "Incorrect properties," and "AWS-specific rules" indicating what the tool validates.

CloudFormation Guard (CFN Guard)

CloudFormation Guard (CFN Guard) is a declarative rules engine to enforce organizational policies against CloudFormation templates. Use CFN Guard to:
  • Block insecure or non-compliant configurations (e.g., public S3 buckets)
  • Enforce tagging standards and cost-control constraints
  • Restrict instance types, require encryption, or mandate specific settings
CFN Guard complements cfn-lint: first run syntactic validation (cfn-lint), then run CFN Guard to enforce policies. Only templates that pass both should move on in the pipeline.
A slide titled "CloudFormation Guard — Enforcing Security and Compliance" showing a cfn-guard file feeding into a template icon that outputs checkmarks for Security policies, Cost optimization, and Governance rules.

Automated CI/CD Checks

Integrate both tools into CI/CD pipelines so changes fail early when templates are invalid or non-compliant. A common pattern:
  1. Source stage (CodeCommit, GitHub, Bitbucket, etc.)
  2. Build stage (CodeBuild) that runs:
    • aws cloudformation validate-template for quick server-side validation
    • cfn-lint for local syntactic and property checks
    • cfn-guard to validate policies and organizational rules
  3. If all checks pass, the pipeline continues to deploy via CloudFormation
Notes:
  • AWS CodeCommit is an AWS-hosted Git provider; the pipeline tooling works the same with GitHub, Bitbucket, or other hosts.
  • Add runtime and post-deployment safeguards: AWS Config drift detection, stack policies to protect critical resources, and change controls (approval stages) as needed.
Include both syntactic validation (cfn-lint / aws cloudformation validate-template) and policy checks (CFN Guard) in your CI/CD pipeline. This prevents invalid or non-compliant templates from reaching deployment.
Always run syntactic and type validation (cfn-lint) before policy enforcement (CFN Guard). Policy rules assume the template is structurally valid—running Guard first may produce confusing results.

Tools and quick examples

Quick local commands:

Example CodeBuild buildspec (concept)

Below is a concise buildspec that demonstrates the validation order. Adjust the installation steps to match your CodeBuild image or use a custom Docker image that already includes the tools.
Replace rules.guard with the path to your CFN Guard rules file. Many teams host rules in the same repo and keep them under a rules/ directory.

Best-practices checklist

  • Run aws cloudformation validate-template as a quick server-side check.
  • Run cfn-lint for thorough syntactic and property validation.
  • Run CFN Guard to enforce organizational security and cost policies.
  • Include these checks in PR / merge pipelines (fail fast on invalid or non-compliant templates).
  • Use AWS Config and CloudFormation drift detection post-deployment.
  • Apply stack policies and change approvals for critical stacks.

References and further reading

This lesson is a primer. Use the practical demo and pipeline example to see how these checks execute in an automated workflow, and iterate on your rules and linting configuration as your organization’s policies evolve.

Watch Video