[AWS CLI] Launch an EC2 instance and attach an EBS volume to it.

Bhupendra Singh Sisodiya
3 min readMar 22, 2021

Here is a step by step guide to how to launch an amazon image Instance using AWS CLI and attach a volume to it.

There a few things you need to get ready before using AWS CLI on your operating system.

  1. An IAM user account with required permission such as PowerUserAccess.
  2. AWS installed in the Operating System.

LAUNCHING AWS INSTANCE

STEP 1: Configure AWS and log to the user you need to Enter the Access key ID, Access key, region name, and output format (JUST PRESS ENTER IT WILL TAKE THE DEFAULT VALUE).

STEP 2: Next step is to create a key pair and note the “KeyName” and “KeyPairId”

aws ec2 create-key-pair — key-name <PutYourKeyNameHere>

and check the info using

aws ec2 describe-key-pairs

STEP 3: Now the next step is to create a security group

BELOW IMAGE SHOWS THE GENERAL SYNOPSIS

Now to create the security group use

aws ec2 create-security-group — group-name <EnterAnyName> — description “<YourDescription>”

Now I will add SSH access to the security group using the command

aws ec2 authorize-security-group-ingress — group-id sg-06af7b6d08b0c9cc3 — protocol tcp — port 22 — cidr 0.0.0.0/0

STEP 4: Next we will launch an instance

Here is the synopsis to launch an instance on AWS CLI

Use the below command to launch the instance

aws ec2 run-instances — image-id <Image ID e.g. ami-0e306788ff2473ccb> — count <countINT> — instance-type <InstanceType e.g. t2.micro> — key-name <YourKeyPairName> — security-group-ids <YourSecurityGroupID> — subnet-id <YourSubnet>

Hurray you’re done launching an instance on AWS using AWS CLI

ATTACHING VOLUME TO THE INSTANCE

Now we need to create a volume and attach it to the instance that we have created and attach the volume to the instance.

Here I will create a volume of 1 GiB and attach it to the instance ID

Here is the synopsis of the create volume command

STEP 1: To create a volume (I’m creating a volume of 1GiB in size)

aws ec2 create-volume — availability-zone <YOurAvailabilityZone> — volume-type gp2 — size <YourSizein INT>

STEP 2: attach the volume to the instance use command:

aws ec2 attach-volume — volume-id <YourVolumeID> — instance-id <YourInstanceID> — device /dev/sdf

And that’s all you’ve successfully attached the volume to the instance.

Keep Learning & Keep sharing

--

--