Getting Started with CloudFormation

Hands-On Lab

 

Photo of Adrian Cantrill

Adrian Cantrill

Training Architect

Length

01:00:00

Difficulty

Beginner

CloudFormation is a powerful automation product within AWS. It can be used to create simple or complex sets of infrastructure any number of times. This hands-on lab provides a gentle introduction to CloudFormation — using it to create and update a number of S3 buckets. By the end of this hands-on lab, you will be comfortable using CloudFormation and can begin experimenting with your own templates.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Getting Started with CloudFormation

Introduction

This hands-on lab provides a gentle introduction to CloudFormation, using it to create and update a number of S3 buckets.

By the end of this hands-on lab, you will be comfortable using CloudFormation and can begin experimenting with your own templates.

Solution

Log in to the live AWS environment using the credentials provided, and make sure you are in the us-east-1 (N. Virginia) region.

The CloudFormation templates and other hands-on lab files can be found here.

A list of AWS resources and what happens when updates occur can be found here.

Create a CloudFormation Stack

We're going to need a file form the GitHub repository for this step. If you're familiar with zip files, you can go the the repository's main page and download it. Then extract from the file that gets downloaded, and find the files as needed.

Or, for this step, you can just go to the createstack page and download it, using your browser's Save Page As functionality. When you've done that, just remember where createstack.json gets saved. We're going to need it.

From the CloudFormation Management Console:

  1. Click Create Stack.
  2. Select Template is ready.
  3. Choose to Upload a template and Choose file.
  4. Locate createstack.json in the AWS browser window that popped up. Click Open once it's selected, then click Next.
  5. Name the stack cfnlab. Click Next and then Next again.
  6. Accept the remaining defaults, and click Create stack.

We can refresh the page to watch the progress.

Once it's done, we can get into the S3 Management Console. We didn't specify a name in the json file for this bucket, so AWS names it with the [STACKNAME]-[LOGICAL_VOLUME_NAME]-[RANDOM_STRING] format.

Update a CloudFormation Stack

Like in the last step, we're going to need a couple of files. Just like last time, either browse to where you downloaded and extracted the zip file, or (if you didn't download the zip file) go to the pages containing the files we need and save them like we did for createstack.json: updatestack1 and updatestack2.

From the CloudFormation console:

Update #1

  1. Select the cfnlab stack, and click Update.
  2. Select Replace current template and Upload a template file.
  3. Select updatestack1.json from wherever we saved it, click Open, then click Next.
  4. Click Next and Next again, and Update stack.

We can check again in the S3 Management Console, and we'll see the new dogpics bucket.

Remove the Update

  1. Select the cfnlab stack, and click Update.
  2. Select Replace current template and Upload a template file.
  3. Find createstack.json again, then click Open and Next.
  4. Click Next and Next again, and then Update stack.

Check in the S3 Console again if you like. The dogpics bucket should be gone.

Update #2

  1. Before we move forward with grabbing the new json file, we need to edit it. Assuming you've already downloaded the updatestack2 file, use whichever text editor you're familiar with to change the X characters in catsareawesomeXXX to something unique. Save the file.
  2. In the AWS console, select the cfnlab stack, and click Update.
  3. Select Replace current template and Upload a template file.
  4. Select updatestack2.json from wherever we saved it, click Open, then click Next.
  5. Click Next, Next again, and update the stack.

Now if we go into the S3 Console, we'll see two changes. One is that the dogpics bucket is back, but the other is that now our Bucket name is catsareawesomeXXX.

Play with More Stacks

Because we explicitly named a bucket in updatestack2.json, we can use that template to create another stack. We can try, by going through the motions we just did in the Update #2 steps. Just change the stack name to cfnlab2. It will fail. We can't have another S3 bucket named catsareawesome123.

But what if we go through the steps we did when we added the first update? Let's do that, just naming the stack cfnlab3 this time.

It works! If we peek in the S3 area again, we'll see two new buckets, where the first part of the name is cfnlab3-, one for catpics, and one for dogpics.

Delete a Stack

From the CloudFormation console, do the following for each CloudFormation stack created in this lab (cfnlab*):

We can select the stacks, one by one, and delete. Hitting refresh will show us recources getting removed, just like it showed us them getting added earlier. Once it's all done, we can look at our S3 buckets again, and all of the cflab ones should be gone, as well as the catsareawesome123 bucket.

Conclusion

We've managed to use templates to create stacks and related resources, as we were able to get things cleaned up when we were done with them in pretty short order. These are very handy skills to have. Congratulations!