Chapter 29 · AWS Cloudformation
Subchapter 29.7
references/validate-cloudformation-template.script.mdMarkdown6 KBView on GitHub
Deterministic procedure for validating a CloudFormation template’s syntax, schema, and resource properties using cfn-lint. Works via the cfn-lint CLI or Python API.
["us-east-1", "eu-west-1"]). Defaults to cfn-lint’s default region if omitted.["W2001", "E3012"]).Constraints for parameter acquisition:
Check which validation mechanism is available.
Constraints:
cfn-lint CLI available on the user’s system (verify with which cfn-lint or cfn-lint --version)cfnlint library (verify by attempting import cfnlint in a throwaway Python command)cfn-lint via pip install cfn-lint. Do you want me to install it, or would you prefer to install it manually?”Obtain the CloudFormation template from the user.
Constraints:
Execute cfn-lint against the template using the best available mechanism.
Constraints:
cfn-lint CLI is available, You MUST invoke it on the template file with appropriate flags:
--regions us-east-1 eu-west-1--ignore-checks W2001 E3012--format json for structured outputcfn-lint --format json --regions us-east-1 template.yamlcfnlint library is available, You MUST invoke cfnlint.api.lint(s=template_content, config={"regions": [...], "ignore_checks": [...]})Report validation findings to the user.
Constraints:
MyBucket errors together)Guide the user on what to do after validation.
Constraints:
check-cloudformation-template-compliance SOP to check security and compliancecloudformation-pre-deploy-validation SOP for final pre-deployment readinessAWSTemplateFormatVersion: '2010-09-09'
Resources:
MyFunction:
Type: AWS::Lambda::Function
Properties:
FunctionNam: my-function
Runtime: python3.9
Handler: index.handler
Role: arn:aws:iam::123456789012:role/my-function-role
Code:
ZipFile: |
def handler(event, context):
return {'statusCode': 200}Your template has 1 error, 0 warnings, 0 info messages.
**MyFunction (AWS::Lambda::Function):**
- E3002 at line 6: Invalid Property Resources/MyFunction/Properties/FunctionNam
Fix (line 6):
FunctionName: my-function # Typo: FunctionNam → FunctionNameIf the tool or CLI returns a parsing error, the template has invalid YAML or JSON syntax. Check for indentation issues, missing colons, or unquoted special characters. Fix the syntax and re-run validation.
If cfn-lint reports errors you believe are incorrect, suppress specific rules using ignore_checks. Verify the rule ID from the output (e.g., W2001) and pass it in the parameter.
Some resource properties are only valid in certain regions. If you see region-related errors, pass the target deployment region in the regions parameter to get accurate validation.
Install with pip install cfn-lint. The tool is maintained at https://github.com/aws-cloudformation/cfn-lint (opens in a new tab).