How to create AWS EC2 instance using Cloudformation

How to create AWS EC2 instance using Cloudformation

On this post we will describe how to use CloudFormation to automate the creation of EC2 instances. If you have read our post about creating EC2 instances, you know here at Austral Tech we like to automate everything, in our post for creating EC2 instances we used AWS CLI. On this tutorial we will show you how to do the same using Amazon CloudFormation.

What is CloudFormation?

CloudFormation is a tool from AWS that allows you to spin up resources effortlessly. You define all the resources you want AWS to spin up in a file, click a button, and then AWS creates it all. This blueprint is called a template in CloudFormation.

A nice thing about CloudFormation is that it makes sure that dependent resources in your template are all created in the proper order. For example, let’s say we want to create a DNS Route53 record and a EC2 instance having the DNS record point to the EC2 instance. CloudFormation will take care to provision the EC2 instance first, wait for that to be ready, and then create the DNS record afterwards.

Basic CloudFormation Example

Let’s go through a simple example of launching a CloudFormation stack. We are going to spin up a EC2 instance and a Security Group.

Instead of starting with an empty CloudFormation template, grab a starter template from the AWS Documentation. The simple one we want is Amazon EC2 instance in a security group.

Here’s a short explanation of what each means:

  • AWSTemplateFormatVersion: Specifies the AWS CloudFormation template version.
  • Description: A text string that describes the template.
  • Mappings: A mapping of keys and associated values that you can use to specify conditional parameter values. This is CloudFormation’s version of a “case” statement.
  • Outputs: Describes the values that are returned whenever you view your stack’s properties. This gets displayed in the AWS CloudFormation Console.
  • Parameters: Specifies values that you can pass in to your template at runtime.
  • Resources: Specifies the stack resources and their properties, like our EC2 instance. This is the only required property.

The most important top-level properties of a CloudFormation template are Parameters and Resources. The resources section is where our EC2 instance is defined:

"Resources" : {
     "EC2Instance" : {
       "Type" : "AWS::EC2::Instance",
       "Properties" : {
         "InstanceType" : { "Ref" : "InstanceType" },
         "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
         "KeyName" : { "Ref" : "KeyName" },
         "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },
                           { "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] }

This EC2Instance resource demonstrates a couple of uses of Ref. Ref is a way to reference values from other parts of the template. For example, Ref: InstanceSecurityGroup refers to the only other resource in this template, the SecurityGroup to be created. Here’s the definition of that resource:

Ref: InstanceType also refers to the InstanceType parameter that can be passed in. The Parameters top-level section is where the InstanceType parameter comes from. Let’s take a look at that part of the Parameters section.

From the template itself You can see that the default parameter for the EC2 Instance type to launch is t2.small. You can override this value when you launch the instance if you would like. For parameters with default values, you do not need to provide the parameter. For parameters without default values, you will need to provide the parameter. In this specific template, the only required parameter is the KeyName. The KeyName is the ssh key use to access the instance.

Launching the Stack

Enough talk, let’s finally launch the stack!

aws cloudformation create-stack --template-body file://EC2InstanceWithSecurityGroupSample.template --stack-name single-instance --parameters ParameterKey=KeyName,ParameterValue=east-key ParameterKey=InstanceType,ParameterValue=t2.micro


     "StackId": "arn:aws:cloudformation:us-east-1:772378070873:stack/single-instance/f200ec50-6b60-11e9-91c2-12dffdf25cac"

Now, we can see the EC2 Instance being created in the AWS Management Console

EC2 Instance being created in the AWS Management Console

We can also check the Stack creation, remember we have created the EC2 instance and the security group, so if we want to see all the Events, go to the CloudFormation service on AWS Management Console, you should see something like this:

AWS Management Console

If you are done with the instance you can just delete the stack from the Console itself or running:

aws cloudformation delete-stack --stack-name single-instance

The AWS Console for CloudFormation will show you the following:

AWS Console for CloudFormation