How to launch an instance and attach the volume through CLI

Manojnagabairu
3 min readOct 29, 2020

Actually launching an instance is a piece of cake in the graphical user interface but when we come to the command line interface you experience there are some of the additional things that we have to follow in our process.

So let’s get started

  • In the first step when we want to use the command-line interface for creating anything in the amazon web services. For this, we need to create an IAM user by which we will get the access ID and secret key to configure our AWS in the command line interface.

Then configure the AWS CLI program which gives us the interface to work in CLI.

This is the link for downloading the CLI interface of AWS https://awscli.amazonaws.com/AWSCLIV2.msi

Then configure the AWS with the access ID and secret key

By these commands the key pair is created and also the key pair is downloaded to your local hard disk

>> aws ec2 create-key-pair — key-name manojcli — output-text > manojcli.pem

Now we have to create a security group for our instance in the following way:-

>> aws ec2 create-security-group — group-name manojcli— vpc-id”vpc-1228c879" — description “second sg”

After creating the security group we can start creating an instance in the following way:-

>> aws ec2 run-instances — image-id ami-0e306788ff2473ccb — instance-type t2.micro — key-name manojcli — security-group-ids “sg-004570854bc19bd80” — subnet-id subnet-c6c0bf8a — count 1

After creating an instance we should create a volume in the elastic block service (EBS) to attach with the created instance.

creation of volume:-

>>aws ec2 create-volume — volume-type gp2 — availability-zone ap-south-1b — size 1

After creating the volume attach it to the instance that we created before, by the command:-

>>aws ec2 attach-volume — device /dev/xvds — instance-id “i-08ad8509542bb370f” — volume-id “vol-00d1191fd966ba83f”

In this way, we can create an instance and volume and attach the volume to the created instance.

Thank you for coming to this blog and for reading this 😊

--

--