Writing Your First CloudFormation Template

What you’ll need to write your first CloudFormation template

AWS CloudFormation is a service that helps you model and set up your Amazon Web Services resources so you can spend less time managing those resources, and more time focusing on your applications. CloudFormation can:

  1. Simplify infrastructure management.
  2. Quickly replicate your infrastructure.
  3. Easily control and track changes to your infrastructure.

In this article, I am going to outline the things you need to know to write your first CloudFormation Template, so in no time, you will be launching your very own AWS Infrastructure. For something a bit more advanced, read Understanding Nested CloudFormation Stacks. You might also enjoy Cloud Academy’s course on How to Use CloudFormation for AWS Automation.

CloudFormation Template Basics

The main things you need to keep in mind when building a template are

  • A CloudFormation template is a JSON-formatted text file that describes your AWS infrastructure.
  • Templates can include several major sections:
    – AWSTemplateFormatVersion
    – Description
    – Metadata
    – Parameters
    – Mappings
    – Conditions
    – Resources
    – Outputs
  • The Resources section is the only section that is actually required.
  • The first character in the CloudFormation template must be an open brace ({), and the last character must be a closed brace (}).

CloudFormation Template Skeleton

So for this exercise, assuming that for some of you this is probably your first template, I will try and keep it as simple as possible and keep it to the bare minimum. From what we’ve seen above, we know that we must include a resources section at the very least.  So, for this example, we will be leaving out AWSTemplateFormatVersion, Description, Metadata, Parameters, Mappings, Conditions, and Outputs.

You can put your sections in any order however it would be best to stick with the standard order for obvious reasons. So our order will follow the sections outlined above.

With our choices, our template will take the following structure where the bold italics are what we have to populate with our own data.
Let’s call this our skeleton Template

{
    "Resources" : {
        "Name-of-your-bucket" : {
            "Type" : "resource type"
            }
        }
}

If you hadn’t guessed it already we are going to create a new S3 bucket with our template

Populate our CloudFormation Template with data

The resource type identifies the type of resource that you are declaring. For example, the AWS::EC2::Instance declares an Amazon EC2 instance. For a list of all the resource types, see AWS Resource Types Reference.

Let’s use the following values
Name of your bucket = dontkickthebucket
resource type = AWS::S3::Bucket

We’re almost finished the template!

The following template declares a single resource of type AWS::S3::Bucket: with the name dontkickthebucket

{
"Resources" : {
    "dontkickthebucket" : {
       "Type" : "AWS::S3::Bucket"
       }
    }
}

Let’s try it out

  1. Cut and Paste the above code and save it as a file called bucket.txt (you obviously may prefer a different name for your bucket besides dontkickthebucket).
  2. Go to your AWS Console, and then to the CloudFormation Service.
  3. Click on ‘create a stack.’
  4. Give your stack a name.
  5. Click on Upload a template to Amazon S3 and choose bucket.txt.
  6. Click Next.
  7. Click Next.
  8. Click on Create.

If all has gone well you should see “CREATE IN PROGRESS” and “CREATE_COMPLETE” when finished

Find your new bucket

If you now go to your S3 dashboard, you should see a new bucket. You may also notice another bucket named cf-templates-xxxxxx . If you go into this bucket you will see the bucket.txt file you uploaded. You can use this again by just referencing the http link the next time.

Obviously, this is such a simple template that it would have been quicker to have made the bucket just via the console itself in S3. Hopefully, however, you can see the potential of using a CloudFormation template for simplifying infrastructure management, quickly replicating your infrastructure, or easily controlling and tracking changes to your infrastructure.

Cloud Academy