Rushan Arunod

How to create an AWS environment with CloudFormation

Amazon Web Services (AWS) CloudFormation is an infrastructure Management Tool to simplify managing infrastructure. This article will help you understand the importance of AWS CloudFormation and also the magic inside it.


Amazon Web Services (AWS) CloudFormation is an infrastructure Management Tool to simplify managing infrastructure. This article will help you understand the importance of AWS CloudFormation and also the magic inside it. By the end of this article you will be learning what is AWS CloudFormation and how to create CloudFormation stacks to build your development, UAT or Production environments without wasting valuable time.

What can be achieved

CloudFormation is an AWS infrastructure management tool which simplifies managing your infrastructure. I would say AWS CloudFormation is one of the most impressive AWS services that can be used to build other AWS services – by way of a simple flow. This helps developers save time managing environments and more time spent developing their applications.

Aside from saving time, other advantages of CloudFormation are as follows:

  • Consistency – ability to provision resources in a safe and repeatable manner
  • Infrastructure as Code – codifying infrastructure allows you to treat your infrastructure as a code
  • Automatic Rollback – capable of Rolling back changes to a previous error-free version automatically (if errors are detected in new changes)
  • Graphical View – easy to work with because it has a graphical view of the environment
  • Change log – maintains detailed records of infrastructure changes
  • Rapid replication – able to replicate infrastructure within a short time period across multiple regions, or even replicate to a totally different AWS account
  • Update stacks – easily edit your environments that are created using Cloudformation
  • Eliminate stacks – terminate and eliminate whole infrastructures stacks at once when needed
  • No additional charge – AWS CloudFormation is available at no additional charge
Templates And Stacks

A template is a text file which includes details of services in it. This is normally written in JSON or a YAML format. This is the file where everything that is in your environment is defined. For example, the instance types: AMI IDs, key pair names, attached volume size, database usernames, security groups and so on.

Stacks are collections of resources. Templates are used to create stacks. You can easily create, update or delete a stack. If you write your own CloudFormation template you will be able to create multiple stacks using that template within any AWS console.

A template file contains various properties such as resources, mappings, parameters and so on. But resources are mandatory in the template. I will describe about some of the resources, mappings and parameters inside a sample template file. These properties are widely used inside CloudFormation scripts. Below is the structure of a CloudFormation template.

(Code snippet 1 : Sample template file)

Creating an environment

Now that you have a basic idea about templates and stacks, let’s get started right away by using a simple yet, common scenario environment. Assume that we need to create an environment with the following resources.

EC2 Machine – used to host client and server applications
S3 bucket – used to store media files
Elastic Ip – for the EC2 instance

(Figure 1: EC2 Instance, Elastic IP and S3 storage bucket)


The above mentioned three resources(EC2, EIP, S3) will be listed as a member under ‘resources’ in the CloudFormation template. For the purpose of this guide, we will create – resource by resource – in order to understand the script creation and stack updating.

Let’s start creating the EC2 instance.

Creating the EC2 Instance

We will be using the following template to create the EC2 Instance that we require.

Change the InstanceType, ImageId and the SubnetId accordingly.

Code snippet 2: Change Instance Type, ImageID and SubnetId

There are two main ways to create a CloudFormation stack using a template. One method is that you can upload or select a template file in the stack creation wizard.

The second method is to use the CloudFormation designer. Designer is a great tool to use because it provides us with a graphical view of our CloudFormation template. You can add, edit or delete resources by simply dragging and dropping.

Either way, start by creating the stack. While creating the stack you can see the logs in the CloudFormation event tab (as below figure 2)

(Figure 2 : Event log in CloudFormation events tab)

As the next step, we will be creating an Elastic Ip and assigning it to the instance that we just created using CloudFormation.

Creating an Elastic IP (EIP)

We now have a CloudFormation stack using the template we just created.

We are going to create an Elastic Ip for the instance in the environment, and in order to do that, we are going to use the update functionality of AWS CloudFormation.

Updated stacks allow users to avoid recreation of resources repeatedly, and thus saves valuable time. Note that it will not recreate unchanged resources.

(Figure 3 : Updating stacks in AWS CloudFormation)

To create an Elastic IP, we need to use “Type”: “AWS::EC2::EIP” in our template.
Every resource in the script must have this “Type” parameter to identify which type of resource we need to provide. And using “AWS::EC2::EIPAssociation” we can associate our newly created EIP to our EC2 machine.

When updating we can see the graphical view of our infrastructure in the CloudFormation designer console. Below is the designer view of our template.

(Figure 4: Graphical view of infrastructure in the CloudFormation designer console)

Below is our updated CloudFormation script with EIP and EIP Association resources.

(Code snippet 3: updated CloudFormation script with EIP and EIP Association resources)

Note how we get the InstanceId and EIP AllocationId to associate with each other.
  • "InstanceId": {
    "Ref": "EC2Machine"
  • "AllocationId": {
    "Fn::GetAtt": ["EIP",

We use Ref attribute to get the reference of a resource. Fn::GetAtt is used to get an attribute inside a resource.

See the event logs to verify that the user stack has updated successfully. After that you will see that the newly created instance has an EIP associated to it (see figure 5 below).

(Figure 5: Resource associated with an EIP)


Creating an S3 bucket

Now you know that we can update our CloudFormation template even after having created the environment.

So, let’s update our script to add a new S3 bucket. Let’s assume that we are simply going to create a new S3 bucket. For the sake of simplicity, we will not be adding bucket policies now. However, we recommend reading about giving permissions to buckets with bucket policies.

When creating the stack we can also get user inputs. Let’s ask users to insert the bucket name when we update the stack. For that, we use a template attribute named “Parameters”. Parameters are used to get user input to the stacks. We can even make use of a drop-down list of values that are accepted within a parameter.

There is one last thing remaining to do in this section.

There is a way to manage variable values inside the template itself. This is known as “Mappings”.

In our CloudFormation script we set the ‘AMI id’ for the EC2 instance inside the EC2 resource. But we can give it as a Mapping attribute and then when required, we can change only the value if needed.

Below is the template with all the changes that we discussed here.

Note how Mappings are used inside the EC2 resource and the BucketNames used inside the S3 resource.

(Code Snippet 4: Completed Cloud Formation template)


Now you know how to write a simple CloudFormation Script and now you are also familiar with the fields inside the script and usages of those attributes.

You may use this as a base template for your own scripts and continue adding more resources. AWS documents are the best resources to follow and you can also find lots of examples there.

You should now be able to create your own CloudFormation script and organise your AWS resources. This will definitely make life a whole lot easier.

Thank you for reading this Tech Guide. We hope you will also read our next tutorial so that we can help you solve some more interesting problems.

Rushan Arunod

Software Engineer | Mitra Innovation