Skip to main content

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.

Written by

Michael Sheehy

I have been UNIX/Linux System Administrator for the past 15 years and am slowly moving those skills into the AWS Cloud arena. I am passionate about AWS and Cloud Technologies and the exciting future that it promises to bring.

Related Posts

Sanket Dangi
— February 11, 2019

WaitCondition Controls the Pace of AWS CloudFormation Templates

AWS's WaitCondition can be used with CloudFormation templates to ensure required resources are running.As you may already be aware, AWS CloudFormation is used for infrastructure automation by allowing you to write JSON templates to automatically install, configure, and bootstrap your ...

Read more
  • AWS
  • formation
Paul Carlstroem
— March 2, 2016

Advanced Use of AWS CloudFormation

Advanced Use of AWS CloudFormation is a fantastic new course from a superb instructorOur exceptional AWS course content continues growing with the addition of  Advanced Use of AWS CloudFormation. This is an 8 lesson course with 2 hours of video content designed, built, and presented b...

Read more
  • AWS
  • formation
Rafael Nunes
— January 11, 2016

Publish a SharePoint Farm Using CloudFormation

We'll show you how to automate and deploy a SharePoint Farm fast using CloudFormation the right way.Have you ever needed to quickly create a brand new SharePoint Farm and give the users access through the internet? Like when your client says: "Hey Rafael, we just watched all the Share...

Read more
  • AWS
  • Azure
  • formation
Chandan Patra
— August 12, 2015

DevOps in AWS: CloudFormation, CodeCommit, CodePipeline, and CodeDeploy

We are entering a new era of technology this is causing and a cultural shift in the way software projects are built. The waterfall model paved the way for agile development a few years back. Today a need exists for better collaboration between IT operations, developers, and IT Infrastru...

Read more
  • AWS
  • DevOps
  • formation
Christian Petters
— July 15, 2015

CloudFormation and AWS Deployment Automation

CloudFormation, OpsWorks, and Elastic Beanstalk: why you can (almost) never apply too much automation to your deployment management.If you are like me (and I know that many of you are), then I am sure this has happened to you. You get your hands on a new toy (like CloudFormation) and ...

Read more
  • AWS
  • formation
Michael Sheehy
— June 30, 2015

Is CloudFormation the Perfect Cloud Deployment Tool for AWS?

AWS CloudFormation lets you create and provision AWS infrastructure deployments predictably and repeatedly using templates.Cloud deployment, and specifically AWS cloud deployment, can be a daunting task and AWS provides many us with many useful tools. However, this time I am going to ...

Read more
  • AWS
  • formation
Sanket Dangi
— February 10, 2015

Understanding Nested CloudFormation Stacks

Nested CloudFormation Stack: a guide for developers and system administrators.AWS's CloudFormation is widely used by developers and system administrators and it's not hard to see why. CloudFormation helps you to provision your AWS resources in a predictable order, allowing you to easi...

Read more
  • formation
Andrea Colangelo
— September 8, 2014

Learn AWS Automation And How to Use CloudFormation With Our New Course

I'm so glad to see a brand new course available in the Cloud Academy Training Library, and an extremely interesting one too How to Use CloudFormation for AWS Automation, made by our Senior DevOps Engineer Antonio Angelino, who worked hard to craft this new tutorial about one of the most...

Read more
  • AWS
  • formation