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.
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
It is very convenient , once you created you only quite literally need to just deploy it on your preferred place through AWS Cloud Formation.
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
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 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
Description: A sample template
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.
Description: 'AWS CloudFormation Sample Template'
Description: Example 'dev' or 'test' switch, determines which settings are passed to userdata.
Description: Enter a valid AMI ID for your selected region, suggested to use latest Amazon Linux.
Description: Enter a valid instance type for your selected region.
- Ref: EnvNameParam
- Ref: InstanceSecurityGroup
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.
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.
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.