Use AWS-CLI to create the EBS volume and attach to instance

Prajwal Patil
4 min readOct 13, 2020

In this article, I am gonna implement AWS-CLI to perform some task.

Task Description β†’

πŸ”… Create a key pair
πŸ”… Create a security group
πŸ”… Launch an instance using the above created key pair and security group.
πŸ”… Create an EBS volume of 1 GB.
πŸ”… The final step is to attach the above created EBS volume to the instance you created in the previous steps.

So Lets start:

Step1 β€” >

Create a IAM user in AWS console.

For that, Go to AWS console, then go on IAM Management Console

Locate the Users Tab navigate on it.

Click on Add User. Then in User Name field, add the name of user you want to add. And forgot to select the Programmatic access option. Click on next option.

For the User to be created, we need to set permissions for security reasons.

So search for PowerUserAccess.

Note: You Can also Give AdministratorAccess

The Tags part is optional,so i haven't added any tags.

So, Review the information on this page, then click on Create User option at bottom right corner.

Now you can see that, the new user is created and you can see Access and Secret keys.

Download the .csv file keys for future reference.

Step2 β†’

Download and Install AWS-CLI program. Click here to download.

Step3 β†’

Now using the keys obtained in Step1 login to AWS in CLI.

Use # aws configure command then add Access and Secret key with default region.

Now, as per Task we need to Create a Key-Pair.

Use command
# aws ec2 create-key-pair --key-name <key_name>

Now Create a Security Group.

aws ec2 create-security-group --group-name cliG1 --description cliT1

To know more details of Security Group,

# aws ec2 describe-security-groups

Launch a EC2 instance using above created Key-Pair and Security Group

# aws ec2 run-instances --image-id <AMI-Id> --instance-type t2.micro --count 1 --subnet-id <Subnet-Id> --security-group-ids <Security-Group-Id> --key-name <Key-Name>

Create EBS (Elastic Block Storage)

# aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone ap-south-1a

Now Final Step:

Attach the above created EBS Volume to EC2 instance using CLI.

# aws ec2 attach-volume --volume-id <EBS_Volume_ID> --instance-id <EC2_Instance_Id> --device /dev/sdf

We can see in WebUI of AWS, is it attached or not..

And ITS ATTACHED !!!

Also, Dont forgot to delete EBS and Instance after Use

Thanks For Reading :)

--

--