How to launch EC2 instance using AWS CloudFormation

AWS CloudFormation can launch and maintain the resources inside AWS using a standardised template format. It acts as Infrastructure as a code service where the template is written in standardized YAML or JSON format.

You can create templates in YAML or JSON format and describe your resources like ec2, rds, etc., and the configurations you want. CloudFormation takes care of provisioning and configuring the resources you don’t need to manually create and configure all the resources.

Visit AWS documentation for detailed information.

In this blog, We will so how to create an ec2 instance using a CloudFormation template.

Getting deeper into CloudFormation:

While working with CoudFormation we mainly work with templates and stacks so it’s important to understand the various terms related to CloudFormation.

Templates: CloudFormation template is a JSON OR YAML formatted text file. In this file, we can define our AWS resources and configurations. CloudFormation uses this template to provision resources.

Stacks: Using stacks we can manage a collection of resources under a single unit. For example, if you have written a template to create an EC2 instance, Auto scaling group and load balancer you need to create a stack under cloud formation and submit that template in order to provision resources. Also, you can simply delete, create, and update the stack by deleting the stack all the resources created under it will be automatically deleted.

Changeset: You can make changes to your stack by updating it so before making any changes to your stack you can generate a change set which shows the summary of the proposed changes that are going to happen

Now we are going to launch an ec2 instance using the CloudFormation template.

Here we are going to provison an ec2 instance we need an ssh-key to log in to the server.
We can define the ssh-key creation in our CloudFormation template. But it is a best practice to create an ssh-key manually from the ec2 console and provide the name in the template.

Step 1: Log in to your AWS account.

Step 2: Go to the CloudFormation template.

Step 3: Select on create a stack option.

Step 4: Now upload the below template and give a name to the stack.

Step 5: Proceed to the next steps and click on create stack option.

After the successful creation of the stack, you will be able to see the similar output in the event sections.

Also, I can see that my ec2 instance is created and also I am able to ssh into the server using the ssh-key pair MyKeyPair.

AWSTemplateFormatVersion: '2010-09-09'

  # Creates a new EC2 Security Group
    Type: AWS::EC2::SecurityGroup
      GroupName: EC2SecurityGroup
      GroupDescription: Security group for EC2 instance
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22

  # Creates a new EC2 Instance
    Type: AWS::EC2::Instance
      ImageId: ami-04706e771f950937f
      InstanceType: t2.micro
      KeyName: MyKeyPair
        - Ref: SecurityGroup
        - Key: Name
          Value: My EC2 Instance

This is a sample of a CloudFormation template. Which launches an ec2 instance using the ssh key “my-key-pair” defined.

This is the version of the cloud formation template used to provision resources.

Just a text to describe what is going to define in the code.

The Resources object contains a list of AWS resource objects.
A resource declaration contains the resource’s attributes, which are themselves declared as child objects.

Here we are defining 2 resources for our ec2 instance.

1: A security group that allows port 22 traffic:
2: An ec2 instance which uses the above 2 resources and provision instance based on the mentioned AMI id.

A resource must have a Type attribute, which defines the kind of Amazon resource you want to create. The Type attribute has a special format:

Resource declarations use a Properties attribute to specify the information used to create a resource.

This is just a basic template to launch an ec2 instance we can define more parameters like VPC, Elastic IP address volumes etc in the template for customising our instance

Summary: In this blog, we have learned how to create an ec2 instance using a cloud formation template.

Leave a Reply

Your email address will not be published. Required fields are marked *