How to convert CloudFormation JSON to YAML

September 1, 2019

In this article, we are going to take an AWS CloudFormation file written in JSON and then convert that file to YAML format.

Luckily, AWS has a way to do this for us which is super easy.

Steps

Login to your AWS account

Navigate to AWS CloudFormation

Select “Design Template”

Click “Template” at the bottom

Paste in the following CloudFormation template

{
    "AWSTemplateFormatVersion": "2010-09-09", 
    "Description": "Example Stack, version 2.2.2.0", 
    "Parameters": {
        "Stages": {
            "Type": "String", 
            "AllowedValues": [
                "dev", 
                "test",
                "prod",
                "qa"
            ], 
            "Default": "dev", 
            "Description": "Multi-stage support"
        }
    }, 
    "Mappings": {
        "TrueFalse": {
            "Yes": {
                "Value": "True"
            }, 
            "No": {
                "Value": "False"
            }
        }
    },
    "Resources": {
        "MyPolicy": {
            "Type": "AWS::IAM::Policy", 
            "Properties": {
                "PolicyName": "MyPolicy", 
                "Roles": [
                    {
                        "Ref": "MyRole"
                    }
                ], 
                "PolicyDocument": {
                    "Version": "2012-10-17", 
                    "Statement": [
                        {
                            "Effect": "Allow", 
                            "Action": [
                                "logs:CreateLogGroup", 
                                "logs:CreateLogStream", 
                                "logs:PutLogEvents"
                            ], 
                            "Resource": "*"
                        }, 
                        {
                            "Effect": "Allow", 
                            "Action": [
                                "ec2:*"
                            ], 
                            "Resource": [
                                "*"
                            ]
                        },
                        {
                            "Effect": "Allow", 
                            "Action": [
                                "dynamodb:DeleteItem", 
                                "dynamodb:GetItem", 
                                "dynamodb:PutItem", 
                                "dynamodb:Query", 
                                "dynamodb:Scan", 
                                "dynamodb:BatchWriteItem"
                            ], 
                            "Resource": [
                                {
                                    "Fn::Join": [
                                        "", 
                                        [
                                            {
                                                "Fn::Join": [
                                                    ":", 
                                                    [
                                                        "arn:aws:dynamodb", 
                                                        {
                                                            "Ref": "AWS::Region"
                                                        }, 
                                                        {
                                                            "Ref": "AWS::AccountId"
                                                        }, 
                                                        "table/"
                                                    ]
                                                ]
                                            }, 
                                            {
                                                "Ref": "MyTable"
                                            }
                                        ]
                                    ]
                                }
                            ]
                        }
                    ]
                }
            }
        }, 
        "MyRole": {
            "Type": "AWS::IAM::Role", 
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17", 
                    "Statement": [
                        {
                            "Effect": "Allow", 
                            "Principal": {
                                "Service": "ec2.amazonaws.com"
                            }, 
                            "Action": "sts:AssumeRole"
                        }
                    ]
                }, 
                "Path": "/"
            }
        },
        "MyTable": {
            "Type": "AWS::DynamoDB::Table", 
            "Properties": {
                "AttributeDefinitions": [
                    {
                        "AttributeName": "id", 
                        "AttributeType": "S"
                    }
                ], 
                "KeySchema": [
                    {
                        "AttributeName": "id", 
                        "KeyType": "HASH"
                    }
                ], 
                "ProvisionedThroughput": {
                    "ReadCapacityUnits": "1", 
                    "WriteCapacityUnits": "1"
                }
            }
        }
    }, 
    "Outputs": {
        "AccountId": {
            "Value": {
                "Ref": "AWS::AccountId"
            }, 
            "Description": "AWS Account Id"
        }, 
        "MyTableName": {
            "Value": {
                "Ref": "MyTable"
            }, 
            "Description": "Name of the DynomoDB Table",
            "Export": {
                "Name": {
                    "Fn::Join": [
                        ":",
                        [
                            {
                                "Ref": "AWS::StackName"
                            },
                            "MyTableName"
                        ]
                    ]
                }
            }
        },
        "MyTableArn": {
            "Value": {
                "Fn::GetAtt": [
                    "MyTable",
                    "Arn"
                ]
            }, 
            "Description": "Arn of the DynomoDB Table",
            "Export": {
                "Name": {
                    "Fn::Join": [
                        ":",
                        [
                            {
                                "Ref": "AWS::StackName"
                            },
                            "MyTableArn"
                        ]
                    ]
                }
            }
        }
    }
}

Select “YAML”

Boom! Now copy the converted YAML and fly away into the sunset 🌄 👋

AWSTemplateFormatVersion: 2010-09-09
Description: 'Example stack, version 2.2.2.0'
Parameters:
  Stages:
    Type: String
    AllowedValues:
      - dev
      - test
      - prod
      - qa
    Default: dev
    Description: Multi-stage support
Mappings:
  TrueFalse:
    'Yes':
      Value: 'True'
    'No':
      Value: 'False'
Resources:
  MyPolicy:
    Type: 'AWS::IAM::Policy'
    Properties:
      PolicyName: MyPolicy
      Roles:
        - !Ref MyRole
      PolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Action:
              - 'logs:CreateLogGroup'
              - 'logs:CreateLogStream'
              - 'logs:PutLogEvents'
            Resource: '*'
          - Effect: Allow
            Action:
              - 'ec2:*'
            Resource:
              - '*'
          - Effect: Allow
            Action:
              - 'dynamodb:DeleteItem'
              - 'dynamodb:GetItem'
              - 'dynamodb:PutItem'
              - 'dynamodb:Query'
              - 'dynamodb:Scan'
              - 'dynamodb:BatchWriteItem'
            Resource:
              - !Join 
                - ''
                - - !Join 
                    - ':'
                    - - 'arn:aws:dynamodb'
                      - !Ref 'AWS::Region'
                      - !Ref 'AWS::AccountId'
                      - table/
                  - !Ref MyTable
  MyRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service: ec2.amazonaws.com
            Action: 'sts:AssumeRole'
      Path: /
  MyTable:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '1'
        WriteCapacityUnits: '1'
Outputs:
  AccountId:
    Value: !Ref 'AWS::AccountId'
    Description: AWS Account Id
  MyTableName:
    Value: !Ref MyTable
    Description: Name of the DynomoDB Table
    Export:
      Name: !Join 
        - ':'
        - - !Ref 'AWS::StackName'
          - MyTableName
  MyTableArn:
    Value: !GetAtt 
      - MyTable
      - Arn
    Description: Arn of the DynomoDB Table
    Export:
      Name: !Join 
        - ':'
        - - !Ref 'AWS::StackName'
          - MyTableArn

Now doesn’t that look much cleaner? The final recommendation, don’t upload AWS Cloudformation directly through the AWS console, use a deployment framework such as the Serverless Framework to make your IAC (Infrastructure as Code) even cleaner.

Serverless Handbook
Access free book

The dream team

At Serverless Guru, we're a collective of proactive solution finders. We prioritize genuineness, forward-thinking vision, and above all, we commit to diligently serving our members each and every day.

See open positions

Looking for skilled architects & developers?

Join businesses around the globe that trust our services. Let's start your serverless journey. Get in touch today!
Ryan Jones - Founder
Ryan Jones
Founder
Speak to a Guru
arrow
Edu Marcos
Chief Technology Officer
Speak to a Guru
arrow
Mason Toberny
Mason Toberny
Head of Enterprise Accounts
Speak to a Guru
arrow

Join the Community

Gather, share, and learn about AWS and serverless with enthusiasts worldwide in our open and free community.