How to launch EC2 instance using AWS CLI

In AWS there are 3 ways we can connect to AWS

  1. using AWS Console (WebGUI/WebAPP)
  2. AWS SDK
  3. AWS CLI

AWS CLI is a command line interface & we can download latest version i.e. version 2 from AWS site. URL to download AWS CLI :

After downloading it we can verify the version as below

AWS CLI Version

In order to connect to AWS from AWS CLI we need to create user using IAM service.

IAM User

Now we can connect to AWS using above Access Key ID & Secret Access Key

AWS Configure command

In order to launch EC2 instance we’ll need following

-AMI (image id)
-Instance type
-Subnet id
-EBS Storage
-Security group
-key pair name

we’ll first create key-pair using below AWS CLI command

AWS Key-pair creation

In AWS Console we can verify its creation

AWS Key-pair creation

Now we need to create security group

Before that we need to get the subnet id using below command


Let’s say we need to launch instance in “SubnetId”: “subnet-b14d49d9”within “AvailabilityZone”: “ap-south-1a”

Pls note that VPC id is “VpcId”: “vpc-7666791e”

Security group can be created as under

Security Group creation

Verify Security Group in AWS Console as follows

Security Group Creation

Now we have all the details except AMI image id which we can get from AWS Console

AMI Image ID of Linux 2

Now we are all set to launch AWS EC2 instance.

Run the following command in CLI

aws ec2 run-instances — image-id ami-0e306788ff2473ccb — instance-type t2.micro — count 1 — subnet-id subnet-b14d49d9 — security-group-ids sg-0e56c19dc1bb20795 — key-name awscsa

It displays following

EC2 Instance Creation using AWS CLI command

Also, we can note that security group is attached to the instance which is currently in pending state

Security Group

We can confirm the details of running EC2 instance as follows

EC2 instance verification in AWS Console

We have to now create an EBS volume of size 1 GIB & attach to this instance.

EBS Volume is regional specific i.e. we need to create EBS volume in the same Availability Zone in which the instance is running. In our case Availability Zone is ap-south-1a

To create EBS volume from AWS CLI we need to fire below command

EBS Volume Creation

Pls note the volumeId which we require while attaching this volume to EC2 instance.

In AWS Console we can verify this volume as follows

EBS Volume creation

Now the next step is to attach this EBS volume to our EC2 instance.

Since above EBS volume is available we can attach to EC2 instance.

Before attaching the newly created EBS volume we can verify the storage of our EC2 instance as under

Storage of EC2 instance

The AWS CLI command to attach EBS Volume to EC2 instance is :

aws ec2 attach-volume — volume-id vol-031c3e1041c7d1573 — instance-id i-0d1c40a3e1a0e1c67 — device /dev/sdf

Attaching EBS volume to EC2 instance

Now , we can verify whether EBS volume has been attached to our instance using AWS Console as follows

EBS Volume in Use

We can see that EBS volume state has been changed from Available to In Use.

Let’s verify the storage of EC2 instance

EBS Volume attached to EC2 instance

Here , we can see that EBS volume has been successfully attached to our instance as a device name /dev/sdf

So, we saw how to create Key-pair, Security Group as well as launch EC2 Instance using AWS CLI commands.

Also , we were able to create an EBS Volume of size 1 GiB and attach to our Ec2 instance using AWS CLI commands.

So, this way we can create ’n’ number of Ec2 instances when there is a requirement to launch hundreds or even thousands of EC2 instances.

It saves a lot of time in such scenario as we don’t have to manually select all the options which we generally do while launching the instance in AWS Console.

Hope you like the article..!!




Cloud Professional experienced in BFSI ,Telecom, Insurance domain with fortune 500 clients spread across USA, Europe, Canada, Australia and India.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

On Readium CSS

CS50 2020 Pset 4: Recover!

Illegal Argument Exception in Java

Web Scrapping using Python

Create a Chatbot in Under 10 Minutes Using PubNub Functions

Customize a chart in Matplotlib with one example

Admin Notebook: How to be quick with kubectl 1.19

CKA Learning Series: Build K8s Cluster using Kubeadm in minutes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arvind Ramugade

Arvind Ramugade

Cloud Professional experienced in BFSI ,Telecom, Insurance domain with fortune 500 clients spread across USA, Europe, Canada, Australia and India.

More from Medium

Best Two Factor Authentication Plugins WordPress

Configuring Nginx Files and Directories

How I Fell In ❤️ With Automation

Installing an Apache Web Server on CentOS 8