Skip to main content

Getting Started with AWS CloudFormation

Greetings!

AWS CloudFormation is an infrastructure as code (IaC) solution by AWS to model, provision, and manage AWS resources. Thus, giving us more time to focus on our applications that run in AWS.

What we do is create a template in YAML or JSON format that describes all the resources we want to create, and CloudFormation takes to create it. We are free of manual work.

Without too much theory, let's create an S3 bucket using CloudFormation. We don't have to worry about notations when we get started.

Steps

  • Define the CloudFormation template
  • Upload it to AWS

An IaC S3 Bucket

First of all, let's create our template (cfn-s3.yml)
Resources:
  HelloBucket:
    Type: AWS::S3::Bucket
Step two is to upload this to AWS.
  1. Go to AWS CloudFormation
  2. Click on Stacks
  3. Click on Create stack -> with new resources
  4. Choose "Template is ready"
  5. Choose "Upload a file"
  6. Select your cfn-s3.yml file
  7. Give a Stack name (HelloWorldS3Bucket)
  8. Click Next -> Next -> Create stack
Wait.. this will few seconds/minutes
You would see "CREATE_COMPLETE" event

Go to S3

You can see a bucket is created (ex: helloworlds3bucket-hellobucket-1lkr58xw19ai0)


Update the Bucket

Now, let's update our bucket with public read
Resources:
  HelloBucket:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: PublicRead
  1. Go to CloudFormation > Stacks > HelloWorldS3Bucket
  2. Choose "Replace current template"
  3. Click Next -> Next -> Update stack
You can see in events "UPDATE_IN_PROGRESS"
Go to S3, and you can see "Publicly accessible" applied to the bucket.

Delete the Bucket

With CloudFormation, we do not have to delete resources one by one. Instead, we delete the stack we created, then CloudFormation will handle all deletions.
  1. Go to Stacks
  2. Select your stack and click on delete
If you like, let's see a little bit of theory.

Stack

One word that bugged me when I got started is "Stack". AWS documentation has a better explanation.

A stack is a collection of AWS resources that you can manage as a single unit. In other words, you can create, update, or delete a collection of resources by creating, updating, or deleting stacks.

Building Blocks

We used the "Resources" section in our S3 example as that is the only mandatory field. There are other components like Description, Transform, Metadata, Parameters, Mappings, Outputs, Conditionals, Rules, and helpers (References, Functions) we need to learn but let's learn as we go.

Why do we use CloudFormation?

Let me grab a few notes from the documentation directly.
  • Simplify infrastructure management - Unlike our simple S3 bucket, a real application consists of many resources. Creating, and managing all these resources by hand is difficult and time-consuming. CloudFormation simplifies all these for us.
  • Quickly replicate your infrastructure - We will have to create the same resources over and over for multiple stages, and environments. With CloudFormation we only need to re-use our template.
  • Easily control and track changes to your infrastructure - When we create, update, delete, etc resources manually, we might do mistakes. Also, we might not remember all the things we did. Instead, we have all our changes in the CloudFormation template so that we can easily track our changes.
  • Source control - As with any other IaC solution, we can have our changes under source control like git where others can review, and approve changes.
Hope this helped you too to get started with CloudFormation. 

Comments