AWS CloudFormation

Have you ever been in situations where you need to deploy more than just a handful of services or applications on AWS. Not only can it be tedious and time-consuming to set each one up manually, but it also increases the chances of you making errors since you have to manage each service manually.  

This is where AWS CloudFormation comes in, CloudFormation is an infrastructure automation platform for AWS that deploys AWS resources in a repeatable, testable and auditable manner. 

What is AWS CloudFormation?

AWS CloudFormation is an AWS service that automates the setup of AWS resources using template files such as JSON and YAML.  

AWS CloudFormation helps you model and set up your AWS resources so that you can spend less time managing those resources and more time focusing on your applications that run in AWS. You create a template that describes all the AWS resources that you want, for example, Amazon EC2 instances or Amazon S3 storage, IAM service for configuring access control. CloudFormation takes care of the provisioning and configuring those resources for you so that you do not have to individually create and configure AWS resources and figure out what is dependent on what.  

The most common use of AWS CloudFormation is for Infrastructure Automation, this is because it can help automate almost every Infrastructure as a Code service. In the upcoming sections you will get some idea of how AWS CloudFormation works for Infrastructure Automation.

Here is a short 3 minute video that talks about what AWS CloudFormation is.

AWS

How does AWS CloudFormation work?

In AWS CloudFormation you make use of template files and stacks, which provide a blueprint for your resources.  

When you provision your infrastructure with CloudFormation, the CloudFormation template describes exactly what resources are provisioned and their settings. Because these templates are text files, you simply track differences in your templates to track changes to your infrastructure, similar to the way developers control revisions to source code. For example, you can use a version control system with your templates so that you know exactly what changes were made, who made them, and when. If at any point you need to reverse changes to your infrastructure, you can use a previous version of your template. 

A CloudFormation template is a JSON or YAML formatted text file. You can save these files with any extension, such as .json, .yaml, .template, or .txt. CloudFormation uses these templates as blueprints for building your AWS resources. For example, in a template, you can describe an Amazon EC2 instance, such as the instance type, the AMI ID, block device mappings, and its Amazon EC2 key pair name. Whenever you create a stack, you also specify a template that CloudFormation uses to create whatever you described in the template. 

A CloudFormation template describes your desired resources and their dependencies so you can launch and configure them together as a stack. You can use a template to create, update, and delete an entire stack as a single unit, as often as you need to, instead of managing resources individually. You can manage and provision stacks across multiple AWS accounts and AWS Regions.

Here is a simple model showing how AWS CloudFormation works:

AWS

Here is a video that talks about using AWS CloudFormation for Infrastructure Automation:

AWS

What features does AWS CloudFormation offer?

AWS CloudFormation gives you an easy way to model a collection of related AWS and third-party resources, provision them quickly and consistently, and manage them throughout their lifecycles, by treating infrastructure as code. Some features that AWS CloudFormation offers are:

1. Authoring with JSON/YAML

Previously, we talked about how AWS CloudFormation makes use of template files. This allows you to model the entire infrastructure using a JSON or a YAML document to determine what AWS resources you would like to use. There is feature called AWS CloudFormation designer that lets you style the infrastructure using predefined templates whilst giving you a visual representation of it.

2. Safety Controls

Amazon CloudFormation automates the provisioning and change of the user infrastructure in a safe and controlled manner. There are almost no manual steps or controls which will cause errors.

It lets you use Rollback Triggers to specify the CloudWatch alarm that CloudFormation uses to monitor the processes such as stack creation and update method. If any of the alarms are triggered, AWS CloudFormation rolls back the whole stack operation to a previous deployed state.

3. Preview Changes to a User Environment

AWS CloudFormation lets the user to preview any projected changes to a stack that would possibly impact the currently running resources.

4. Dependency Management

Amazon CloudFormation automatically manages dependencies between the resources throughout stack management actions. This reduces the burden on the users by not having to worry about specifying the order during which resource are created, updated, or deleted. It determines the proper sequence of actions to use for every resource once playing stack operations.

5. Cross Account and Cross-Region Management

AWS has a service called StackSets that enables you to provision a typical set of AWS resources across multiple accounts and regions with one CloudFormation template. It undertakes the process of provisioning, updating, or deleting stacks in multiple accounts and across various regions. This provides some level of automation to stack management operations across different regions and accounts.

6. Extensibility

Amazon CloudFormation allows you to make custom extensions to stack templates using AWS Lambda. You will be able to use Custom Resources, which let customize provisioning during a Lambda process and have it trigger whilst a CloudFormation stack runs.

How much does it cost to use AWS CloudFormation?

AWS CloudFormation offers an easy and consistent way to model, provision, and manage a collection of related AWS and third-party resources by treating infrastructure as code. You only pay for what you use, with no minimum fees and no required upfront commitments.

AWS CloudFormation lets you use third party resources, where the first 1,000 handler operations per month per account are free. After which, each per handler operation costs $0.0009 with an additional cost for the amount of time it operates.  First 30 seconds of handler operation duration are offered at no additional charge per operation. If your handler operation duration runs longer than 30 seconds per operation, you will be charged at $0.00008 per-second above the 30 seconds threshold.

Conclusion

By now you have an idea of what AWS CloudFormation is. But the question of the day is, why should you use AWS CloudFormation. Lets take an example scenario into consideration.

For a scalable web application that also includes a backend database, you might use an Auto Scaling group, an Elastic Load Balancing load balancer, and an Amazon Relational Database Service database instance. You might use each individual service to provision these resources and after you create the resources, you would have to configure them to work together. All these tasks can add complexity and time before you even get your application up and running. 

Instead, you can create a CloudFormation template or modify an existing one. A template describes all your resources and their properties. When you use that template to create a CloudFormation stack, CloudFormation provisions the Auto Scaling group, load balancer, and database for you. After the stack has been successfully created, your AWS resources are up and running. You can delete the stack just as easily, which deletes all the resources in the stack. By using CloudFormation, you easily manage a collection of resources as a single unit.