Launching AWS Instance Using AWS CLI

Sathvika Kolisetty
4 min readOct 15, 2020

Problem Statement

  • Create a key pair
  • Create a security group
  • Launch an instance using the above-created key pair and security group.
  • Create an EBS volume of 1 GB.
  • The final step is to attach the above created EBS volume to the instance you created in the previous steps.

All the above steps must be performed using AWS CLI.

Before moving on to task we need to know some prerequisites about

What is AWS?

Amazon Web Services (AWS) is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis.

Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform, offering over 175 fully-featured services from data centres globally. Millions of customers — including the fastest-growing startups, largest enterprises, and leading government agencies — are using AWS to lower costs, become more agile, and innovate faster.

AWS Command Line Interface

The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

AWS EC2 Service

Amazon EC2 is a web service that provides resizable compute capacity in the cloud. Amazon EC2 reduces the time required to obtain and boot new user instances to minutes rather than in older days, if you need a server then you had to put a purchase order, and cabling is done to get a new server which is a very time-consuming process.


AWS EBS is a block storage service designed to be used exclusively with EC2 instances. It provides a high-performance option for many use cases, and it can be used for various databases (both relational and non-relational), for a wide range of applications, and for big data analytics.

Let us move on to the task

Step1 — Creating a key pair

A key pair, consisting of a private key and a public key, is a set of security credentials that you use to prove your identity when connecting to an instance. Amazon EC2 stores the public key, and you store the private key. You use the private key, instead of a password, to securely access your instances.

For creating the key-pair, use the following command:

aws ec2 create-key-pair --key-name mykey2201 --tag-specification ResourceType=key-pair,Tags=[{Key=Name,Value=mykey2201}]

For confirmation, we can use the following command

aws ec2 describe-key-pairs

Step 2 — Creating the security group

Creating the Security group for instance so our clients can access from other devices as the AWS has some default security setting for not allowing to connect from outside the host so there is a firewall which protects from outside from connecting

For confirmation, we can use the following command

aws ec2 describe-security-groups

Step3 — Launching Ec2 instance

Launching an instance with created key pair and security group and to connect into the instance

aws ec2 run-instances — image-id ami-0e306788ff2473ccb —- instance-type t2.micro -- count 1 -- subnet-id  subnet-d048239c — security-group-ids sg-0186840bb87671260 -— key-name mykey2201

We can see that the instance is launched

Step 5 — Creating the EBS volume

To create the EBS volume, you can make use of the create-volume command available in AWS CLI. Along with this command, you need to provide the availability zone that you want to use.

aws ec2 create-volume --availability-zone ap-south-1b --size 1 --volume-type gp2 --tag-specification ResourceType=volume,Tags=[{Key=Name,Value=clivolume}]

Step 6 — Attaching that EBS Volume to the EC2 instance

For attaching the volume, you need the device name and instance-id to which you need to attach the volume and obviously the volume-id of the volume that is to be attached to the instance.

aws ec2 attach-volume — instance-id i-01789fea4bff6b69 — volume-id vol-034b841ea640cd02e — device /dev/sdf

Thank you for reading...