Skip to main content

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 focus on my personal favorite cloud deployment option: CloudFormation.

Once you’ve launched a few CloudFormation installations, you can use these templates over again and again, which is extremely helpful if you need to constantly deploy new infrastructure. Best of all AWS has pre-built templates that you can use or modify to your heart’s content.

AWS CloudFormation Best Practices

As with all AWS services, before you start it’s a good idea to do some research. Of course, there AWS’s own documentation, but I’ll offer you a brief CloudFormation best-practice summary:

Planning and organizing

  • Organize your stacks by Lifecycle and Ownership (use the lifecycle and ownership of your AWS resources to help you decide what resources belong in each stack).
  • Reuse Templates to replicate stacks in multiple environments (to make templates reusable, use the parameters, mappings, and conditions sections so that you can customize your stacks when you create them).
  • Verify quotas for all resource types (before launching a stack, ensure that you can create all the resources that you want without hitting your AWS account limits).
  • Use Nested Stacks to reuse common Template patterns (separate out common components and create dedicated templates for them).

Creating templates

  • Do not embed credentials in your Templates (use input parameters to pass in information whenever you create or update a stack).
  • Use AWS-Specific Parameter Types (you can specify a parameter as type AWS::EC2::KeyPair::KeyName).
  • Use Parameter Constraints (describe allowed input values so that AWS CloudFormation catches any invalid values before creating a stack).
  • Use AWS::CloudFormation::Init to deploy software applications on Amazon EC2 instances (install and configure software applications on Amazon EC2 instances by using the cfn-init helper script and the AWS::CloudFormation::Init resource).
  • Validate Templates before using them (validating a template can help you catch syntax and some semantic errors ).

Managing stacks

  • Manage all stack resources through AWS CloudFormation (do not make changes to stack resources outside of AWS CloudFormation).
  • Use Stack Policies (stack policies help protect critical stack resources from unintentional updates).
  • Use AWS CloudTrail to log AWS CloudFormation calls (CloudTrail tracks anyone making AWS CloudFormation API calls in your AWS account).
  • Use code reviews and revision controls to manage your templates (to review changes and to keep an accurate history of your resources).

If you are going to create your own templates then it is in your best interests to try and adhere to these best practices, as they’re based on real-world experience from active AWS CloudFormation users.

What does a template look like?

A CloudFormation template is a JSON-formatted text file that describes your AWS infrastructure. Templates include several major sections. Here are the most common sections that you’re likely to find in a CloudFormation template:

Note: the Resources section is the only section that is actually required!

{
  "AWSTemplateFormatVersion" : "version date",
  "Description" : "JSON string",
  "Metadata" : {
    //template metadata
  },
  "Parameters" : {
    //set of parameters
  },
  "Mappings" : {
    //set of mappings
  },
  "Conditions" : {
    //set of conditions
  },
  "Resources" : {
    //set of resources
  },
  "Outputs" : {
    //set of outputs
  }
}

Launching a template

As stated previously, the good news is that AWS has lots of sample templates available for each region.
Because each region might have different requirements, a template that works in one region might not work in another region.

The following example uses the Asia Pacific (Sydney) RegionLet’s choose a template and prepare to launch it.

  • Choose your template. I am going to use the template “Amazon EC2 instance in a security group” which, as you should be able to guess, creates an Amazon EC2 instance in an Amazon EC2 security group. You can view the template here.
  • Click on the “Launch stack” button as shown here:
CloudFormation launch stack
  • On the “Select Template” page just click NEXT as you have already chosen your template.
  • You will now come to “Specify Parameters” page. This presents the template’s parameters:
"Parameters" : {
    "KeyName": {
      "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
      "Type": "AWS::EC2::KeyPair::KeyName",
      "ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
    },
    "InstanceType" : {
      "Description" : "WebServer EC2 instance type",
      "Type" : "String",
      "Default" : "m1.small",
      "AllowedValues" : [ "t1.micro", "t2.micro", "t2.small", "t2.medium", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "m3.medium", "m3.large", "m3.xlarge", "m3.2xlarge", "c1.medium", "c1.xlarge", "c3.large", "c3.xlarge", "c3.2xlarge", "c3.4xlarge", "c3.8xlarge", "c4.large", "c4.xlarge", "c4.2xlarge", "c4.4xlarge", "c4.8xlarge", "g2.2xlarge", "r3.large", "r3.xlarge", "r3.2xlarge", "r3.4xlarge", "r3.8xlarge", "i2.xlarge", "i2.2xlarge", "i2.4xlarge", "i2.8xlarge", "d2.xlarge", "d2.2xlarge", "d2.4xlarge", "d2.8xlarge", "hi1.4xlarge", "hs1.8xlarge", "cr1.8xlarge", "cc2.8xlarge", "cg1.4xlarge"],
      "ConstraintDescription" : "must be a valid EC2 instance type."
    },
    "SSHLocation" : {
      "Description" : "The IP address range that can be used to SSH to the EC2 instances",
      "Type": "String",
      "MinLength": "9",
      "MaxLength": "18",
      "Default": "0.0.0.0/0",
      "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
      "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
   }
  • I am going to choose t1.micro, name my key pair, and leave SSH Location as default. Click NEXT.
  • On the options page, I suggest Key=Name and Value={any name you want to describe it}. Click NEXT.
  • Review the Page then click CREATE.
  • Click CREATE STACK in top left corner
  • You should then see CREATE_IN_PROGRESS
  • Refresh screen after a minute or two and you should see CREATE_COMPLETE
  • Click on the stack name and then on the Events tab. You should see a screen similar to this:
CloudFormation Stack

Summary

The above is probably the simplest example of a CloudFormation deployment. Here are some more things you could do on AWS with the simple click of a launch button:

  • Create a DynamoDB table with global and local secondary indexes.
  • Create an AWS OpsWorks stack with a load-balanced application that runs inside a designated VPC.
  • Create an Amazon RDS database instance with provisioned IOPs.
  • Create a publicly accessible Amazon S3 bucket that is configured for website access.

Other CloudFormation resources

If you’d like to dig a bit deeper into CloudFormation, try:

Avatar

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

Alisha Reyes
Alisha Reyes
— July 22, 2019

Cloud Academy’s Blog Digest: July 2019

July has been a very exciting month for us at Cloud Academy. On July 10, we officially joined forces with QA, the UK’s largest B2B skills provider (read the announcement). Over the coming weeks, you will see additions from QA’s massive catalog of 500+ certification courses and 1500+ ins...

Read more
  • AWS
  • Azure
  • Cloud Academy
  • Cybersecurity
  • DevOps
  • Kubernetes
Avatar
Stuart Scott
— July 18, 2019

AWS Fundamentals: Understanding Compute, Storage, Database, Networking & Security

If you are just starting out on your journey toward mastering AWS cloud computing, then your first stop should be to understand the AWS fundamentals. This will enable you to get a solid foundation to then expand your knowledge across the entire AWS service catalog.   It can be both d...

Read more
  • AWS
  • Compute
  • Database
  • fundamentals
  • networking
  • Security
  • Storage
Avatar
Adam Hawkins
— July 17, 2019

How to Become a DevOps Engineer

The DevOps Handbook introduces DevOps as a framework for improving the process for converting a business hypothesis into a technology-enabled service that delivers value to the customer. This process is called the value stream. Accelerate finds that applying DevOps principles of flow, f...

Read more
  • AWS
  • AWS Certifications
  • DevOps
  • DevOps Foundation Certification
  • Engineer
  • Kubernetes
Avatar
Stuart Scott
— July 2, 2019

AWS Machine Learning Services

The speed at which machine learning (ML) is evolving within the cloud industry is exponentially growing, and public cloud providers such as AWS are releasing more and more services and feature updates to run in parallel with the trend and demand of this technology within organizations t...

Read more
  • Amazon Machine Learning
  • AWS
  • AWS re:Invent
  • Machine Learning
Avatar
Stuart Scott
— June 27, 2019

AWS Control Tower & VPC Traffic Mirroring

AWS re:Inforce 2019 is a two-day conference for security, identity, and compliance learning and community building. This year's keynote, presented by AWS Vice President and CIO, Stephen Schmidt, announced the general availability of AWS Control Tower and the new VPC Traffic Mirroring fe...

Read more
  • AWS
  • re:Inforce 2019
  • traffic mirroring
  • VPC
Avatar
Stuart Scott
— June 20, 2019

Working with AWS Networking & Amazon VPC

Being able to architect your own isolated segment of AWS is a simple process using VPCs; understanding how to architect its related networking components and connectivity architecture is key to making it a powerful service. Many services within Amazon Web Services (AWS) require you t...

Read more
  • AWS
  • VPC
Avatar
Stuart Scott
— June 19, 2019

AWS Compute Fundamentals Update

AWS is renowned for the rate at which it reinvents, revolutionizes, and meets customer demands and expectations through its continuous cycle of feature and service updates. With hundreds of updates a month, it can be difficult to stay on top of all the changes made available.   Here ...

Read more
  • AWS
Jeff Hyatt
Jeff Hyatt
— June 18, 2019

10 Steps for an Effective Reserved Instances Strategy

Amazon Web Services (AWS) offers three different ways to pay for EC2 Instances: On-Demand, Reserved Instances, and Spot Instances. This article will focus on effective strategies for purchasing Reserved Instances. While most of the major cloud platforms offer pre-pay and reservation dis...

Read more
  • AWS
  • EC2
Joe Nemer
Joe Nemer
— June 18, 2019

AWS Certification Practice Exam: What to Expect from Test Questions

If you’re building applications on the AWS cloud or looking to get started in cloud computing, certification is a way to build deep knowledge in key services unique to the AWS platform. AWS currently offers 11 certifications that cover major cloud roles including Solutions Architect, De...

Read more
  • AWS
  • AWS Certifications
Avatar
John Chell
— June 13, 2019

AWS Certified Solutions Architect Associate: A Study Guide

The AWS Solutions Architect - Associate Certification (or Sol Arch Associate for short) offers some clear benefits: Increases marketability to employers Provides solid credentials in a growing industry (with projected growth of as much as 70 percent in five years) Market anal...

Read more
  • AWS
  • AWS Certifications
Chris Gambino and Joe Niemiec
Chris Gambino and Joe Niemiec
— June 11, 2019

Moving Data to S3 with Apache NiFi

Moving data to the cloud is one of the cornerstones of any cloud migration. Apache NiFi is an open source tool that enables you to easily move and process data using a graphical user interface (GUI).  In this blog post, we will examine a simple way to move data to the cloud using NiFi c...

Read more
  • AWS
  • S3
Avatar
Chandan Patra
— June 11, 2019

Amazon DynamoDB: 10 Things You Should Know

Amazon DynamoDB is a managed NoSQL service with strong consistency and predictable performance that shields users from the complexities of manual setup. Whether or not you've actually used a NoSQL data store yourself, it's probably a good idea to make sure you fully understand the key ...

Read more
  • AWS
  • DynamoDB