Convert Your CloudFormation template from JSON to YAML
Tags: aws, infrastructure, iac, cloudformation
Published: 2021-05-17 15:56:18

---
published: true
title: Convert Your CloudFormation template from JSON to YAML
description: Sometimes you have a JSON template you want to convert to YAML or vice versa, which is what I ran into today. AWS CloudFormation Template Flip is a command-line tool (and also a python library) you can use to convert a JSON template to YAML or the other way around.
tags: aws, infrastructure, iac, cloudformation
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asqunozwl80kcy02srft.png
canonical_url: https://jennapederson.com/blog/2021/5/17/flip-your-cloudformation-template-from-json-to-yaml/
---
 
Did you know that CloudFormation templates come in two flavors? YAML and JSON. When CloudFormation launched, JSON was the only format supported. YAML was introduced to CloudFormation in 2016. Both JSON and YAML are text and can be edited in any text editor. But more importantly, they can be managed in your version control system just like you do your application code.

## Why YAML?

JSON was designed for the serialization of data, not configuration. Comments aren't supported, which means you can't add important context to your infrastructure code.

YAML supports comments and it's human readable. Instead of curly braces and commas to represent hierarchy, it uses indentation. YAML takes less characters to represent the same configuration. All of this makes it easier to read and troubleshoot complex CloudFormation templates.

## What if you don't have YAML?

Sometimes you have a JSON template you want to convert to YAML or vice versa, which is what I ran into today. I found [this template](https://s3.amazonaws.com/cloudformation-templates-us-east-1/Rails_Multi_AZ.template) (from [the sample templates in the AWS docs](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/sample-templates-appframeworks-us-east-1.html)) to provision a highly available, scalable Ruby on Rails stack with a multi-AZ MySQL Amazon RDS database. I had no desire to read through the full JSON but I did want to understand the various parts of the template that make this work for this use case.

Enter [AWS CloudFormation Template Flip](https://github.com/awslabs/aws-cfn-template-flip).

AWS CloudFormation Template Flip is a command-line tool (and also a python library), you can use to convert a JSON template to YAML.

![Using cfn-flip to convert a JSON template to YAML](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikryshg74889hppw2mh1.gif)

From the command line:

```yaml
$ cfn-flip s3.json
```

You can convert this JSON template:

```json
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Example for creating an S3 Bucket",
    "Resources": {
        "MyBucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "BucketName": "my-bucket"
            }
        }
    }
}
```

To YAML:

```yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: Example for creating an S3 Bucket
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-bucket
```

## Flip Back to JSON

You can also use `cfn-flip` to convert from YAML back to JSON. Just remember that you'll lose any comments from the original YAML template when you do.

For instance, when converting this YAML template:

```yaml
AWSTemplateFormatVersion: 2010-09-09
Description: Example for creating an S3 Bucket

Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-bucket
      # Uncomment to enable bucket encryption
      # BucketEncryption:
      #   ServerSideEncryptionConfiguration:
      #     - ServerSideEncryptionByDefault:
      #         SSEAlgorithm: 'aws:kms'
```

To JSON:

```json
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Example for creating an S3 Bucket",
    "Resources": {
        "MyBucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "BucketName": "my-bucket"
            }
        }
    }
}
```

You'll lose the comments.

Ready to use `cfn-flip`? You can install it with `pip` or `brew` and find more info on how to use it [here](https://github.com/awslabs/aws-cfn-template-flip).

## Wrap Up

I hope this helps you the next time you have a CloudFormation template in a format that you can't or don't want to use. You can flip between JSON and YAML and back again with `cfn-flip` at the command line, just beware that when going from YAML to JSON you'll lose out on those important comments.

*Like what you read? Follow me here on [Dev.to](https://dev.to/jennapederson) or on [Twitter](https://twitter.com/jennapederson) to stay updated!*