Cloud computing has fundamentally altered our perceptions of IT infrastructure. Long gone are the days in the times of racking and stacking, where you would provision infrastructure by putting servers on metal racks, install OS on physical boxes, load ISO’s and install tons of libraries and dependencies using remote administration consoles hoping it all works after waiting several days or months. That still kind of happens in the backend, but you don’t have to worry about it too much now, thanks to cloud computing.
Furthermore, the adoption of cloud computing isn’t slowing down any time soon, in fact, things have moved fast since then. We switch over from a lot of racking and stacking to a lot of typing and commenting, treating it like a code. Yes! you heard it right, coding an infrastructure or often known as Infrastructure as Code.
Introducing AWS CloudFormation
Can you figure out what kind of cloud formation you’re looking at? You guessed it right! It is a cumulonimbus cloud but that’s only an analogy, we are not going to talk about it right now (maybe we can over beer) but today we are going to talk about AWS CloudFormation.
AWS CloudFormation is an example of the Infrastructure as Code (IaC) service of AWS where you write code in a text file in JSON/YAML format to deploy and manage a particular resource or infrastructure in AWS cloud environment. CloudFormation helps you with your resource modeling by specifying them in a template that can be deployed as an AWS stack. You can go from creating resources from the console to automating complicated architecture on-demand with CloudFormation.
Let’s get started with AWS CloudFormation’s fundamentals.
The benefits of using CloudFormation
CloudFormation has a number of advantages, including:
- Improving your automation. The template’s simplicity allows you to describe how you want your resources to appear. It is no longer necessary to use other scripting tools to produce the resources.
- Human-readable template. If you’re working on a web application or a microservice, you’ve probably used YAML or JSON before. They are both commonly used configuration languages in IT, making them simple to comprehend and to get information about resources on a particular stack.
- Consistency in infrastructure. The declarative approach to template definition ensures consistency. The stacks produced with the template will always be the same at any given time unless you update and declare a new version of it.
- Infrastructure replication is done quickly. You can swiftly clone your infrastructure, recreating your dev, test, staging, and production environments. You can use the template to make as many stacks as you want.
Understanding AWS CloudFormation
To understand how CloudFormation works, we need to be aware of these three concepts.
Let’s define each one.
A template is a declarative YAML or JSON file that defines your resources. This serves as a blueprint for your infrastructure. You write a blueprint document with all the resources you want AWS to produce, run a command, and AWS magically creates everything.
The sample.yaml below shows an example of a template file that creates an EC2 instance and attached an EC2 Security Group that allows both port 80 and 443 access to the instance.
When you deploy a template like the one we used earlier, it creates both Security Group and EC2 resources as a stack. Because these resources were built as a single entity, any changes or deletions to them will be reflected in the stack.
As long as there are no naming conflicts, you can utilize a single template to construct as many stacks as you want.
When a stack has to be updated, all you have to do is perform an update on the stack and AWS will take care of the rest. ChangeSet goes a step further by allowing you to preview the effects of the changes you’re making before they’re implemented. This would be the counterpart of the terraform plan in the Terraform world.
Deploying an AWS CloudFormation template
From this short guide, you will learn how to deploy AWS CloudFormation templates and use Infrastructure as Code to provision your infrastructure resources.This example assumes you have an AWS account, networking setup, access keys, and AWS CLI installed. Screenshots were intentionally used for you to retype the template manually for better retention.
Creating a stack
To create a new stack, simply create a new file sample.yaml, and copy the following code snippets earlier.
To deploy a stack, run:
aws cloudformation create-stack –stack-name sample-stack –template-body file://sample.yaml
After running this command, you should see an output that looks like this:
Updating a stack
Let’s imagine you wish to change the instance type used in the stack. Simply remove t2.nano and replace it with t2.micro in the template sample.yaml. We can then use the update-stack API function to deploy that change. This is also a good moment to assess the impact of our changes with an AWS CloudFormation changeset.
aws cloudformation create-change-set –stack-name sample-stack –change-set-name sample-changeSet –template-body file://sample.yaml
As can be seen above, the EC2 instance type will be updated. In our scenario, replacing the EC2 instance is our requirement, thus we can proceed with the changeset.
We can run the following commands to apply the changeset:
aws cloudformation execute-change-set –stack-name sample-stack –change-set-name sample-changeSet
In the AWS console, we can see the changes being applied.
Deleting a stack
To delete the stack, you can run the delete-stack API action. In the demo example, run:
aws cloudformation delete-stack –stack-name sample-stack
In the console, you can see the stack being deleted.
There you go, that’s basically what AWS CloudFormation is, but we only just scratched the surface. If you want to dive deep into using AWS CloudFormation, you can find more information by reading the AWS documentation about Getting started with AWS CloudFormation.
For more information on how Apper can be your partner in innovation, you can check out our services and other DevOps resources here.