Hands-on AWS CloudFormation - Part 1. It All Starts Here
Tags: aws, cloud, cloudformation
Published: 2020-12-17 05:15:29

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](https://dev-to-uploads.s3.amazonaws.com/i/z11nmwbclksackedvzgf.jpg)

##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](https://dev-to-uploads.s3.amazonaws.com/i/3n63enm6xyiy3ql1qwr8.png)

I've screenshotted this image from [lecture by 
Andrew Brown, ExamPro](https://www.youtube.com/watch?v=NhQhltDp1o4&ab_channel=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:

```YAML
# 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
```

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

[Here is AWS documentation for AWS S3 bucket](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html)

[Here is GitHub link](https://github.com/Tiamatt/Mastering-AWS-CloudFormation)

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](https://dev-to-uploads.s3.amazonaws.com/i/li2qu394k0bs83ibhz8d.png)

Upload your template file

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bb90za8u3qutt2hghopw.png)

Give a name to your stack

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a6lv23zdg6lunu2c8dnp.png)

Add a tag for your stack :)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/aggofdgex46whi6fe1uk.png)

Review the stack and click on "Create stack"

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/auh5ms4a0kiak908mxft.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lrsdem2fazjlf4lwq4zg.png)

###Step 3. New resources###

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pm1zlezf22q4j9zs6rdt.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/2fi4a8q8ilfllozt8sqw.png)

###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](https://dev-to-uploads.s3.amazonaws.com/i/ln5gvhzbkbne3kltk9cj.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i0terlgqmnolu1qcht0x.png)

Your newly created S3 bucket should no longer exist:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wclxe29mltuw4sn0sn0j.png)

##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.