Understanding AWS Cloud Formation

Harivarshan
5 min readJul 14, 2021

It’s truly an over whelming feeling when you suddenly have to work on AWS Cloud Formation and you will be confused as to what is going on. It is understandable because when we first start to understand CFT’s it could be really confusing.

So lets move on to the important question of

What is CFT?

The definition as given from Amazon is

AWS CloudFormation is a service that helps you model and set up your Amazon Web Services resources so that you can spend less time managing those resources and more time focusing on your applications that run in AWS.

To put things in perspective AWS CFT is literally coding the infrastructure you create for your projects. You heard me , yes, coding. To give more depth , we can say that whatever you do with the console you can do with the code.

The CFTs can be created in two different ways. One is through the AWS CFT visualizer which is available in the Management Console itself. The second one is through code which has to be formatted in a certain way and can be written in either .yaml or .json format.

Image of AWS CFT Visualizer

Why CFT?

You might ask that,

but i love creating my EC2 and my ECS clusters

and it is a fair argument. Doing things through the AWS console provide us a swift experience. The key issues arises when we need to do things repeatedly and to replicate else where it can be only done through the same person without messing and it can cause quite a hassle when describing what resources to create and etc…

So the solution lies in CFT, where you can quite literally code your infra and you can run it anywhere and it would provide the same results.(Given that you would need to change quite a few things.)

The biggest few things that I would say that the biggest three pros ‘ would be

Convenience

It is very convenient , once you created you only quite literally need to just deploy it on your preferred place through AWS Cloud Formation.

Replication

It is the easiest way to replicate a infra as much as you want and it can save a huge amount of time.

Ease of use

Easy Vectors by Vecteezy

The biggest yet underrated thing is the ease of use of CFT. When you decide that you no longer need the infra that you were using you can just delete the stack and that would rewind any change and resource created by the script with just a click.

Making changes

Change Vectors by Vecteezy

Making changes has been never easy, lets say we forgot to use the correct instance AMI and in normal cases we might need to manually recreate everything up to the point but with CFT we can just edit that line and run it and it would do everything.

and many more

By now i believe you might start to understand the power of CFT. These aren’t the only usages and there are many more usages.

Creating your first CFT

I guess enough talk. Lets create our first CFT. To get started you can quite literally use any IDE you want and there are plugins that can help you in the creation of it but i’m not going to go through those.

Step 1 : Create an empty .yaml file

You can copy paste what is below and or download any sample template and work on it and there is no need to have a plugin or anything specifically to do.

Step 2 : Get a sample Template

Below we have a very simple , CFT which would create an EC2 in a few seconds. To explain further

AWSTemplateFormatVersion: "2010-09-09"
Description: A sample template
Resources:
MyEC2Instance:
Type: "AWS::EC2::Instance"
Properties:
ImageId: "ami-0ff8a91507f77f867"
InstanceType: t2.micro
KeyName: testkey
BlockDeviceMappings:
-
DeviceName: /dev/sdm
Ebs:
VolumeType: io1
Iops: 200
DeleteOnTermination: false
VolumeSize: 20

AWSTemplateFormatVersion is the format version you need to use and you can pretty much use the same version and there won’t be any cases for you to change it most of the time.

The next is the Description , it is self-explanatory and it is literally a description for your infra you create and you can write what you desire.

The main few sections would be Resources, Parameters and Mappings. We only have Resources section above , we till take a deep look at the three in next section.

AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS CloudFormation Sample Template'
Parameters:
EnvNameParam:
Description: Example 'dev' or 'test' switch, determines which settings are passed to userdata.
Type: String
Default: dev
AllowedValues:
- dev
- test
AMIidParam:
Description: Enter a valid AMI ID for your selected region, suggested to use latest Amazon Linux.
Type: AWS::EC2::Image::Id
Default: ami-5ec1673e
InstanceTypeParam:
Description: Enter a valid instance type for your selected region.
Type: String
Default: 't2.micro'
Mappings:
EnvMap:
dev:
UserToCreate: devuser
WebServer: httpd
test:
UserToCreate: testuser
WebServer: nginx
Conditions:
ExampleConditionIfDev:
Fn::Equals:
- Ref: EnvNameParam
- 'dev'
Resources:
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: FindInMap-Inside-Sub-Example
EC2Instance:
Type: 'AWS::EC2::Instance'
Properties:
ImageId:
Ref: AMIidParam
InstanceType:
Ref: InstanceTypeParam
SecurityGroupIds:
- Ref: InstanceSecurityGroup
UserData:
Fn::Base64:
Fn::Sub:

Mappings

The optional Mappings section matches a key to a corresponding set of named values. For example, if you want to set values based on a region, you can create a mapping that uses the region name as a key and contains the values you want to specify for each specific region. You use the Fn::FindInMap intrinsic function to retrieve values in a map.

Parameters

This section is where you define your custom variables needed for resources. Use the optional Parameters section to customize your templates. Parameters enable you to input custom values to your template each time you create or update a stack.

Resources

This is the section you need to add the resources you want to create. Any and all resources should be included here.

Now the biggest question you have i understand a bit of it but there is a lot of properties i don't understand.

I have few steps to rectify that, we can simply go to the AWS CFT documentation and search for the resource you need to create and then we can view all the properties and explanation regarding them.

DISCLAIMER : ALWAYS , ALWAYS add the resources to the CFTs one by one because it can be quite difficult when you have included all the resources in place to debug if you have any issues because at the end of the day you have to remember we are still making infrastructures through coding so there is bound to be bugs.

--

--