Amazon CloudFormation Part 2: Creating a CloudFormation Template

The first part of this series introduced Amazon CloudFormation with a basic explanation of what this service is, the resources that this Amazon technology offers, and the general structure that a file should have to describe CloudFormation resources. In this second part, we are going to see how to define our first template and how to create this template from two sources: the Web CloudFormation Console and Amazon CLI.

For this first template, we will use one of the predefined templates that Amazon provides for application frameworks: the LAMP stack.


LAMP Stack

According to TechTarget, LAMP refers to a stack with four layers that has Linux, APACHE as the web server, MySQL as the relational database and PHP as the scripting language (this is the most common one, but you can also use Perl or Python instead). Some variations of this stack are: WAMP (Windows, Apache, MySQL, PHP), LAPP (Linux, Apache, PostgreSQL, PHP), MAMP (Mac OS X, Apache, MySQL, PHP) and XAMPP (Linux, Mac OS X, Windows, Apache, MySQL, PHP, Perl). Also, a more modern stack based on JavaScript that is currently widely used is the MEAN stack (MongoDB database, Express.js, AngularJS, Node JS) (Programmable Web).

From our infrastructure’s point of view, these variations don’t greatly affect the way we instantiate our resources on CloudFormation, because we design our template in terms of resources that run these technologies, OS, and databases.

For our first template, we are going to use this LAMP stack because its simplicity helps us concentrate on the main objective of this post: learning about CloudFormation.

Creating our First Template

For this example, we are going to use this template: LAMP stack. We will define each section of the template:

AWSVersion and Description Sections

The first two sections of this template are AWSTemplateFormatVersion and Description. As you remember from the first blog post, the only AWS version that is currently used for CloudFormation is “2010-09-09,” and the description allows us to understand what the template is for, which is especially important for future programmers and their improvement.

AWSTemplateFormatVersion and Description

Parameters Section

Here, we define some parameters that are needed to create a more generic template. First, we have KeyName; this parameter is the name of the key pair that will be used to connect to EC2 instances declared on this template (we will create a new key pair to be used on this template in the following section). DBName, DBUser, DBPassword and DBRootPassword are parameters that are used in the RDS instance (MySQL) that we will create. These parameters have several validations like MinLength, MaxLength, AllowedPattern and Type. The InstanceType parameter is the type of available EC2 instance that we want to define for our EC2 instance, and SSHLocation corresponds to the range of IP addresses that can access the EC2 instance with SSH (“” by default means any IP address can access this EC2 instance).


Mappings Section

In this section, we define some mappings that are used for some EC2 properties: AWSInstanceType2Arch is used to verify if the chosen InstanceType is a valid one; AWSRegionArch2AMI is used to verify if the region on the InstanceType and AWSInstanceType2NATArch is not used on this template.


Resources Section

For this LAMP stack, we just need to declare two resources: an EC2 instance that will have all LAMP elements (in this case, we have a local MySQL inside the EC2 instance, not an RDS instance) and a security group to allow HTTP access to the EC2 by port 80. First, for the EC2 instance called WebServerInstance, we declare several packages that will be installed (“Install” property inside the object), also inside the “files” property, we set the basic configuration for our initial PHP file and the initial configuration for a local MySQL. On the “Services” property, we declare the set of services that will be executed for MySQL and other services. In the “Configure” section, we define the commands to set the root password for our new MySQL instance, and we create the new database we are going to use in the stack. Inside “Properties,” we associate this EC2 instance with the security group we created for the EC2 and we update several Linux packages.

For the resource WebServerSecurityGroup, we define a couple of rules for the EC2 instance. For port 80, we give all IPs access to the EC2 instance, and for port 22, we set just the IP range we chose on SSHLocation.


Outputs Section

In this last section, the values that are described will be printed to the AWS console when the template is correctly created. We will see the URL for our new website.


Creating the Template on the AWS Web Console

Now that we have our template defined, we have to go to the AWS management console to create the infrastructure. The following image shows the AWS login page:

AWS login page


On the AWS main page, click on


Services menu / EC2 Console


“Services” and look for “EC2 instance” (we need to create a new key pair before the template creation). Inside the EC2 console, select the “Key Pairs” option on the left sidebar:

Click on the “Create Key Pair” button at the top of the page; after that, choose a name and select “Create.” Save the key pair as a .pem file (this format is used if you want to access the EC2 from your terminal):


Create the key pair / Save the key pair file

Create the key pair / Save the key pair file


After the key pair creation, search for the CloudFormation console inside Services; you will see the CloudFormation console main page. Click on “Create Stack”:


Go to the CloudFormation console / Click on “Create Stack”

Go to the CloudFormation console / Click on “Create Stack”


To create a new stack, you have to define the template you will use. Four options are available: “Design template,” “Select a sample template,” “Upload a template to an Amazon S3,” or “Specify an S3 URL.” After that, click “Next,” define the list of parameters that you want to use, and click “Next” again.


Select the template / Select the key pair and parameters

Select the template / Select the key pair and parameters


The next two pages will show some options that you can add to your template (not necessary in this example), so click “Next.” The last page is a “Review” page where you see all the values chosen for the template. Click “Create”:


Some options / Review of the parameters chosen for the template


Some options / Review of the parameters chosen for the template


Now the creation is in progress, so you can check the process with the tabs that are available (Overview, Resources, Events, Template, Parameters).


Overview / Resources

Overview / Resources


When the process is finally done, you can see the status CREATE_COMPLETE and the list of events that were done.



Creating the Template on AWS CLI

The process on CLI is pretty similar. You have to install AWS. After that, configure the AWS credentials in your console and execute the following command (just set stack name, template and parameters):


Credentials configuration

Credentials configurations



Template execution on AWS CLI on Mac OS

Template execution on AWS CLI on Mac OS





You will see the creation process was triggered by the CLI:


Creation triggered by AWS CLI

Creation triggered by AWS CLI


Now the process is done exactly as with the AWS Web console:





You can access the new URL printed to the “Outputs” section, and you will see your first LAMP working!


Index page

Index page


Now we know how to create a Cloudformation template and how to use tools provided by the Amazon console. In the next and final blog post, we are going to see some advanced features that Cloudformation provides us, such as change sets and nested stacks.



Subscribe to our Blog

Gaudy Blanco
Gaudy Blanco
Gaudy Blanco is one of Gorilla Logic’s talented software engineers. Gaudy graduated with a degree in Computer Science and English from the University of Costa Rica in 2016. Gaudy is interested in programming, web development, databases, and teaching. She enjoys learning new technologies.

Deliver off-the-chart results.

WordPress Video Lightbox Plugin