CloudFormation Template Basics

Defining your resources in JSON.

Posted by Mike Apted on Monday, March 21, 2016

An AWS CloudFormation template, which is a JSON document, may contain the following sections (keys):

  • Parameters
  • Conditions
  • Mappings
  • Resources
  • Outputs

Note that only the Resources section is required to use a template, the rest are optional. In addition you can include:

  • AWSTemplateFormatVersion
  • Description
  • Metadata

Parameters

Parameters allow you to accept input into your CloudFormation stacks. It might be something as simple as the application name, it might be database parameters, it might be instance types for your EC2 instances.

You can specify the parameter type, i.e. string, number, list, etc.. You can specify default values, allowed values and allowed value patterns. You can also include an optional description which will help users when creating a new stack in the AWS web console, or for others re-using your template and inspecting the JSON directly.

A parameter to accept an application name, with a couple constraints, would look like this:

{
  "Parameters": {
    "ApplicationName": {
      "Description": "The application name",
      "Type": "String",
      "Default": "My Cool Application",
      "MinLength": "1",
      "MaxLength": "16"
    }
  }
}

Conditions

Conditions allow you to define true/false states based on CloudFormation intrinsic functions. For example, if a resource should only be created in a production version of the stack you might specify something like:

{
  "Conditions" : {
    "CreateProdResources" : {
      "Fn::Equals" : [
        { "Ref" : "EnvironmentType" }, "production"
      ]
    }
  }
}

This would inspect the parameter named EnvironmentType in this case, and set the CreateProdResources condition to true if it equals “production”. This condition can then be used to include or ignore Resources in the stack.

Mappings

Mappings allow you to define key/value lookups that can be used to enhance the portability of your templates. You may define a set of AMIs to use depending on the AWS region that the template is being used to create a stack in. It might look something like:

{
  "Mappings" : {
    "RegionMap" : {
      "us-east-1"      : { "32" : "ami-6411e20d"},
      "us-west-1"      : { "32" : "ami-c9c7978c"},
      "eu-west-1"      : { "32" : "ami-37c2f643"},
      "ap-southeast-1" : { "32" : "ami-66f28c34"},
      "ap-northeast-1" : { "32" : "ami-9c03a89d"}
    }
  }
}

Resources

Resources, the only required element of the template, is where you define the actual infrastructure components of the stack. The list of supported resources is quite large, but not exhaustive. On each resource you specify the type, for example AWS::EC2::Instance and any required properties.

Outputs

Outputs can be used to communicate information about the stack to the user or to other CloudFormation templates (they can be nested).

You might choose to output the DNS name of a load balancer, for example, which you would open in your browser to test the stack creation and use the application after it’s built.