Converting a CloudFormation Template from JSON to YAML

  • September 26, 2016

In our blog last week we told you that AWS CloudFormation has grown its support beyond JSON to include YAML. Prior to the announcement, our AWS consultants had been writing in YAML and used an in-house YAML CloudFormation generator to help us avoid the typical pain points associated with JSON. We promised in that article to share with you instructions on how to convert existing JSON CloudFormation templates into YAML and are delivering on that promise today.


Our AWS CloudFormation experts have assembled the following single standard CloudFormation JSON to YAML converter.

import yaml
import json
import argparse
import sys
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--json', help='Input file (JSON)', required=True)
parser.add_argument('--yaml', help='Output file (YAML)', required=True)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
json_file = args.json
yaml_file = args.yaml
with open(json_file) as fp:
json = json.load(fp)
with open(yaml_file, 'w') as yaml_fp:
yaml.safe_dump(json, yaml_fp, allow_unicode=True, default_flow_style=False)
print "Created YAML file {0}".format(yaml_file)
view raw converter.py hosted with ❤ by GitHub

Subscribe to our blog

ribbon-logo-dark

Related Blog Posts