Streamline Your AWS Infrastructure: An Introduction to CloudFormation.

Sarumathy P
featurepreneur
Published in
5 min readMar 27, 2023

AWS CloudFormation is a tool that helps you provision and manages resources on Amazon Web Services (AWS) in a declarative way.

In simple terms, when you want to create a new AWS infrastructure for your application or modify an existing one, you need to specify what resources you want to create, their configurations, and their relationships with each other. Doing this manually can be time-consuming and error-prone, especially if you have a complex infrastructure.

AWS CloudFormation allows you to automate this process by defining your infrastructure as code. You can use a template, which is a JSON or YAML file, to describe your resources and their configurations. CloudFormation then takes care of provisioning and configuring these resources for you, in the right order, and in a reliable and repeatable way.

Here, I will be using YAML files to create, manage and delete stacks.

  1. Creating a Stack:

YAML Code to Create a Stack with a single ec2 Instance:

Resources:
MyCloudFormationEC2:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: ami-0ab0629dba5ae551d
Tags:
- Key: "Name"
Value: "firstInstance"

This YAML-formatted CloudFormation template defines a CloudFormation resource named “MyCloudFormationEC2”.The Resources section is a mandatory section of a CloudFormation template, which defines the AWS resources to be created and managed by the stack. In this case, a single resource is defined with the logical name "MyCloudFormationEC2”.

The Type property specifies the AWS resource type of the CloudFormation resource. Syntax:

AWS::ProductIdentifier::ResourceType

Eg: To create an S3 Bucket, you have to specify AWS::S3::Bucket . To create a Security group, you have to use AWS::EC2::SecurityGroup.

Here, The EC2 instance created will have the following specifications:

  • Instance Type: t2.micro (Defines Hardware specifications of the Instance)
  • Amazon Machine Image (AMI): ami-0ab0629dba5ae551d (OS of the Instance)
  • Tags: a key-value pair where the key is “Name” and the value is “first-instance”(Name of the EC2 Instance).

By using this CloudFormation template, an EC2 instance with the default VPC will be launched in the specified region. However, it’s important to note that the other properties of the EC2 instance, such as security groups, subnets, and storage, are not specified in this template. If you want, you can specify. Else, it will assume default values.

Note: This file should be saved with .yaml extension.

Now, let us try uploading this file to AWS CloudFormation Management Console. Head over to the AWS CloudFormation console page. Click Create Stack. Now you will land on a page like this.

If you want to use a sample template provided by AWS, you can use it by selecting ‘Use a sample template’ and choosing a desired option among the listed options.

Here, we will be using the ‘Template is ready’ option and uploading our own template.

We can upload our code template in 2 ways.

  1. We can put the YAML(or JSON) file into an S3 bucket and provide the s3 URL.
  2. Upload the file directly by selecting “Upload a template file”.

Here, we are going to upload the above given YAML file. After choosing the file, you will see:

Click ‘View in Designer’ to check if your template is valid.

You can click that ‘tick’ symbol to validate your template. If any errors, it will show you and you can edit the template and save it. Else, it will show “Template is valid”. Then u can click that ‘cloud-with-up-arrow’ symbol to create a stack. Then click next. Then, you will have to enter your stack name.

Then for all others, you can specify if u want. Else, the default will be assumed. I am leaving everything as default. After clicking a bunch of next, You will be asked to “Submit”.

You will have to wait for a few seconds for the creation to complete. After that, you can go to EC2 to find your instance running there which you have created using CloudFormation.

We have now successfully created an EC2 Instance using CloudFormation.

2. Deleting stack:

As mentioned there, if you delete the stack, then all the resources created under the stack will be deleted automatically.

3. Update Stack:

If you want to update your stack, you can do so by clicking the ‘Update’ button which you will find if you select your Stack. You can use either of the three options to update your stack.

Then follow the same steps to create your updated stack. You can view Change Set before updating your stack.

Change Set:

It shows how a change gonna affect your existing resources when you try to update your stack.

  • If you try to change the name of the instance, It is not gonna terminate your instance.
  • If you change your instance type, same.
  • But, if you change your AMI Id, then your instance will be terminated and start again. This may lead to data loss.

Thank you. Hope it helps.

--

--