CloudFormation JSON to YAML Converter
Convert CloudFormation JSON to YAML using cfn-flip, our online converter, or AWS CLI tools. YAML is preferred for CloudFormation templates because it supports comments and shorthand intrinsic functions. This guide covers AWS JSON to YAML conversion methods, validation, and best practices for CloudFormation template management. See our YAML vs JSON comparison on why YAML is often chosen for IaC.
Method 1: Online Converter (Quickest)
The fastest way to convert CloudFormation JSON to YAML:
- Open converter: Go to our JSON to YAML tool
- Paste CloudFormation JSON: Enter your template
- Click Convert: Get YAML output instantly
- Download: Save as .yaml file
Method 2: cfn-flip CLI (Recommended)
cfn-flip is the most popular tool for CloudFormation conversion:
# Install cfn-flip
pip install cfn-flip
# Convert JSON to YAML
cfn-flip template.json > template.yaml
# Or use stdin/stdout
cat template.json | cfn-flip > template.yaml
# Convert with specific format
cfn-flip -y template.json # Force YAML output
cfn-flip -j template.yaml # Force JSON output
Example CloudFormation Conversion
Input (JSON):
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "S3 bucket for website hosting",
"Resources": {
"WebsiteBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": {"Fn::Sub": "${AWS::StackName}-website"},
"WebsiteConfiguration": {
"IndexDocument": "index.html",
"ErrorDocument": "error.html"
}
}
}
},
"Outputs": {
"BucketURL": {
"Value": {"Fn::GetAtt": ["WebsiteBucket", "WebsiteURL"]}
}
}
}
Output (YAML):
AWSTemplateFormatVersion: '2010-09-09'
Description: S3 bucket for website hosting
Resources:
WebsiteBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${AWS::StackName}-website
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
Outputs:
BucketURL:
Value: !GetAtt WebsiteBucket.WebsiteURL
Method 3: AWS Rain
AWS Rain is a modern CloudFormation CLI tool:
# Install rain
brew install rain # macOS
# or download from GitHub
# Format and convert template
rain fmt template.json > template.yaml
# Validate template
rain lint template.yaml
CloudFormation YAML to JSON
Convert CloudFormation YAML to JSON when needed:
# Using cfn-flip
cfn-flip -j template.yaml > template.json
# Using yq
yq -o json template.yaml > template.json
# Python one-liner
python -c "import yaml,json,sys; print(json.dumps(yaml.safe_load(open('template.yaml')), indent=2))"
Why Use YAML for CloudFormation?
| Feature | JSON | YAML |
|---|---|---|
| Comments | ❌ Not supported | ✅ Supported (#) |
| Intrinsic shorthand | Fn::Ref, Fn::Sub | ✅ !Ref, !Sub |
| Multi-line strings | Escape sequences | ✅ | and > syntax |
| Readability | Good | ✅ Excellent |
| File size | ✅ Smaller (minified) | Similar |
CloudFormation YAML Features
Shorthand Intrinsic Functions
# YAML shorthand (preferred)
BucketName: !Sub ${AWS::StackName}-data
RoleArn: !GetAtt MyRole.Arn
SecurityGroup: !Ref MySecurityGroup
AvailabilityZone: !Select [0, !GetAZs '']
# Equivalent JSON long form
# "BucketName": {"Fn::Sub": "${AWS::StackName}-data"}
# "RoleArn": {"Fn::GetAtt": ["MyRole", "Arn"]}
# "SecurityGroup": {"Ref": "MySecurityGroup"}
Comments for Documentation
# Production VPC Configuration
# Last updated: 2025-01-15
# Owner: Platform Team
Resources:
ProductionVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16 # /16 gives 65,536 IPs
Tags:
- Key: Environment
Value: Production # Change to Staging for test
Multi-line User Data
UserData:
Fn::Base64: |
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "Hello from $(hostname)" > /var/www/html/index.html
Frequently Asked Questions
How do I convert CloudFormation JSON to YAML?
Use cfn-flip CLI tool (pip install cfn-flip),
our online converter, or rain fmt command.
Example: cfn-flip template.json > template.yaml. All methods
preserve CloudFormation intrinsic functions correctly.
Should I use JSON or YAML for CloudFormation?
YAML is recommended for CloudFormation templates because it supports comments, is more readable, and allows shorthand syntax (!Ref instead of Fn::Ref). AWS fully supports both formats.
Does AWS CloudFormation support both JSON and YAML?
Yes, AWS CloudFormation fully supports both JSON and YAML template formats. You can use either format when creating stacks via Console, CLI, or API. The functionality is identical.
What is cfn-flip?
cfn-flip is a Python CLI tool specifically designed for converting CloudFormation templates between JSON and YAML. It correctly handles intrinsic functions, maintains formatting, and is widely used in the AWS community.
Need to convert CloudFormation templates?
Use our free online converter for instant JSON ↔ YAML conversion.
Open Converter Tool →