Launching a complete Instance and Mount a EBS volume by CLI in AWS

Kunal Jaiswal
6 min readOct 19, 2020

Task which we are going to perform through the AWS CLI

1.) Create a key pair

2.) Create a security group

3.) Launch an instance using the above created key pair and security group.

4.) Create an EBS volume of 1 GB.

5.) The final step is to attach the above created EBS volume to the instance you created in the previous steps.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Introduction about some important terms

👉 What is AWS?

Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform, offering over 175 fully featured services from data centers 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.

👉 What is EC2?

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable computing capacity — literally, servers in Amazon’s data centers — that you use to build and host your software systems.

👉 What is 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.

👉 What is security-group?

A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Inbound rules control the incoming traffic to your instance, and outbound rules control the outgoing traffic from your instance. When you launch an instance, you can specify one or more security group.

👉 What is EC2 instances?

An EC2 instance is a virtual server in Amazon’s Elastic Compute Cloud (EC2) for running applications on the AWS infrastructure.

👉 What is EBS volume?

EBS is stands for Elastic Block Storage. An Amazon EBS volume is a durable, block-level storage device that you can attach to your instances. After you attach a volume to an instance, you can use it as you would use a physical hard drive. EBS volumes are flexible.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Let’s do some practical.

Prerequisite for this task are:

1.) You need to sure that you already install the AWS CLI software/program in your system/workplace. AWS CLI provides us the interference between the AWS and the CLI means terminal.

If not then then install the software by the below link. 👇👇

https://awscli.amazonaws.com/AWSCLIV2.msi

Now, you can check whether AWS CLI install or not then run the below command 👇👇

aws 

2.) After installing the AWS CLI successfully then first you required to make a IAM (Identity and Access Management) means a user who are going to access the AWS.

↪ For this you need to enter the AWS account with the root account.

↪ Go to the IAM service.

↪ Create user by clicking on the Add User

↪ Give the user name and tick the programmatic access which make the access key ID and the secret access key for login through the AWS CLI.

↪ Now, you add the user permissions which you want the user access. In my case I give the AdministratorAccess.

↪ Now, you can give the tags which is optional and then you can review.

↪ After review you can create a user by clicking on the create user button.

↪ Now, your IAM is successfully created. You can save the access key and secrete access key clicking on the download.csv

3.) After this you run the command in your cmd👇👇

aws configure

To configure the access key and secrete key to login into the AWS. give the access key and secret key which we download previously.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Now, let’s come to our task.

Step 1 :- Create a key pair. To create the key pair run the below command.

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

Step 2 :- Create a security group.

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

Step 3 :- Launch an instance using the above created key pair and security group. For this you need the image-id, instance-type, subnet-id, security group id and the key pair name. You can got it by the AWS console.

aws ec2 run-instances --image-id <value> --instance-type <value> --count <value> --subnet-id <value> --security-group-ids <value>      --key-name <name> 
Successfully launch the instance

Step 4 :- Create an EBS volume of 1 GB.

aws ec2 create-volume --availability-zone <value> --size <value> 
Successfully create a EBS volume

Step 5 :- The final step is to attach the above created EBS volume to the instance you created in the previous steps. For this you need volume id which is created in the step 4, instance id which is created in the step 3 and device name.

aws ec2 attach-volume --volume-id <value> --instance-id <value> --device /dev/sdf
This EBS volume is in use.
Successfully attach the volume with the instance

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

So, our task is successfully done✌✌

Thanks for learning. If you have any suggestion or any query then feel free for suggest and ask.

--

--