AWS CloudFormation Templates – YAML or JSON

AWS CloudFormation Templates – YAML or JSON

November 25, 2020 / Nirav Shah

AWS CloudFormation Templates – YAML OR JSON

Intrinsic Functions in CloudFormation provide assistance in assigning values to template properties that aren’t available until runtime.

Confused?

Let us take a simple example of FindMap:

1. FindMap using YAML

As we already know that using FindinMap we can determine which AMI to use for which region. So FindInMap is the intrinsic function that we’re going to use here in our example:

   Mappings:
    RegionsMap:
   Us-east-1:
       HVM64: “ami-0dsfsf……”
       HVMG2: “ami-0a5fger…..”
   Eu-west-1:
      HVM64: “ami-45vdwd….”
      HVMG2: “ami-31c2fe...”

So in our example we have mappings header, and then we have the RegionMap, which is the name of our mapping, and under we list all of the regions, and then the HVM64 or the HVMG2 AMI ID Like

HVM64: “ami-offa91……”

Template:

   Resources:
      my EC2 instance:
      Type : “AWS::EC2::instance”
   properties :
      imageId : !FIndINMap
      -RegionMap
      -!Ref ‘AWS::Region’
      -HVM64
   InstanceType:m1.small

Now we can define resources, properties and imageID, but if you check it again in imageID you will see the exclamation mark FindInMap function call and we specify the call to the RegionMap mapping, and then we specify the Ref! and then region, and then we specify that we want the HVM64 result.

2. Find Map using JSON example

Find Map using JSON example code

It’s similar to YAML, but you can see more brackets and colons. And this is how we run the function. There are some square brackets and then define that we’re looking for RegionMap, and then the Ref function on the AWS Region.

Now let’s look at “What is Nesting in CloudFormation?”.

So stack contains resources and resources is like s3 bucket, ec2 instance or many AWS services. With nesting, a whole other stack can be a resource.

So why would we do this?

Well, it allows a huge set of infrastructure to be split over multiple templates. As we know there are some limitations of CloudFormation stacks like in CloudFormation stacks we allow only 200 resources, 60 outputs, or 60 parameters.

You can overcome these limits by using nested stacks. Read More About How to work with nested stacks and Class list of AWS EC2.

Conclusion

We learned about CloudFormation’s different format in YAML/JSON and also saw what are nested stacks, why they are important and how are they implemented.

Also Read : EC2: Manage Security Groups with help of JSON policy

FAQs:

1. Should I use YAML or JSON with CloudFormation?

2. What format is used for AWS CloudFormation templates?

3. Which is faster YAML or JSON?

Talk to AWS Certified Consultant

    Spread Love By Sharing:

    Let Us Talk About Your AWS Development Requirements

    Have queries about your AWS project ideas and concepts? Please drop in your project details to discuss with our AWS experts, professionals and consultants.

    • Swift Hiring and Onboarding
    • Experienced and Trained AWS Team
    • Quality Consulting and Programming
    Let’s Connect and Discuss Your Project