DEV Community

Cover image for Hands-on AWS CloudFormation - Part 1. It All Starts Here
Samira Yusifova
Samira Yusifova

Posted on • Updated on

Hands-on AWS CloudFormation - Part 1. It All Starts Here

Greetings, everyone!

I am starting a series on Hands-on AWS CloudFormation in which I will be creating AWS Cloudformation templates starting from basics and building it up. This is the first article as part of that series.

First we need to understand basic terminologies.

What is AWS CloudFormation?

AWS CloudFormation is a service that 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 (like EC2 instances or RDS DB instances), and AWS CloudFormation takes care of provisioning and configuring those resources for you.

Alt Text

Diving Deeper

Infrastructure as Code is the process of provisioning and managing your cloud resources by writing a template file that is both human readable, and machine consumable. For AWS the built-in choice for Infrastructure as Code is AWS CloudFormation.

Template is a JSON or YAML formatted text file. AWS CloudFormation uses these templates as blueprints for building your AWS resources.

Stacks. When you use AWS CloudFormation, you manage related resources as a single unit called a stack. First, you declare all your infrastructure in a template. Then your resources are mapped to the stack. Stack is an end product. It instantiates all specified resources in AWS. You create, update, and delete a collection of resources by creating, updating, and deleting stacks. Remember, all the resources in a stack are defined by the stack's AWS CloudFormation template.

Change sets. If you need to make changes to the running resources in a stack, you update the stack. Before making changes to your resources, you can generate a change set, which is a summary of your proposed changes. Change sets allow you to see how your changes might impact your running resources, especially for critical resources, before implementing them.

Template anatomy

Alt Text

I've screenshotted this image from lecture by
Andrew Brown, ExamPro
. You can watch this very short (under 3 mins) video to understand a template structure (also I'd highly recommend to watch a whole course if you want to get AWS Certified Solutions Architect – Associate certification).

Let's get our hands dirty!

We are going to create a simple CloudFormation template and provision a new S3 bucket.

YAML-based templates use less punctuation and should be substantially easier to write and to read. They also allow the use of comments. That is why we will go with YAML.

Step 1. Create a template

Let's open any code editor such as Visual Studio Code. Create a new "1-Basics.yaml" file. And add the following code:



# create a new S3 bucket 
Resources:
  MyS3Bucket: # logical ID
    Type: 'AWS::S3::Bucket' 
    Properties:
      BucketName: jon-snow-bucket
      Tags: # list of tags
        - Key: CloudFormationLab
          Value: JonSnow


Enter fullscreen mode Exit fullscreen mode

Template describes a single resource - AWS S3 bucket named 'jon-snow-bucket'.

Here is AWS documentation for AWS S3 bucket

Here is GitHub link

Note, each resource will have a Logical ID (resource logical name) and a Physical ID (id assigned by AWS after resource creation). Think of Logical IDs as being used to reference resources within AWS CloudFormation template and Physical IDs being used to identify resources outside of AWS CloudFormation templates after they have been created.

Step 2. Create a stack

Log in to AWS Console, search for CloudFormation service. Click on "Create stack"

Alt Text

Upload your template file

Alt Text

Give a name to your stack

Alt Text

Add a tag for your stack :)

Alt Text

Review the stack and click on "Create stack"

Alt Text

Here you go! (wait couple of seconds and click on refresh button to get the updated list of events)

Alt Text

Step 3. New resources

Click on "Resources" tab to see the list of newly created resources

Alt Text

As you see, a new S3 bucket named 'job-snow-bucket' has been created by AWS CloudFormation. Once you click on bucket's physical ID you will be navigated to Amazon S3:

Alt Text

Step 4. Cleanup

You might be charged for running resources. That is why it is important to clean all provisioned resources once you are done with the stack. By deleting a stack, all its resources will be deleted as well.

Alt Text

Alt Text

Your newly created S3 bucket should no longer exist:

Alt Text

Summary

In this article, we spotlight just a tip of the tip of the CloudFormation iceberg, but this should give you an understanding of how to create template and stack.

The next article in the series will show how to use Intrinsic functions.

Top comments (0)