
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
- 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.

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

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:- Source stage (CodeCommit, GitHub, Bitbucket, etc.)
- Build stage (CodeBuild) that runs:
aws cloudformation validate-templatefor quick server-side validationcfn-lintfor local syntactic and property checkscfn-guardto validate policies and organizational rules
- If all checks pass, the pipeline continues to deploy via CloudFormation
- 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.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-templateas a quick server-side check. - Run
cfn-lintfor 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
- cfn-lint GitHub
- CloudFormation Guard GitHub
- AWS CloudFormation validate-template (CLI)
- AWS CodeBuild Documentation
- AWS Config Overview