Image for post
Image for post

Launching an Instance and attaching with EBS Volume Using AWS CLI

Sachin Kumar
Oct 15, 2020 · 9 min read

Let’s learn how to work on AWS using AWS CLI.

Before Start the practical part, I want you to have some little knowledge of the AWS Cloud platform so that you can easily relate its practical part. That’s why I am writing the introduction part of AWS.

Introduction Of AWS

Amazon Web Services (AWS) is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis. These cloud computing web services provide a variety of basic abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet. AWS’s version of virtual computers emulates most of the attributes of a real computer, including hardware central processing units (CPUs) and graphics processing units (GPUs) for processing; local/RAM memory; hard-disk/SSD storage; a choice of operating systems; networking; and pre-loaded application software such as web servers, databases, and customer relationship management (CRM).

The AWS technology is implemented at server farms throughout the world and maintained by the Amazon subsidiary. Fees are based on a combination of usage (known as a “Pay-as-you-go” model), hardware, operating system, software, or networking features chosen by the subscriber required availability, redundancy, security, and service options. Subscribers can pay for a single virtual AWS computer, a dedicated physical computer, or clusters of either. As part of the subscription agreement,[6] Amazon provides security for subscribers’ systems. AWS operates from many global geographical regions including 6 in North America.[7]

Amazon markets AWS to subscribers as a way of obtaining large scale computing capacity more quickly and cheaply than building an actual physical server farm.[8] All services are billed based on usage, but each service measures usage in varying ways. As of 2017, AWS owns a dominant 33% of all cloud (IaaS, PaaS) while the next two competitors Microsoft and Google have 18%, 9% respectively according to Synergy Group.[9][10]

TASK DESCRIPTION:

Now, We will see the solution to the following problem statements in this article.

  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 created in the previous steps.

Prerequisites: To Perform this task, we will need the following two prerequisites.

  1. AWS Account
  2. AWS CLI Application.

We have three ways to do work on the AWS platform i.e WebUI, CLI, and SDK. but here we will use the AWS CLI application, So before starting the practical part. You should know about AWS CLI. that’s why I am writing some introduction part of AWS CLI.

AWS Command Line Interface

The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

How to create an IAM Account through AWS Root Account.

To create an IAM User Account, you should have an AWS Root Account. If you have not. then please follow the AWS documentation to create an AWS Root Account. So Now let’s jump on the creation of an IAM User Account.

Step 1.1: First of all you have to log in with Root AWS Account after then click on “Services.

Image for post
Image for post
Click on Services

Step 1.2: Search “IAM on the search bar then press the enter key.

Image for post
Image for post
Search IAM at Search Bar

Step 1.3: Click on “Users.

Image for post
Image for post
Click on Users

Step 1. 4: Click on “ Add user”.

Image for post
Image for post
Click on Add user

Step 1.5: Give your User name whatever you want and Select the highlighted Steps. In the last, Click on “ Next: permission”.

Note: Here, I am selecting options according to my requirement but you can also choose as you want.

Image for post
Image for post
Image for post
Image for post
Give the permissions

Step 1.6: Click on “add user to group”.

Image for post
Image for post
Click on Add user to group

Step 1.7: Give the name to your group and select the policy name which you want. but here, I am selecting “PowerUserAccess” permission. So that I can perform this task smoothly. After then click on “Create group”.

Image for post
Image for post
Search group which you want to give

Step 1.8: Click on “Attach”.

Image for post
Image for post
Click on Attach

Step 1.9: This part is optional, so I’m not giving any tags but you can give any tags according to your need. After then click on “Next Review”.

Image for post
Image for post
Click on Next: Review

Step 1.10: Review your user account and click on “Create user”.

Image for post
Image for post
Click on Create: user

Step 1.11: Now, your IAM account is ready so you can log in with this account using either CLI or WebUI. But I will show you both the methods of login in the next section according to the task’s requirement.

Note: Download yours .csv file. So when you will log in through CLI that time you will need this file.

Image for post
Image for post
IAM account is ready

How to install AWS CLI in your system.

After creating the IAM account, you will need to install the AWS CLI in your system. Here I am using the Linux operating system so I have to AWS CLI for the Linux system But if you are using Windows or Mac operating system then you have to install AWS CLI according to your need. So let’s jump to the installation part of AWS CLI in Linux OS.

Prerequisites for Linux

  • You must be able to extract or “unzip” the downloaded package. If your operating system doesn’t have the built-in unzip command, use an equivalent.
  • The AWS CLI version 2 uses glibc, groff, and less. These are included by default in most major distributions of Linux.
  • We support the AWS CLI version 2 on 64-bit versions of recent distributions of CentOS, Fedora, Ubuntu, Amazon Linux 1, and Amazon Linux 2.
  • We support the AWS CLI version 2 on Linux ARM.
  • Because AWS doesn’t maintain third-party repositories, we can’t guarantee that they contain the latest version of the AWS CLI.

Install the AWS CLI version 2 on Linux.

Step 2.1: Run the below command to download a zip file od AWS CLI

curl “https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o “awscliv2.zip”

Image for post
Image for post
The download zip file of AWS CLI

Step 2.2: Run the below command to unzip your file.

unzip awscliv2.zip

Image for post
Image for post
unzip AWS CLI

Step 2.3: Run the below command to install CLI.
sudo ./aws/install

Image for post
Image for post
Install AWS CLI

Step 2.4: Check AWS CLI is installed or not using the below command.

Image for post
Image for post
Check the CLI is Installed or not
AWS CLI Installation Commands

Now AWS CLI has installed in the system if your system is showing the above output.

How to login with AWS Account through CLI

To perform the above task assigned, we need to configure the IAM — User profile that I have already created earlier. For the first time, we need to run this command. The command is as follows:

Image for post
Image for post
log in with your IAM Account

Here, we need to add the Access-key and Secret-key provided by AWS while creating this IAM User (see step 11.1). Also, along with this, we need to mention the default output format and the region. Here, the region I am using is Mumbai i.e. ap-south-1.

Now, you have logged in to your IAM Account, and also “aws help” is a command which will help you to find out all commands of aws commands according to your requirement.

Image for post
Image for post
aws ec2 help

Step 3.1: Create a key pair using the below command.

Note: Here, “ mykey” is the key name which will use when you will launch the instance so remember this name and also give this name according to your comfort.

Image for post
Image for post
aws ec2 create-key-pair — key-name mykey

Here, I am showing you that my key pair is created or not. So to show the output, I have to log in with my IAM account that’s why I am showing you how to log in with the IAM account. To log in with IAM Account you will need to AWS Account ID, USER_NAME and PASSWORD. So you have to take the account id from your Root Account and USER_NAME and PASSWORD have to use which you gave during the creation of the IAM Account.

Step 3.2: Copy the Account Id from your Root Account.

Image for post
Image for post
copy your account id

Step 3.3: Give your username and password with Account Id.

Image for post
Image for post
Give your credentials

Now, you have logged in with your IAM Account.

Image for post
Image for post
A dashboard of AWS IAM

Step 3.4: Click on Services and select EC2.

Image for post
Image for post
Click on EC2

Step 3.5: Click on the “EC2 Dashboard” and select “key pair”.

Image for post
Image for post
click on key pair

Now, you can see the key pair which I created.

Image for post
Image for post
Key pair

Step 3.6: Create a security group using the below command. and write down the security group id because you will need it during the launching of the instance.

Image for post
Image for post
create your security group

Again, I am showing you our security group is created or not. So checking the security you have to open the EC2 dashboard the click on the “security group”.

Image for post
Image for post
Click on security group

Output: Now, you can see my created group is existing.

Image for post
Image for post

Step 3.7: Launch the instance using the below command.

Image for post
Image for post
launch your instance

Output: Here, you can see the all details of your instance. Also, write down the highlighted part because you will need it when you will attach your volume with your instance.

Image for post
Image for post
Instance output in CLI Form

Also, you can check through WebUI that it is existing or not.

Image for post
Image for post
Instance output in WebUI Form

Step 3.8: Create the Volume using the below command and write down “VolumeId”.

Image for post
Image for post
Create an EBS Volume

Output: you can see my volume is ready.

Image for post
Image for post
the output of EBS Volume

Optional: you can take your instance id through WebUI also.

Image for post
Image for post
Instance id

Step 3.9: Attach the EBS Volume with your instance using the below command.

Image for post
Image for post
Attach the EBS Volume with Instance

Output: Now you can check volume has been attached with your instance.

Image for post
Image for post
the final output of this task
AWS CLI Commands

Conclusion

Now, we have attached our EBS volume with an instance. Also, we learned how to create an IAM account and logged in through either CLI or WebUI. Hopefully, you have enjoyed this article.

Thanks for reading this article…

Hackcoderr’s Hub

Upgrade your knowledge with Hackcoderr

Sachin Kumar

Written by

Jr. Researcher | Computer Science Engineering Student| Tech Enthusiast | Technical Content Writer

Hackcoderr’s Hub

A Medium Publication sharing the technical concepts, ideas and contents.

Sachin Kumar

Written by

Jr. Researcher | Computer Science Engineering Student| Tech Enthusiast | Technical Content Writer

Hackcoderr’s Hub

A Medium Publication sharing the technical concepts, ideas and contents.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store