How to Convert CloudFormation JSON to YAML and Vice Versa

How to Convert CloudFormation JSON to YAML and Vice Versa

How to Convert CloudFormation JSON to YAML and Vice Versa

You have a CloudFormation template in JSON and you want to convert it into YAML. You try searching for some JSON to YAML online converter and try it. But sad, it doesn’t quite work with CloudFormation specific syntax.

Sounds familiar?

I have faced this a lot while I was starting with AWS CloudFormation.

Although I understand, you can convert you JSON to YAML yourself if you have very good knowledge of JSON and YAML both.

But who has the time to spend on not so productive work like this.

Worry not, today I am going to tell you not one but two ways to convert CloudFormation JSON to YAML and YAML to JSON. So stay hooked till the end.

Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.

Convert CloudFormation JSON to YAML using AWS Console

AWS CloudFormation has a designer console to design templates for your use. If you have ever used it, you will know that you can use it to convert your CloudFormation template from JSON to YAML or YAML to JSON.

Let’s see how you can do it.

Login to the AWS Management Console and search CloudFormation in the search bar.

How to Convert CloudFormation JSON to YAML and Vice Versa 1

Once it appears as you can see on above screenshot, click on CloudFormation. This will lead you to CloudFormation console.

Click Create stack -> With new resources

How to Convert CloudFormation JSON to YAML and Vice Versa 2

Click Create template in Designer radio button as shown below.

How to Convert CloudFormation JSON to YAML and Vice Versa 3

And then, click on Create template in designer button again as shown in above screenshot. This will redirect you to designer console. The designer console looks like below-

How to Convert CloudFormation JSON to YAML and Vice Versa 4

Click on Template(bottom) and paste your JSON template. Make sure JSON radio button is selected(notice in above screenshot).

Now switch the radio button Choose template language to YAML by clicking on YAML radio button.

And voila, your template is converted to YAML and you see your success message like below-

Apart from the success meesge, you see your converted YAML as well in template section.

How to Convert CloudFormation JSON to YAML and Vice Versa 5

Copy the template text from here and you are good to go.

Here is the converted template for your reference-

AWSTemplateFormatVersion: 2010-09-09
Description: Template to create a sample queue
Parameters:
  StandardQueueName:
    Type: String
    Description: Standard Queue Name
    Default: DemoStandardQueue
  FifoQueueName:
    Type: String
    Description: Fifo Queue Name
    Default: DemoFifoQueue.fifo
Resources:
  MyStandardQueue:
    Type: 'AWS::SQS::Queue'
    Properties:
      QueueName: !Ref StandardQueueName
  MyFifoQueue:
    Type: 'AWS::SQS::Queue'
    Properties:
      QueueName: !Ref FifoQueueName
      FifoQueue: true
Outputs:
  StandardQueueURL:
    Description: Queue URL for standard queue
    Value: !Ref MyStandardQueue
  FifoQueueURL:
    Description: Queue URL for Fifo queue
    Value: !Ref MyFifoQueue
  StandardQueueArn:
    Description: Queue Arn for Standard queue
    Value: !GetAtt 
      - MyStandardQueue
      - Arn
  FifoQueueArn:
    Description: Queue Arn for FIFO queue
    Value: !GetAtt 
      - MyFifoQueue
      - Arn

Convert CloudFormation YAML to JSON using AWS Console

In the last section, you saw how I converted my JSON template to YAML. I feel by now, you know how to do YAML to JSON. We just have to do the opposite.

Paste your CloudFormation YAML template in the Template section while language is selected as YAML and click on JSON and your template will be converted to JSON.

Basically, this radio button as you would expect, works as flip. You can flip your template from YAML to JSON and Vice versa. Just paste your template in one and choose the other radio button and you are done- Easy peasy 🙂

Convert CloudFormation JSON to YAML and YAML to JSON using CFN Flip

Now let me tell you about this cool online tool which does the same flip functionality for your CloudFormation template. And the real magic is you don’t need to be logged into the AWS console at all unlike CloudFormation designer.

Let’s see how this works.

go to https://cfnflip.com/

This is how It looks like at the time of writing this tutorial

Now paste your template there- please note that you can simply paste your JSON or YAML template and flipping will flip it.

How to Convert CloudFormation JSON to YAML and Vice Versa 6

Click on the Flip button from the top left corner as shown in the above screenshot.

And there you go, your pretty YAML is ready to use.

Converted Template for your reference-

AWSTemplateFormatVersion: '2010-09-09'
Description: Template to create a sample queue
Parameters:
  StandardQueueName:
    Type: String
    Description: Standard Queue Name
    Default: DemoStandardQueue
  FifoQueueName:
    Type: String
    Description: Fifo Queue Name
    Default: DemoFifoQueue.fifo
Resources:
  MyStandardQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Ref 'StandardQueueName'
  MyFifoQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Ref 'FifoQueueName'
      FifoQueue: true
Outputs:
  StandardQueueURL:
    Description: Queue URL for standard queue
    Value: !Ref 'MyStandardQueue'
  FifoQueueURL:
    Description: Queue URL for Fifo queue
    Value: !Ref 'MyFifoQueue'
  StandardQueueArn:
    Description: Queue Arn for Standard queue
    Value: !GetAtt 'MyStandardQueue.Arn'
  FifoQueueArn:
    Description: Queue Arn for FIFO queue
    Value: !GetAtt 'MyFifoQueue.Arn'

Don’t forget to bookmark this as it really saves a lot of time in converting the CloudFormation template from JSON to YAML or the other way around.

Conclusion

In this post, we learnt how to convert CloudFormation JSON to YAML and vice versa. We also leanrt that although there are many online JSON to YAML or YAML to JSON converters. But, they don’t always work with CloudFormation intrinsic function or specific syntax of CloudFormation.

Those tools will although convert your template but the resulting template is invalid many times and doesn’t work.

However, the two ways we have seen in this tutorial are sure shot way to convert your CloudFormation template from JSON to YAM and vice versa accurately.

Hope the post was useful to you. Feel free to leave a comment or provide feedback.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

Suggested Read:

Leave a Reply

Your email address will not be published. Required fields are marked *