AWS Cloud Formation

Introduction

We can create AWS resources directly from web management console. This may be ok when dealing with a very simple PoC type of projects. But imagine you have 50 projects and 4 environments per project. How are you going to provision your infrastructure? Doing this via management console will be a very tedious and prone to error process.

It will take long time to provision infrastructure and this will not scale. There is no version control in manual process. Lack of Audit trail (CloudTrial may not efficient) and Lack of documentation is another concern for manual processes.

There should be a better way…Meet AWS Cloud Formation service.

What is AWS Cloud Formation

AWS cloud formation is a service that helps you model and then setup your AWS resources. It is the tool that realizes “Infrastructure as Code”. It helps reduce time managing our resources which allow spent more time on applications development. Few CloudFormation features are as follows:

  • Declarative programming language for deploying AS resources.
  • Support majority of AWS services.
  • All of our resources are managed via a single unit called a cloud formation stack.
  • Detect drift on stack and individual resources

Basic Cloud Formation Components

Templates and Stacks are the two core components of AWS CloudFormation Service:

Template

A CloudFormation template is a JSON or YAML formatted text file. We can use this template to create/update/delete CloudFormation stacks.

Stack

When you use CloudFormation, you manage related resources as a single unit called a stack. You create, update, and delete a collection of resources by creating, updating, and deleting stacks. All the resources in a stack are defined by the stack’s CloudFormation template.

  • Stack is A collection of AWS Resources.
  • Stacks are the instantiation of a template.
  • Templates can be instantiated multiple times to create multiple stacks.

Here is a diagram from the official website showing the CloudFormation workflow:

Structure of AWS Template

Here is the structure of a CloudFormation template:

In a CloudFormation template, only Resources section is required. Following is very simple description of a template sections:

AWS Template Version

This is version info of the template and set by AWS.

Description

Contains some description about the template.

Metadata

Contains objects that provide additional information about the template.

Parameters

This section hold the values to pass into template at runtime. e.g. environment variables. These parameters can then be referenced in both the resource and output sections of the template.

Mappings

It is a key-value pair construct that can be used to setup a lookup table. You can match to a key and then retrieve the corresponding value inside the resource or output sections of the template.

Conditions

Control when certain resources are created e.g. a resource’s creation could depend on whether a stack is being used for production or a test environment.

Transforms

Used to specify the version of AWS’s serverless application model and used to work with template snippets stored elsewhere (promotes reusability).

Resources

Is the only required section within a template. It specifies the resources and their properties.

Output

Returns values that are shown as stack properties when viewing the result of a deployed template.

Intrinsic Functions

Technically not a section but very important part of an overall template. It allows perform string manipulations, lookup key-values, generate base64 values and other operations to support defining a template in a generic manner.

CloudFormation Template- S3 based website

Lets examine a very simple CloudFormation template. This can be used to define an S3 based static website resources:

You can download the template from this git repository.

Following are the things this cloudformation template is describing:

  • Create and configure a bucket.
  • Configure bucket for the website.
  • Create and configure a bucket policy.
  • Output the website URL.

The above mentioned template is very simple and self explanatory. We have parameters section where bucket name is parameterized. Then two resources are defined in the resources section which are needed for S3 bucket based website and finally we have Outputs section to output the URL.

Based on your requirement, you can use all or some of template sections.

Note, there are some built-in functions like !Ref, !Join and GetAtt to help us with using template information in some meaningful way.

Next, lets see how we can use this template to create a stack.

Create a Stack

Ok, lets use this template to create the infrastructure. To keep things simple, we will use AWS CloudFormation web console (we will see other methods in later posts).

On the console web-page click Create stack button:

This will start a wizard and first step is to prepare templates:

Here I am simply uploading the template file and when ready click Next button to specify stack details:

Here we can specify the stack-name (note BucketName is automatically populated from the template). Click Next will take us to Configure stack options page:

Here we can configure a lot of options. I am leaving everything to default here and moved to next page (Review):

Here we can review all the steps and once done, click the Create stack button at bottom of the page and we can see stack creation in progress:

In few minutes (based on template) the stack creation will be completed:

Here you can see the other information from the template as well e.g. Outputs, Parameters etc.

So, we have successfully create the stack from CloudFormation template.

Clean up

Once finished with our demo testing, to make sure we aren’t charged for any unwanted services, we can clean up by deleting the stack and its resources.

Sample CloudFormation Templates

There are many samples CloudFormation templates available on the official web page you can check. There are examples involving EC2, Load Balancers, VPCs etc.

You can click View button to check the sample template. These samples are great way to learn CloudFormation.

Summary

AWS CloudFormation simplifies provisioning and management on AWS. You can create templates for the service or application architectures you want and have AWS CloudFormation use those templates for quick and reliable provisioning of the services or applications (called “stacks”). You can also easily update or replicate the stacks as needed.

We will use CloudFormation stacks in some later posts to provision and manage resources and see few more examples as well.

Let me know if you have some comments or questions.