How to Launch an EC2 instance using Cloud Formation?

Share

Cloud Formation

AWS Cloud Formation is a Infrastructure as a Code tool, it means that you can create your cloud infrastructure with a code/syntax using cloud formation tool , you can use either json or yml templets where you declare which resource  or service you want to deploy , cloud formation will use these templets and will create the declared resources on the cloud ,you can  reuse this templets to deploy same infrastructure or make changes/upgrade  to already deployed one’s , In this blog we will learn how to launch a EC2 instance using cloud formation with a yaml templete.

Create a yaml file, something like server.yaml , server is the name of the yaml file I am creating , copy the below yaml syntax and save it, let’s understand it by going through each line

 

server.yaml

1  AWSTemplateFormatVersion: '2010-09-09'

2  Resources:
3   EC2Instance:
4     Type: AWS::EC2::Instance
5     Properties:
6       InstanceType: t2.micro
7       ImageId: "ami-0186e3fec9b0283ee"
8       SecurityGroups:
9         - !Ref InstanceSecurityGroup

10   InstanceSecurityGroup:
11     Type: AWS::EC2::SecurityGroup
12     Properties:
13       GroupDescription: Allow traffic on port 22(SSH) 
14       SecurityGroupIngress:
15         - IpProtocol: tcp
16           FromPort: 22
17           ToPort: 22
18           CidrIp: 0.0.0.0/0
Line-1: AWSTemplateFormatVersion: ‘2010-09-09’
here 2010-09-09 is the latest templet format version available, it indicates the capabilities of the templet, this is the only valid version available.
Line-2: Resources
you will declare AWS resources to be deployed and their configuration such as ec2 instance, s3 bucket, Kubernetes cluster

 

Line-3: EC2Instance it’s just a name that I gave for my reference or documentation and referring to it in the same yaml file, this will be reflected as a tag for the resource created, tags are useful in managing and searching your resources.

 

Line-4: Type: AWS::EC2::Instance
“Type” is the key word used to specify the resource type (like ec2 instance, load balancer, security group) that you want to deploy and “AWS::EC2::Instance” is the key word for ec2 instance, each resource will have a unique keyword you can get these keywords from AWS cloud formation  documentation.

Connect with our technical consultants





    Line 5-7: Properties , here you specify the configuration of resource to be deployed , as we are creating an ec2 instance properties like InstanceType and AMI ID(ImageId) are used , I am  using instance type t2.micro which will have 1 vCPUs , 1 GiB Memory  and  ImageId of Red Hat Enterprise Linux(ami-0186e3fec9b0283ee).

     

    Line 8,9:  if you want to use existing security group give the security group id after the key word SecurityGroups , but here we are creating a new security group  (from line 10-18) and and giving it’s details using Ref function , this function returns the value of the specified parameter or resource.

     

    you may be confused here that how a security group is attached after creating instance as we declared instance details first and then the security group creation details in the yaml file, cloud formation will identify  required  dependencies and  creates them  first only then it will start creating the main resource, In our case CloudFormation  will create security group first and than starts deploying ec2 instance.

     

    On your aws web console go to Cloud Formation and Click on create stack.

     

    Select “Template is ready ” for “Prepare template” and “upload a templet file”  under “Specify templet” , as you can see you can also use a templet stored in S3 bucket, after giving the file path if it’s in s3 bucket/uploading  the file if it’s in your local machine  , click on next.

     

    In step-2 give a name to your stack , then click on next.

     

    In Step-3  give tags if required ,and for the Permissions you can create a IAM role and give it here if you want to restrict Cloud formation from using all the permissions of the user that you have logged in on to the web console , for example if you are using Cloud formation from a root user account and you don’t want to allow deletion of ec2 instances  from cloud formation create a IAM role with permissions to only create  and modify ec2 resources.

    Note: Cloud formation can  modify , update or destroy resources which are created by cloud formation only.

    and for Stack failure options select Roll back all stack resources which means that you are instructing cloud formation to roll back all the resources created in case if it fails to create any one the resources that you have declared in the templet / yaml file  and the other option is Preserve successfully provisioned resources  , this option will keep the resources that are created successfully  even if fails to deploy whole stack .

    Leave remaining configuration options to default, scroll down and click on next

     

    In Step-4  review the configuration you have made and click on “create stack” ,  now you will be redirected to events page.

    Here you can see the Status of the resources deploying , as you can see in the above image even though I declared creation of security group after declaring the instance creation details in sever.yaml file  cloud formation is creating the security group first and than going on to the ec2 instance which implies that cloud formation will scan your templet file and creates the resources in order even thou you do not follow any order while  declaring them in your file.

    Now let’s go and verify  whether deployed EC2 instance uses mentioned AMI, security group and instance type(verify the configuration with server.yaml file that we have used to deploy resources).

    below are the snapshots of EC2 instance Launched using Cloud Formation and server.yaml file.

     

    Please contact our team for any offshore infrastructure management services website, LinkedIn

    Leave a Reply

    Your email address will not be published.

    *