Creating Infrastructure In Few Clicks: CloudFormation on AWS

aps08
5 min readJun 2, 2024

--

Creating the entire infrastructure for a project or organization can be time-consuming and requires many clicks, especially if resources need to be created in a specific order. Additionally, if this infrastructure was initially set up for a proof of concept (POC), you might need to delete all resources afterward, which can be cumbersome and prone to errors, potentially leading to unexpected charges if some resources are not properly removed.

CloudFormation Icon

This article explores how AWS CloudFormation can help address these challenges. It covers (1) What is CloudFormation (2) How to create a CloudFormation template (3) Creating your first service using CloudFormation and (4) How to take user inputs for dynamic resource names.

keywords — AWS, CloudFormation, YML and JSON, Infrastructure as a Code.

What is CloudFormation ?

With AWS CloudFormation, you can define and manage your AWS resources using YAML or JSON templates, streamlining the development process by eliminating the need for manual resource creation and deletion. This service is part of the AWS Free Tier, allowing you to experiment at no cost.

Here’s how it works: First, you create a template( sample template here) in either YAML or JSON format. Then, you navigate to the AWS CloudFormation service and upload your template. After the template is uploaded, the status will show “CREATE_IN_PROGRESS”. Once it transitions to “CREATE_COMPLETE”, it indicates that all your resources have been successfully created without any errors.

Sample template taken from TeamShiksha repository from github.

Let’s get started with our first step, which is to create a template.

How to create a CloudFormation template

As mentioned earlier, CloudFormation will accept a template in either JSON or YML. For this article we will be using YML format (YAML is a human-readable data serialization language, just like XML and JSON, read more about it here). Now let’s understand the building blocks of a CloudFormation template:

  1. Resources: This is the core of the CloudFormation template and represents different AWS resources that will be created or configured. CloudFormation automatically manages the creation, updates, and deletion of resources. Example:
Resources:
MyS3Bucket:
Type: 'AWS::S3::Bucket'

Creating your first service using CloudFormation

Let’s try the template given in the earlier section:

  1. Create a YAML file with the content given below.
Template file

2. Go to the CloudFormation console on AWS, click on “Create stack” and select “With new resources (standard)”. Select “Choose an existing template” under “Prerequisite — Prepare template” and under “Specify template” choose “Upload a template file” and select the template you created in the first step.

Stack upload

3. Click “Next” provide the stack name as “S3BUCKETCLOUDFORMATION” and click “Next” again. On the review and create page, click “Submit”. After that, you will see a screen like this:

Stack status screen

After some time, the S3 bucket will be created as soon as the status transitions to “CREATE_COMPLETE.”

S3 bucket created using CloudFormation

Why is the bucket name so weird? That’s because we never specified the S3 bucket name, so AWS has automatically used some information from the CloudFormation template (template name and resource object name) to create the S3 bucket name, as bucket names must be globally unique.

Let’s understand more about the different sections of the stack:

  • Stack Info: Contains information about the template/stack created.
  • Events: Contains information about the events that occurred during the stack creation.
  • Resources: Contains information about the resources created for the stack. You can also click on the resource to be redirected.
  • Outputs: If resources created are to be used by another template, this section displays some of the values from the current stack, and those values/variables will be visible here.
  • Parameters: If the template takes input from the user, those are considered parameters. All those parameters will be visible here along with the values entered by the user.
  • Template: Contains the template file that was uploaded.

Other sections are for advanced features.

How to take user inputs for dynamic resource names

In the earlier section, we created an S3 bucket, but the bucket name was weird. In this section, we will allow the user to define a bucket name. How can we do this? CloudFormation has another feature called “Parameters.”

Parameters allow you to provide input to the AWS CloudFormation template. They are important to know if you want to reuse the template across a company. Parameters contain the “type,” which defines the type of data. You can also add regex patterns and error messages, as shown in the template below:

Parameters:
BucketName:
Type: String
Description: The name of the S3 bucket
AllowedPattern: '^[a-z0-9-]{3,63}$'
ConstraintDescription: Must be a lowercase alphanumeric string between 3 and 63 characters

Resources:
MyS3Bucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: !Ref BucketName

!Ref is a CloudFormation intrinsic function that allows you to substitute the parameter value in the BucketName property.

Now we can upload this file and, during the process, you will be asked to enter the bucket name, as shown below:

Console to take the bucket name

After typing the bucket name and submitting the form, a bucket will be created using the input value. As you can see below, the bucket name is the same as the input value.

Bucket name is same as input value

In case you want to give the user a dropdown with a fixed set of values for input, you can use Mappings. You can read about it here.

That’s how you can take input from users and create resources. This article serves as a demo for CloudFormation and how you can use it. This was just a simple template for creating an S3 bucket, but CloudFormation supports most of the AWS resources. You can check out a more complex one here. You can learn more CloudFormation here.

Thank you all, for such an awesome response on my last 3 article. We have crossed 4K views. 😃😃😃 . If you like this article give me a clap and feel free to connect with me on Twitter, GitHub and LinkedIn.

--

--