Working with AWS CloudFormation

Agnel Nandapurapu
Tensult Blogs
Published in
3 min readJun 6, 2019
Ref: https://bit.ly/2QOblTT

In AWS, configuration and provisioning of any resource should be done either through CLI or the console. So when using these interfaces, you have to follow multiple steps to configure resources/architecture. For fewer resources, it won’t be hard to configure, but in the case of large scale deployments, it will be tough to handle the configurations. In this case, CloudFormation comes into the picture. Cloud formation is an AWS service to configure resources using a template. The template is used to define all required configurations to setup architecture in one go.

What is Template?

It is nothing but a JSON/YAML file. Attributes of the template should be defined as per CloudFormation documentation. CloudFormation documentation clearly explains how to configure resources and attributes in templates. Here I’m mainly discussing the important attributes of the Cloud Formation template. Please refer to this link for CloudFormation documentation.

Main Attributes of Cloudformation Template :

  • Parameters: this attribute is called as inputs to the template, parameters will be referred in many places inside the template.
  • Resources: this attribute is very crucial, this is used to configure AWS resources, without this, we can’t configure any resource in the Cloudformation template.
  • Outputs: Outputs are used to get the values after the execution of the template.
  • Mappings: Mappings are used as pre-define a JSON body which matches a key to a corresponding set of named values.
  • Conditions: these are the statements used to perform certain actions when the statement is true.

I gave a brief explanation of the main attributes in the CloudFormation template. Now I am going to explain the functionalities of the template to create an AWS ec2 instance.

While executing the above template, firstly it asks values for parameters. We have only one parameter and that is “EnvType” as I mentioned in allowed values, so we should select either “prod” or “test”.

At Conditions attribute, based on given EnvType we are setting CreateProdResources value, if EnvType is “prod” then CreateProdResources value will be “true” here otherwise the value will be “false”.

If we check MountPoint and NewVolume Resources, they are depending value of CreateProdResources.

  • If CreateProdResources is true then MountPoint and NewVolume will be configured.
  • If CreateProdResources is false then MountPoint and NewVolume will not be configured.

The above template has three resources, namely, Ec2Instance, MountPoint, and NewVolume.

If we see elements in Ec2Instance resource, Type is used to define the type of AWS resource, so here we are creating one Ec2 instance so Type is AWS::EC2:: Instance.

Note: cloud formation defined a specific resource type for each resource.

Note: AWS:: Region returns cloud formation template running region.Supppose template is running in us-east-1 region then AWS:: Region is us-east-1.

If we see ImageId property, it is fetching the value from the RegionMap object in the Mappings attribute. It checks keys in RegionMap by AWS:: Region value. So ImageId gets the values from RegionMap object in Mapping with associated AMIs to the regions.

If we see MountPoint, it is referring Ec2Instace for getting the value of InstanceId and referring NewVolume for getting volume.

Also, if we see a new volume, it is referring Ec2Instace for getting value AvailabilityZone.

Note: If one resource is depending on another one in the template, in the execution order of template, independent resources run first then dependent resources run.

So first Ec2Instance will be configured, after NewVolume and MountPoint will be configured.

Outputs give values after all resources are configured in the template, the volume gets the value after NewVolume resource creation and if CreateProdResources is true.

Conclusion

In the template we used several intrinsic functions to fetch values from other elements, they are Fn:: GetAtt, Fn:: Equals, Fn:: FindInMap. These are Cloud Formation predefined functions that we can use as per our requirement. Please read this doc for more functions.

Here, I explained only the functionalities inside the template, but I didn’t show how to execute through the AWS console, so please refer to this link to understand the execution steps of the cloud formation template.

This Blog has been moved from Medium to blogs.tensult.com. All the latest content will be available there. Subscribe to our newsletter to stay updated.

--

--