In the last post we got set up with a virtual network, sub-nets, routing and internet gateways needed to house applications.

In this post we’re going to add templates to the CloudFormation setup. It will start and run an Amazon Linux EC2 Instance in the network.

Warning: Building this stack will create an actual EC2 Instance and accrue charges on your AWS Account.

The full AppServer_Instance.template is shown below:

It looks like there is a lot in the template, but most of the length is in the mapping section.

The template was sourced from AWS Sample Templates. Let’s break down each section:

[title]Parameters[/title]

One of the best things about CloudFormation templates are the parameters, and not just putting in strings or numbers, but being able to use an AWS type as a parameter type.

In the the previous template, there were variables in the output. When using the CloudFormation UI to create and update stacks, you are presented with handy drop down menus to include AWS Resource Types as parameters.

Template validation will occur when using the types and managing templates via the API.

Users can take output from one template and use it as input for another template.

[title]Mappings[/title]

This section was not included in the previous template. It is a key-value lookup that can be utilized inside the template functions using Fn::FindInMap

[title]Resources[/title]

This is the section of the template that will be doing most of the hard work.

The properties are fairly self explanatory, but here is a break down:

  • Create an EC2 Instance
  • Lookup the Instance Type from Parameter InstanceType
  • Lookup SSH KeyName from parameter KeyName
  • Lookup the AMI Image Id from the mappings using the FindInMap functions
  • Configure NetworkInterface to: Associate Public IP, add to the Security Group, add to the Subnet

[title]Outputs[/title]

In this template we are outputting data and information to be used later.

For instance, PublicIP will allow us to SSH into the box once the stack has been created.

[title]Wrapping It Up[/title]

We now have a fresh Amazon Linux EC2 instance running in the network.

In the next post, you’ll learn how to automate the configuration of the instances to avoid manually typing a laundry list of commands every time.