liveBook · Manning

AWS CloudFormation

As explained in Amazon Web Services in Action, Third Edition: An in-depth guide to AWS

AWS CloudFormation is an infrastructure-as-code service that provisions and manages AWS resources from declarative templates.

AWS CloudFormation automates the creation and management of AWS infrastructure. You describe the desired resources in a JSON or YAML template; CloudFormation translates that description into API calls. This declarative approach specifies what the infrastructure should look like, rather than the sequence of commands needed to create it. section 4.0 section 4.5

Start with three terms:

  • A template describes infrastructure. Its major parts are parameters, resources, and outputs. section 4.6
  • A stack is infrastructure created from a template. You can create multiple stacks from the same template, such as separate test and production environments. section 4.5.2 section 4.5
  • A parameter lets you customize a stack. For example, change an EC2 instance’s InstanceType parameter and update the stack instead of manually stopping, modifying, and restarting the instance. section 4.5.2 section 4.5.3
Listing 4.16 A template to create an EC2 instance with CloudFormation
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS in Action: chapter 4'
Parameters:
  VPC:                                               #1
    Type: 'AWS::EC2::VPC::Id'
  Subnet:                                            #1
    Type: 'AWS::EC2::Subnet::Id'
  InstanceType:                                      #2
    Description: 'Select one of the possible instance types'
    Type: String
    Default: 't2.micro'
    AllowedValues: ['t2.micro', 't2.small', 't2.medium']
Resources:
  SecurityGroup:                                     #1
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      # [...]
  VM:                                                #3
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: 'ami-061ac2e015473fbe2'
      InstanceType: !Ref InstanceType
      IamInstanceProfile: 'ec2-ssm-core'
      SecurityGroupIds: [!Ref SecurityGroup]         #4
      SubnetId: !Ref Subnet
Outputs:
  InstanceId:                                        #5
    Value: !Ref VM
    Description: 'Instance id (connect via Session Manager)'

Inspect the YAML template’s Parameters and Resources: the EC2 instance refers to the selected InstanceType, while a security group is defined as another resource. section 4.5.2

Create a WordPress proof of concept

A single stack can create a load balancer, an RDS MySQL database, an EFS network filesystem, security groups, and two EC2 web servers configured for WordPress. In the AWS Management Console, select CloudFormation, choose Create Stack with new resources, and supply the prepared template’s S3 URL. CloudFormation creates the connected resources; delete the stack when the evaluation ends to remove the infrastructure. CloudFormation itself is free, but running resources are billed based on usage. section 2.1 section 2.4 section 2.5

Deploy an application update

CloudFormation can also coordinate a rolling update with an Auto Scaling group and EC2 user data. One command starts the supplied Etherpad stack: `aws cloudformation deploy --stack-name etherpad-cloudformation --template-file chapter15/cloudformation.yaml --parameter-overrides EtherpadVersion=1.8.17 --capabilities CAPABILITY_IAM`. To deploy a new version, update the version parameter. The group launches an instance whose startup script installs and starts the application, then terminates the old instance. This approach suits disposable instances that keep no application data on local disk or in memory. section 15.2

CloudFormation handles resource dependencies and can apply template changes, but it is not the right deployment method for every workload. Rolling updates with user data are flexible and avoid user-facing downtime in the described setup, yet startup can be slow or fail if a dependency such as GitHub is unavailable. CodeDeploy offers in-place deployments; Packer bundles an application into an AMI. Choose according to how the application runs and where it keeps state. section 4.5 section 15.2 section 15.4

How Amazon Web Services in Action, Third Edition: An in-depth guide to AWS covers it

How deep it goes
Works through templates, stacks, parameters, an EC2 update, and an Auto Scaling rollout; does not detail CloudFormation’s internal API-call planning.
Examples
Uses a WordPress infrastructure stack, a parameterized EC2 template, and an Etherpad rolling deployment.
What it assumes
Introduces declarative infrastructure and templates from the ground up; the rolling-deployment example assumes familiarity with Auto Scaling and load balancing.
What you can do after
Operate/configure CloudFormation stacks to create, update, and delete AWS resources and deploy changes to disposable EC2 instances.
How it connects
Connects infrastructure as code with AWS APIs, EC2, Auto Scaling, user data, and deployment alternatives CodeDeploy and Packer.

Also explained in

Compare how 2 books explain AWS CloudFormation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Amazon Web Services in Action, Third Edition: An in-depth guide to AWS. The section links point to the parts of the book it draws on.