Validate a CloudFormation Template
AWS CloudFormation templates define cloud infrastructure as code and must be valid YAML before deployment. This example provisions an S3 bucket with versioning and a lifecycle policy. CloudFormation returns cryptic errors for invalid YAML and does not show which line failed. Validate and format the template before uploading to the AWS console or running aws cloudformation deploy.
Example
AWSTemplateFormatVersion: '2010-09-09'
Description: S3 bucket with versioning
Parameters:
BucketName:
Type: String
Default: my-app-bucket
Resources:
AppBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
VersioningConfiguration:
Status: Enabled
LifecycleConfiguration:
Rules:
- Id: DeleteOldVersions
Status: Enabled
NoncurrentVersionExpirationInDays: 30
Outputs:
BucketArn:
Value: !GetAtt AppBucket.ArnFAQ
- What is the difference between Parameters and Outputs?
- Parameters accept input values when you deploy the stack, letting you reuse the same template for different environments. Outputs export values from the stack that other stacks or scripts can reference.
- What does !Ref do in CloudFormation?
- !Ref is a CloudFormation intrinsic function that returns the value of a Parameter or the physical ID of a Resource, letting you pass values between resource definitions.
- Can I use JSON instead of YAML for CloudFormation?
- Yes. CloudFormation accepts both formats. YAML is preferred because it supports comments and is easier to read, but both produce identical stacks.
Related Examples
Validate a Kubernetes Pod Manifest
Kubernetes manifests are complex YAML documents where a formatting error causes ...
Validate an Ansible PlaybookAnsible playbooks are YAML files that define automation tasks across servers. In...
Format a Helm Values FileHelm values files configure Kubernetes charts and are injected into templates as...