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

Avatar
Jeremy Cook
— September 17, 2019

Cloud Migration Risks & Benefits

If you’re like most businesses, you already have at least one workload running in the cloud. However, that doesn’t mean that cloud migration is right for everyone. While cloud environments are generally scalable, reliable, and highly available, those won’t be the only considerations dri...

Read more
  • AWS
  • Azure
  • Cloud Migration
Joe Nemer
Joe Nemer
— September 12, 2019

Real-Time Application Monitoring with Amazon Kinesis

Amazon Kinesis is a real-time data streaming service that makes it easy to collect, process, and analyze data so you can get quick insights and react as fast as possible to new information.  With Amazon Kinesis you can ingest real-time data such as application logs, website clickstre...

Read more
  • amazon kinesis
  • AWS
  • Stream Analytics
  • Streaming data
Joe Nemer
Joe Nemer
— September 6, 2019

Google Cloud Functions vs. AWS Lambda: The Fight for Serverless Cloud Domination

Serverless computing: What is it and why is it important? A quick background The general concept of serverless computing was introduced to the market by Amazon Web Services (AWS) around 2014 with the release of AWS Lambda. As we know, cloud computing has made it possible for users to ...

Read more
  • AWS
  • Azure
  • Google Cloud Platform
Joe Nemer
Joe Nemer
— September 3, 2019

Google Vision vs. Amazon Rekognition: A Vendor-Neutral Comparison

Google Cloud Vision and Amazon Rekognition offer a broad spectrum of solutions, some of which are comparable in terms of functional details, quality, performance, and costs. This post is a fact-based comparative analysis on Google Vision vs. Amazon Rekognition and will focus on the tech...

Read more
  • Amazon Rekognition
  • AWS
  • Google Cloud Platform
  • Google Vision
Alisha Reyes
Alisha Reyes
— August 30, 2019

New on Cloud Academy: CISSP, AWS, Azure, & DevOps Labs, Python for Beginners, and more…

As Hurricane Dorian intensifies, it looks like Floridians across the entire state might have to hunker down for another big one. If you've gone through a hurricane, you know that preparing for one is no joke. You'll need a survival kit with plenty of water, flashlights, batteries, and n...

Read more
  • AWS
  • Azure
  • Google Cloud Platform
  • New content
  • Product Feature
  • Python programming
Joe Nemer
Joe Nemer
— August 27, 2019

Amazon Route 53: Why You Should Consider DNS Migration

What Amazon Route 53 brings to the DNS table Amazon Route 53 is a highly available and scalable Domain Name System (DNS) service offered by AWS. It is named by the TCP or UDP port 53, which is where DNS server requests are addressed. Like any DNS service, Route 53 handles domain regist...

Read more
  • Amazon
  • AWS
  • Cloud Migration
  • DNS
  • Route 53
Alisha Reyes
Alisha Reyes
— August 22, 2019

How to Unlock Complimentary Access to Cloud Academy

Are you looking to get trained or certified on AWS, Azure, Google Cloud Platform, DevOps, Cloud Security, Python, Java, or another technical skill? Then you'll want to mark your calendars for August 23, 2019. Starting Friday at 12:00 a.m. PDT (3:00 a.m. EDT), Cloud Academy is offering c...

Read more
  • AWS
  • Azure
  • cloud academy content
  • complimentary access
  • GCP
  • on the house
Avatar
Michael Sheehy
— August 19, 2019

What Exactly Is a Cloud Architect and How Do You Become One?

One of the buzzwords surrounding the cloud that I'm sure you've heard is "Cloud Architect." In this article, I will outline my understanding of what a cloud architect does and I'll analyze the skills and certifications necessary to become one. I will also list some of the types of jobs ...

Read more
  • AWS
  • Cloud Computing
Avatar
Nitheesh Poojary
— August 19, 2019

Boto: Using Python to Automate AWS Services

Boto allows you to write scripts to automate things like starting AWS EC2 instances Boto is a Python package that provides programmatic connectivity to Amazon Web Services (AWS). AWS offers a range of services for dynamically scaling servers including the core compute service, Elastic...

Read more
  • Automated AWS Services
  • AWS
  • Boto
  • Python
Avatar
Andrew Larkin
— August 13, 2019

Content Roadmap: AZ-500, ITIL 4, MS-100, Google Cloud Associate Engineer, and More

Last month, Cloud Academy joined forces with QA, the UK’s largest B2B skills provider, and it put us in an excellent position to solve a massive skills gap problem. As a result of this collaboration, you will see our training library grow with additions from QA’s massive catalog of 500+...

Read more
  • AWS
  • Azure
  • content roadmap
  • Google Cloud Platform
Avatar
Adam Hawkins
— August 9, 2019

DevSecOps: How to Secure DevOps Environments

Security has been a friction point when discussing DevOps. This stems from the assumption that DevOps teams move too fast to handle security concerns. This makes sense if Information Security (InfoSec) is separate from the DevOps value stream, or if development velocity exceeds the band...

Read more
  • AWS
  • cloud security
  • DevOps
  • DevSecOps
  • Security
Avatar
Stefano Giacone
— August 8, 2019

Test Your Cloud Knowledge on AWS, Azure, or Google Cloud Platform

Cloud skills are in demand | In today's digital era, employers are constantly seeking skilled professionals with working knowledge of AWS, Azure, and Google Cloud Platform. According to the 2019 Trends in Cloud Transformation report by 451 Research: Business and IT transformations re...

Read more
  • AWS
  • Cloud skills
  • Google Cloud
  • Microsoft Azure