CloudFormation and EC2 Template

Gaurav Singla
5 min readAug 17, 2020

--

With the help of AWS CloudFormation, users can describe and manage their AWS resources which help them in reducing time managing the resources. It uses a simple JSON or YAML script to write a template.

Users can also create templates with the help of cloudformation designers where users can just drag the resources from the toolbars. With the help of templates, users can create as many copies of the stack as they want, for example, a typical use case wherein users use cloudformation templates is to replicate their environment setup over multiple regions, these templates can come in handy to simply copy one environment to another rather than modeling another one. This will help users not only deploy the resources faster but also makes it error-free.

So with the help of a use case and solution, illustrating you a sample CLOUDFORMATION TEMPLATE :

USE CASE: “‘To launch an ec2 instance either in production or development environment, where the Instance type depends on environment, the Image ID depends on your AWS Region and mounting of new volume if production environment.”

Solution:-“HERE

Template Component explanation with Code Snippet from Solution:

1.Parameters

This is an important part of the template where we try to provide inputs to your CloudFormation template. These are heavily used where you want to give users a choice about any resource or if the templates are being reused for the same environment across the company. They also come in handy from keeping your template error-free.

Code Snippet:

Here we have defined parameters which we will be asked by the user to fill in while creating the stack from the template. The user will be asked about the Environment name (development/production), VPCID, SUBNETID, SecurityGroupID, which will then be referenced into the resources section.

2.Mapping

These are used to fix some variables in your template, for example, if you want to define fixed AMI but different for the production and test department, mappings come to the rescue.

Code Snippet:

Here we have tried to map the ImageId with the AWS Region the user will be working on also the Instance Type too is defined for both the Environments. All of which would be referenced to the EC2 Resources section.

3.Conditions

Conditions are used to control the creation of some resources depending on the conditions you set up, for example, if you want to create an S3 bucket only if the prod environment is selected else no bucket, Conditions can be used in these cases.

Code Snippet:

To include all the basic features of the template, we have also set a condition parameter named “CreateProdResources” which checks if the Environment is production or not by referencing the EnvironmentName variable in the parameters section.

4.Resources

This is the core member of your CloudFormation template. These represent all the different resources that the template creates or configures. With the help of the Properties section, we can configure our resource setup.

Code Snippet:

Here we are mainly configuring the EC2 resources and referencing all the properties from the above sections as and when required.

TIP: it’s hard to remember all the properties, just check out the documentation for the required fields.

Also, if the condition-“CreateProdResources” is matched we will be attaching a volume as specified in the use case, which is depicted by “NewVolume” referenced by “MountPoint”.

The “!GetAtt EC2Instance.AvailabilityZone” is used here to get the AZ you have used to configure your EC2 instance.

5.Outputs

These are used for Cross-stack references, where you can use some values from one stack into the other stack. They are also used to show some output text when you use the AWS Console or AWS CLI.

Code Snippet:

Here we are simply printing out the NewVolume id if the Environment is production.

6.Metadata

These are simply used to group your parameters while the user is creating the stack, which helps them to understand what the parameter is required for.

Code Snippet:

Here we have grouped all the Network Config fields and the EC2 config fields so that the user can distinguish and has a clear understanding of the template.

If you want to create a stack of this template, just copy it from github repo and paste the code in CloudFormation designer and BOOOOM….you are ready to go!!

References

I’ve referred to the below sources for writing this post:

FREE:- Yes, absolutely FREE to clap ( You know you want to 😂). Share it with your AWS enthusiasts friends, and stay tuned for further updates. Also, I am open to critics, suggestions, questions, and discussions.😄

“If you can imagine it, you can achieve it. If you can dream it, you can become it.”

-William Arthur Ward

--

--

Gaurav Singla

AWS Solutions Architect Associate|AWS Developer Associate| Devops-in progress