DEV Community

James MIllar
James MIllar

Posted on • Originally published at james-millar.co.uk

Introducing CloudFormation Templates

This is the first in a series of posts where we’re going to be exploring CloudFormation templates.

If you’re using Amazon Web Services then you should have already heard of CloudFormation. Ideally, you’re already using it in which case you can stop reading now! If not then read on…

CloudFormation is Amazon’s resource automation stack, It allows you to define your infrastructure in code, inside simple YAML or JSON templates. That’s great I hear you say but why would I want to do that?

Well by codifying your infrastructure, that is describing it within a file, you have a way of including it within our source control solution. We can check the files into GitHub or whatever source control solution we’re using. This makes maintaining the infrastructure much easier as you have the full power of a source control system to manage changes to the files.

It also makes deployments much easier. Because the infrastructure is defined within a file, you can reliably and repeatably deploy it over and over again. You can even include it in your build and release pipeline so as your application is deployed to the cloud, any changes required to the infrastructure can also be deployed at the same time.

Infrastructure as code also helps prevent what’s known as environmental. Drift Before Infrastructure as code, developers had to maintain many different development environments, replicating changes across environments manually. This invariable didn’t happen in a consistent way and you end up with differences between one environment and another.

In addition to all of this, your test teams get early access to production-like test environments and It’s possible to spin up an entirely new test environment that’s identical to your production environment very quickly. This might be for load testing or penetration testing for example and once finished the environment can be torn down and discarded.

So let's start by looking at some of the key concepts here. Firstly, we define our infrastructure in what’s known as a template. This is a simple text file in either JSON or YAML format. In order to actually create the resources though we need to create a stack, essentialy we tell AWS to read the template and then create all of the resources that it defines. Stacks can be created within the AWS portal or through the CLI.

So let's take a look at possibly the simplest of templates.

This template creates an Amazon S3 bucket called MyBucket

Resources:
    MyBucket:
        Type:  AWS::S3::Bucket
Enter fullscreen mode Exit fullscreen mode

Now creating buckets is easy as Amazon can create them with default values but what if we want to override or configure those properties. Well, we can do that too. In this example, we’re setting the Access Control property to Public Read.

Resources:
    MyBucket:
        Type:  AWS::S3::Bucket
        Properties:
            AccessControl: PublicRead
Enter fullscreen mode Exit fullscreen mode

Now there are over 500 different resource types that we can create with CloudFormation and we will look at more of those over the coming weeks. We will also look at how you can inject values into and output values from your templates. We will also look at how you can make your templates highly customisable.

In the next post, I’m going to walk you through a simple scenario and show you how to create and execute a complete CloudFormation Template that uses multiple resources.

Top comments (0)