Setup Your First EC2 Creation Using CloudFormation

What is Cloud Formation

CloudFormation is one of the services provided by the AWS, which helps setup a Web Services resources. Which means less time is needed to setup a resource and concentrating on other Applications/services which runs across AWS.

Since I am using Windows platform, we need to install AWSCLI and I have explained in my previous article, Setup Your First S3 Bucket using CloudFormation.

To create an EC2 instance, we will be logging into UI of AWS, Select the availability zone, OS flavor we need and then we start our process which takes max of 5-10 mins

So Instead of that, just we can have a template pre-defined one where we only need to modify the few parameters like Image Flavors, Zone selection, KeyPairs and few more. And with a single command we could see our server is hosted ready.

So now, lets start with the creation of EC2 instance.

Creation of EC2 Instance

So, to create a EC2 instance using CloudFormation service, we have many number of parameter needs to configured right from Zone, ImageFlavour, Security group and many more.

In This Article, I will be covering all the important and Frequently used parameters only.

To know more about on kindly check out Link.

So, let’s start in detail,

Sample Template looks like,

Setup Your First EC2 Creation Using CloudFormation 

Now, we will step into Template Creation of simple EC2 instance and then will see how to execute it using AWS CLI.

Setup Your First EC2 Creation Using CloudFormation 
 
Setup Your First EC2 Creation Using CloudFormation 

Above is the simple code to create a EC2 instance. So now lets see the break of each parameter used:

  1. "KeyName": {  
  2.             "Description""Key Pair name",  
  3.             "Type""AWS::EC2::KeyPair::KeyName",  
  4.             "Default""DockerAutomation"  
  5.         },  

Description

Some detailed description about the Template

KeyName

Which defines the Key which we are going to define in our template

  1. "VPC": {  
  2.             "Type""AWS::EC2::VPC",  
  3.             "Properties":{  
  4.                 "CidrBlock""10.0.0.0/16",  
  5.                 "EnableDnsHostnames""true"  
  6.                   
  7.             }  
  8. "Subnet":{  
  9.             "Type""AWS::EC2::Subnet",  
  10.             "Properties": {  
  11.                 "VpcId": {"Ref""VPC"},  
  12.                 "CidrBlock""10.0.1.0/24",  
  13.                 "AvailabilityZone""us-east-1"  
  14.             }  

VPC

This defines under which VPC the EC2 instance should be created. Provide a valid CIDR block so that instance will be created.

Availability Zone

Mention about the zone. We have AWS Regions and End-Point which is specified in Link.
 
Setup Your First EC2 Creation Using CloudFormation 

  1. "InstanceType": {  
  2.             "Description""Select one of the possible instance types",  
  3.             "Type""String",  
  4.             "Default""t2.micro",  
  5.             "AllowedValues": ["t2.micro""t2.small""t2.medium"]  
  6.         }  

InstanceType

Mention about the Instance, which needs to be Initiated. Here, I have one more parameters "AllowedValues", which says only any one values needs to be substitued to default parameter.

  1. "Resources":{  
  2.         "SecurityGroup":{  
  3.             "Type""AWS::EC2::SecurityGroup",  
  4.             "Properties": {  
  5.                 "GroupDescription""CloudFormation",  
  6.                 "VpcId": {"Ref""VPC"},  
  7.                 "SecurityGroupIngress": [{  
  8.                     "CidrIp""0.0.0.0/0",  
  9.                     "FromPort": 22,  
  10.                     "IpProtocol""tcp",  
  11.                     "ToPort": 22  
  12.                 }]  
  13.             }  

Resources

Which defines under which VPC and security group the instance must be created.

  1. "Server": {  
  2.             "Type""AWS::EC2::Instance",  
  3.             "Properties": {  
  4.                 "ImageId""ami-0080e4c5bc078760e",  
  5.                 "InstanceType": {"Ref""InstanceType"},  
  6.                 "KeyName": {"Ref""KeyName"},  
  7.                 "SecurityGroupIds": [{"Ref""SecurityGroup"}],  
  8.                 "SubnetId": {"Ref""Subnet"}  
  9.             }         
  10. }  

Server

Mention about the type of Server (ID of Image). This can be taken from the page,
 
Setup Your First EC2 Creation Using CloudFormation

Once the command is success, we could see logs as ‘Stack created’ and From CloudFormation service we could confirm that the Instance creation process is initiated.

Setup Your First EC2 Creation Using CloudFormation
 
Setup Your First EC2 Creation Using CloudFormation 

Once the above command is success, you can able to check a EC2 Machine is created,

Setup Your First EC2 Creation Using CloudFormation 

Also, even if we execute a change set of errors in it, CloudFormation has Rollback Triggers that allows to monitor the stack created or updating process and rollback the environment to make to previous state.

What Next ….

Will be covering on Creating of VPC using CloudFormation.


Similar Articles