A Complete Beginners Guide on AWS CloudFormation
Amazon Web Services (AWS) is the world’s leading cloud platform, widely adopted and offering more than 200 fully-featured services from global data centers. With a cloud computing provider like AWS, users get access to various technology services such as storage, computing power, and databases without buying, owning, and maintaining physical servers and data centers. From leading government agencies and established enterprises to rapidly expanding startups, AWS is the premier choice for faster innovation, more agility, and lower costs.
However, deploying multiple applications or services on AWS can be time-consuming, tedious, and error-prone if each one has to be set up manually.
That’s where AWS CloudFormation comes into the picture. It is an infrastructure automation platform facilitating the faster, more secure, and more efficient deployment of AWS resources.
This article is an overview of What is AWS CloudFormation and the basics of how it works.
What is AWS CloudFormation?
The creation and management of multiple AWS resources can be laborious and challenging. But AWS CloudFormation can help minimize the problems. Through the provisioning and updating of a collection of AWS resources in an organized and predictable way, it provides an easy solution to create and manage multiple resources. In other words, AWS CloudFormation lets users create and model applications and infrastructure, and eliminates the need to perform actions manually.
AWS CloudFormation uses template files for the automation of AWS resources. It can be described as a cloud automation solution and an Infrastructure-as-Code (IaC) tool since it can automate the configuration and deployment of the various services that run on AWS.
For those getting started with AWS or learning how to deploy AWS services, the conventional approach is to manually configure and deploy services using the AWS API, command-line interface, or Web Console. However, as the environments scale in size, it becomes pertinent to leverage a solution like AWS CloudFormation for faster, efficient, and more consistent deployment.
How Does AWS CloudFormation Work?
When using AWS CloudFormation, there are three fundamental concepts that one needs to be aware of to understand how the automation platform works. The concepts are as follows:
- Change Set
Let’s take a detailed look into each of the above concepts:
Templates in AWS CloudFormation are a declarative way of defining resources as a JSON or YAML file. Using the template, users can deploy their resources either utilizing the console or the command-line interface. When using the console, one can drag and drop a resource into a drawing panel, and the relevant code for that resource (in JSON and YAML) is generated automatically. It can be modified subsequently. Users also can use any editor to write their own script and upload it to CloudFormation.
Given below is a sample template in YAML:
The above sample script has four important sections:
AWSTemplateFormatVersion: Currently, 2010-09-09 is the only version of the CloudFormation template.
Resources: The list of all the resources that the template will create.
Properties: This is where users specify any external property to be used and referenced in the template.
Output: It is the list of outputs that will be displayed on the CloudFormation console.
While deploying a template, it creates both resources (EC2 and RDS in the above example) as a stack. Since the resources are created as a unit, any update or deletion of resources gets applied to the stack. A single template can be used to create multiple stacks so long that the naming is different.
When a stack needs an update, users can simply run the update on the stack and AWS does the job of replacing the requisite resources. With Change Set, users can see the impact of the changes before they are actually applied.
An Example to Show How CloudFormation Works
In the example below, we will show the deployment of the demo.yaml template. It creates an EC2 instance and an Elastic IP and then attaches the IP to the instance.
We will use the AWS command-line interface to deploy the template. Users must have AWS CLI installed.
Creating a Stack
Run the following to create a stack:
aws cloudformation create-stack –stack-name demo-stack –template-body file://demo.yaml
Running the above command will give the following output:
An error-free deployment will give the following message in the AWS console:
Updating a Stack
If the user wants to update the stack to a different instance type, they have to simply update the template by removing t2.nano and adding t2.micro. Hence, the user will be able to deploy the change by running the update-stack API action. Further, using a change set at this point will show the impact of the changes.
Run the following command:
aws cloudformation create-change-set –stack-name demo-stack –change-set-name demo-changeSet –template-body file://demo.yaml
The changes will be visible in the AWS console as follows:
By clicking on the change set name, users can see what happens if they apply the change set. Here’s a snapshot of what will be visible for demo-changeSet:
As evident, both EC2 and EIP get modified. Run the following command to apply the change set:
aaws cloudformation execute-change-set –stack-name demo-stack –change-set-name demo-changeSet
The changes being applied will be shown in the AWS console as follows:
Deleting a Stack
Running the delete-stack API action will delete the stack. The command for the same is as follows:
aaws cloudformation delete-stack –stack-name demo-stack
The AWS console shows the stack is being deleted.
AWS CloudFormation offers several benefits. To begin with, the simplicity of the templates eliminates reliance on other scripting tools for resource creation. In addition, it allows quick infrastructure replication without impacting other resources created previously. Furthermore, the declarative manner of defining templates allows infrastructure consistency. Last but not the least, JSON and YAML make the templates easy to read while also simplifying understanding and finding resources. Overall, CloudFormation streamlines the AWS deployment process and eliminates the risks of configuration inconsistencies that could lead to security compromise and management issues.