Skip to main content
Welcome. This lesson explains how to validate and enforce best practices for AWS CloudFormation templates using cfn-lint, CloudFormation Guard (cfn-guard), and automated CI/CD checks. These tools help you catch syntax errors, misconfigurations, and policy violations early—reducing human error and preventing insecure or noncompliant deployments. CloudFormation supports multiple layers of validation and policy enforcement. When combined into a pipeline, they form a repeatable, automated gate that ensures templates are syntactically correct, conform to organizational policies, and meet operational controls before resource creation.
A "CloudFormation Best Practices" diagram of an AWS architecture showing a VPC across two Availability Zones with NAT Gateways, Amazon EC2 Auto Scaling, auto scaling groups and instances. To the right are tooling items labeled "cfn-lint", "CloudFormation Guard" and "Automated checks".
Why automate template checks?
  • Reduce manual verification and review time.
  • Prevent insecure or costly configuration from reaching production.
  • Enforce organizational policies consistently across teams.
  • Provide fast, actionable feedback to authors in pull requests and CI builds.

cfn-lint — Syntax and AWS-specific template validation

cfn-lint is a command-line linter that validates CloudFormation YAML/JSON templates for syntax, schema rules, property types, required properties, and many AWS service–specific constraints (S3, EC2, IAM, etc.). It pinpoints offending lines to make remediation straightforward. Quick local workflow:
  • Install cfn-lint on your development machine or in CI.
  • Run it as a pre-commit hook and in CI to catch issues before merge.
  • Fix the reported errors until the linter exits clean.
Install and run:
Example error output:
This shows the rule ID, message, and the template path to inspect. Use cfn-lint as the syntactic and structural gate in your pipeline.
A diagram titled "cfn-lint — CloudFormation Template Validation" showing a command-line icon sending a CloudFormation template into a linter that reports checks. The right side lists checked items with green ticks: syntax errors, incorrect properties, and AWS-specific rules.

CloudFormation Guard (cfn-guard) — Policy enforcement for templates

CloudFormation Guard (cfn-guard) lets you codify organization policies as rule files and run them against CloudFormation templates. Typical uses include enforcing tag standards, restricting instance sizes, preventing public exposure of resources, and disallowing unencrypted storage. Workflow pattern:
  • Define rules describing allowed or disallowed properties and values.
  • Execute cfn-guard locally and in CI to validate templates.
  • Fail the build when templates violate policy so only compliant changes are deployed.
Example invocation:
Use cfn-guard as a policy gate in pull-requests and CI to enforce security, compliance, and cost controls consistently.
A slide titled "CloudFormation Guard — Enforcing Security and Compliance" showing a cfn-guard file feeding into a template icon. Three green checkmarks list enforced checks: Security policies, Cost optimization, and Governance rules.

Tooling summary

Automating checks in CI/CD pipelines

Integrate these validations into your CI/CD so templates are gated before deployment:
  • Use cfn-lint to catch syntax and structural problems.
  • Use cfn-guard to enforce security and governance rules.
  • Use aws cloudformation validate-template as an additional AWS-side validation.
Example GitHub Actions job (conceptual) to run these checks during PRs:
Automate checks early (in pre-commit hooks and PR pipelines). Don’t rely on a single check — combine cfn-lint, cfn-guard, and AWS validation to create a layered policy gate.

Operational best practices

  • Enable AWS Config to detect configuration drift and alert when running resources diverge from templates.
  • Use CloudFormation stack policies to protect critical resources (databases, encryption keys, S3 buckets) from accidental replacement or deletion.
  • Enforce tagging, security, and cost controls with cfn-guard rules so compliance is built into every change.
  • Add automated tests (unit tests for macros, integration tests for stacks where feasible) in your pipeline to validate runtime behavior.
  • Keep rule files and linter configuration under version control and review them via the same PR process as templates.
A slide titled "CloudFormation Best Practices" showing five colorful icon cards. Each card lists a recommendation (validate templates; enforce security with CloudFormation Guard; add automated CI/CD checks; enable AWS Config to detect drift; use stack policies to protect resources).
Make validation and policy checks part of pull-request and CI workflows so issues are caught early and only validated, policy-compliant templates proceed to deployment.
Further reading and references This lesson prepares you for the demo pipeline that integrates these tools into an automated CloudFormation deployment workflow.

Watch Video