Launching new EC2 Instance and attaching EBS volume to it using AWS CLI

Manas Srivastava
3 min readOct 13, 2020

For launching EC2 instances we will mandatory require AMI Image ID, Instance Type, Count, Key Name, and Security Group. So, we will create our Key and Security Group, and then after launching the instance will attach a volume to it.

Creating a Key-Pair

Command to create a new key pair is -

aws ec2 create-key-pair --key-name <value>

Here in the place of value, we have to write the name which we want to give to our Key Pair

After this, a key pair named “awstask” will be created in your AWS Account. For verifying you can check your AWS WebUI there one key with the name “awstask” will get created.

Creating a Security Group

The command for creating a new security group

aws ec2 create-security-group --group-name <value> --description <value>

In the place of <value>, you have to give a name to your Security group and a description

After running this command new Security Group will be created with Group Name- TaskGroup and Description — “ Arth Task Group”

Launching EC2 Instance

For launching a new EC2 instance we will require the Amazon Machine Image (AMI) ID. Here I am using Amazon Linux 2 AMI whose AMI ID is

ami-0e306788ff2473ccb

Instance type as t2.micro , number of instances 1 (count) , Key Pair and Security Group (which we created above)

aws ec2 run-instances — image-id ami-xxxxxxxx — count 1 — instance-type t2.micro — key-name <key name> — security-group-ids <sg id>

Creating EBS Volume

EBS volume is used as an external storage device like a pen drive. For creating a new volume we will require the name of the availability zone where we want to create our volume and the size of the volume

aws ec2 create-volume --size <int> --availability-zone <zone_name>

Attaching the Volume to the EC2 Instance

For attaching our volume to any instance we will require the device name, instance id, and the volume id

aws ec2 attach-volume --volume-id <value> --instance-id <value> --device <value>

Now your volume will get attached to the EC2 instance.

Hope this blog helped you in understanding the process of creating a new instance and attaching a volume to it.

--

--

Manas Srivastava

Bachelor of Technology (Computer Science and Engineering)