Launching of Key-pair, security group, EC2 instance and attach extra volume using AWS CLI

Anubhav Singh
8 min readOct 18, 2020
AWS CLI

In this post, we are going to create a key-pair, a security group, an EC2 instance and an extra EBS volume of 1 GiB is attach to that. This whole setup we are going to attach over the AWS using AWS CLI.

On AWS, we have multiple ways to configure like WebUI, CLI, and SDK. WebUI is going through directly on the AWS website and for the CLI we can use of windows command prompt or any of the terminal of your Linux or macOS.

Before going further to provision the system let’s know what is AWS?

AWS

Amazon Web Services(AWS) is a public cloud platform that offers computing power, database storage, and content delivery like 175 services across the world through their Datacenters(formally known as Availability Zones).

It required no developer knowledge, there are tonnes of great services that are ready to use in just a single click. You have to just create an account and demand the service that you want AWS is ready to give that for you. To know more about AWS you can go through this post What is AWS and how it is useful for us?

How to install AWS CLI?

Now, Let’s go to the setup part. To use the CLI we have to download AWS CLI and you can download that by clicking here. I am using the 64-bit Windows operating system and this link will provide you the same but in the case of any other system, you can simply search your requirements you can easily get that.

You will also find some pre-requisites and a pattern to install the CLI but in case you have some problem regarding the installation process here is my blog that can help you better.

Pre-requisites

There is one more pre-requisite. While connecting your CLI to your AWS account you will need some process to authenticate your account. We have some ways to authenticate like providing usernames and passwords. But in real use cases to improve our security we use a key to login and in CLI we use the key to authenticate.

How to get the key?

In order to get the key, AWS has a service called IAM. In IAM, you can create a user and provide some power to him once you create the IAM user download the user-credential file. This user-credential file will have an Access Key and a Secret key. The access key is similar, like a username and the secret key is like a password.

Note: In case of facing any issue while creating the IAM user you can take help from this CLICK HERE.

Now, You have AWS CLI installed in your system and you have a user. Let’s authenticate the CLI and start our task.

aws configure is the command that will help you to provide your credential.

Once the authentication is confirmed you are good to go with your task.

Task Description

Create the AWS commands to do all below task

  • Create a key-pair.
  • Create a security group.
  • Create an EC2 instance using the above-created key and security group.
  • Create an EBS volume of 1 GiB.
  • Attach that extra volume to our instance.

Create a Key-pair

These commands are of one or two lines and after performing the task I know that and can directly write that here. But I am not going to do that we will follow a process to find our command. It may feel you lengthy once but when you will create successfully you will get the approach. After getting the approach you will create any command.

We know that AWS has its original command aws and to go through any command just use help

aws help
aws help command output on windows command prompt
AWS help

Here, you will find a detailed note of the manual for using the AWS CLI.

Our task is to create the key pair. When you will go through AWS WebUI to create the key pairs, it is inside the EC2 dashboard. So, we have to first search for the EC2 command. You can use the enter key to go down and down.

aws help command for ec2
AWS EC2 command

Now you got that you can use aws ec2, for further query again use help.

aws ec2 help

Copy the text and add this to your previous command. Your command is

aws ec2 create-key-pair

Now to create we have to provide some details and to know that you can again use help

aws ec2 create-key-pair help

Whatever in the red box that is important to provide and whatever in the square bracket is not as such necessary. You can use these as per your choice.

Finally, here is our first command

aws ec2 create-key-pair --key-name "arthtask3"

Create a Security Group

Again we will follow the same procedure creating this command as well. We only have knowledge about aws command and using help we will create our command.

The security group also belongs to ec2, how to know this? you can directly go through the AWS website and check that in services.

create a security group

You got this and now again query help to know, exact what are the details required to create a security group.

aws ec2 create-security-group help

A description and a group name is required. To know detail about the keyword you can go through more deep. You will find a detailed discussion of the keywords used.

Our final command becomes

aws ec2 create-security-group --description "Security group for Arth Task 3" --group-name "ArthTask3"

Create an EC2 instance using the above-created key and security group

To create an instance we will further check for instance command in EC2. aws ec2 help.

We got our command. Let’s find the required details.

aws ec2 run instances help

Here a lot of detsails are there so to find the required details we will use WebUI to gather the required details. I am not showing the WebUI now, hope you guys will find on your own. When you will go to check you will following these things are required to launch the instance.

image id, instance type, key-name, security-group-ids(as we have the ID of our sg)

You can also provide your subnet ID in case if not given it will launch in the default subnet. Our command becomes

aws ec2 run instances --image-id <image-id> --instance-type <instance-type> --key-name <key-name> --security-group-ids <group-id> 
ec2 instance launch using command prompt

Create an EBS volume of 1 GiB

First, we have to create a volume of 1 GiB. Let’s search for the command that we need to implement.

Creating a volume is again a sub-service inside the EC2 service and hence we will use the command aws ec2 help . Again you will find create-volume as a command

Again use aws ec2 create-volume help .

Availability Zone is an important factor that we have to decide and we also want the size of 1 GiB. So, we will use two sub-commands

aws ec2 create-volume --availibility-zone <availability zone> --size <size>

What to provide in the size? Not sure? Read the detailed discussion of below

Here size is in integer format and that in GiBs.

Let’s put the value of volume size is 1 and run the command. It is necessary to provide the same availability zone as your instance has because EBS is a regional service and it can only be attached to the same region instances.

Finally, use the command to create the volume.

create volume command

Attach that extra volume to our instance

Now we are required to attach the volume that we have created in our above command. Let’s find the command to attach the volume, run aws ec2 help .

attach-volume is the command that will help us in attaching that

EBS volume creation

Let’s find the required details to attach the volume. In the below image, we can see that we are required three details to attach the volume i.e. device, instance ID, volume ID.

EBS Volume attach help

We have instance ID, we have volume ID, now all we need to find the device name. In the same picture, you can get the device will be in string format and above there is an example of that as well.

We can find this on web UI as well. Inside the storage section of instance details, you will find the device name /dev/xvda.

While creating the instance there is one device already connected at /dev/xvda and this ranges from /dev/xvda to /dev/xvdf. I changed a to b in this case and attached my volume. In the below case, you can clearly check I was not able to use the same device again.

EBS volume attached

In the first case, we get an error of Attachment point /dev/xvda is already in use.

After changing that I was able to do that.

Hurrah! We have successfully completed our whole task but let’s check the output in the website.

Here is our key pair

Our security group

Instance

EBS volume before attached. It is showing it is available to use.

After attaching the volume to the instance.

Congratulations, We don’t know about any command of AWS. All we created our own command and do this setup.

Hope you guys learn something today.

Thank you!

--

--

Anubhav Singh

Computer Science & Engineering | Programmer and Developer | Blogger