Skip links

Understanding AWS CloudFormation Concepts

When you use AWS CloudFormation, you work with templates and stacks. You create templates to describe your AWS resources and their properties. Whenever you create a stack, CloudFormation provisions the resources that are described in your template.

Topics I will be discussing in details are;

  • Templates
  • Stacks
  • Change sets

Templates

A CloudFormation template is a JSON or YAML formatted text file. You can save these files with any extension, such as .json, .yaml, .template, or .txt. CloudFormation uses these templates as blueprints for building your AWS resources. For instance, in a template, you can describe an Amazon EC2 instance, such as the instance type, the AMI ID, block device mappings, and its Amazon EC2 key pair name. So, whenever you create a stack, you also specify a template that CloudFormation uses to create whatever you described in the template.

For example, if you created a stack with the following template, CloudFormation provides an instance with an ami-0ff8a91507f77f867 AMI ID, t2.micro instance type, testkey key pair name, and an Amazon EBS volume.

Below is a Json template format version in CloudFormation

JSON

{

  “AWSTemplateFormatVersion” : “2010-09-09”,

  “Description” : “A sample template”,

  “Resources” : {

    “MyEC2Instance” : {

      “Type” : “AWS::EC2::Instance”,

      “Properties” : {

        “ImageId” : “ami-0ff8a91507f77f867”,

        “InstanceType” : “t2.micro”,

        “KeyName” : “testkey”,

        “BlockDeviceMappings” : [

          {

 “DeviceName” : “/dev/sdm”,

            “Ebs” : {

              “VolumeType” : “io1”,

              “Iops” : 200,

              “DeleteOnTermination” : false,

              “VolumeSize” : 20

            }

}

]

}

}

}

}

Below is a Yaml template format version in CloudFormation

YAML

AWSTemplateFormatVersion: “2010-09-09”

Description: A sample template

Resources:

  MyEC2Instance:

    Type: “AWS::EC2::Instance”

    Properties: 

      ImageId: “ami-0ff8a91507f77f867”

      InstanceType: t2.micro

      KeyName: testkey

      BlockDeviceMappings:

        –

          DeviceName: /dev/sdm

          Ebs:

            VolumeType: io1

 Iops: 200

            DeleteOnTermination: false

            VolumeSize: 20

You can also specify multiple resources in a single template and configure these resources to work together. Example, you can modify the previous template to include an Elastic IP address (EIP) and associate it with the Amazon EC2 instance, as shown in the example below:

JSON

{

    “AWSTemplateFormatVersion” : “2010-09-09”,

    “Description” : “A sample template”,

    “Resources” : {

      “MyEC2Instance” : {

        “Type” : “AWS::EC2::Instance”,

        “Properties” : {

          “ImageId” : “ami-0ff8a91507f77f867”,

          “InstanceType” : “t2.micro”,

 “KeyName” : “testkey”,

          “BlockDeviceMappings” : [

            {

              “DeviceName” : “/dev/sdm”,

              “Ebs” : {

                “VolumeType” : “io1”,

                “Iops” : 200,

                “DeleteOnTermination” : false,

                “VolumeSize” : 20

              }

            }

          ]

        }

      },

      “MyEIP” : {

        “Type” : “AWS::EC2::EIP”,

        “Properties” : {

  “InstanceId” : {“Ref”: “MyEC2Instance”}

        }

      }

    }

  }

YAML

AWSTemplateFormatVersion: “2010-09-09”

Description: A sample template

Resources:

  MyEC2Instance:

    Type: “AWS::EC2::Instance”

    Properties: 

      ImageId: “ami-0ff8a91507f77f867”

      InstanceType: t2.micro

      KeyName: testkey

  BlockDeviceMappings:

        –

          DeviceName: /dev/sdm

          Ebs:

            VolumeType: io1

            Iops: 200

            DeleteOnTermination: false

            VolumeSize: 20

  MyEIP:

    Type: AWS::EC2::EIP

    Properties:

      InstanceId: !Ref MyEC2Instance

The previous templates are centred around a single Amazon EC2 instance. However, CloudFormation templates have additional capabilities that you can use to build complex sets of resources and reuse those templates in multiple contexts. For example, you can add input parameters whose values are specified when you create a CloudFormation stack. In other words, you can specify a value like the instance type when you create a stack instead of when you create the template, making the template easier to reuse in different situations.

Leave a comment

This website uses cookies to improve your web experience.
Explore
Drag