AWS Command Line Interface|How to launch EC2 Instances & attach additional EBS Volume

Yashraj Panda
7 min readOct 14, 2020

--

Blog Objective:

  • Creating a key pair for AWS ec2 instance.
  • Creating a security group for AWS ec2 instance.
  • Launching an AWS ec2 instance using the above created key pair and security group.
  • Adding an EBS volume of 1 GB.
  • Attach the above created EBS volume to the ec2 instance we created.

[Note: All the above objectives we will achieve by using the CLI.]

What is CLI?

A command-line interface (CLI) processes commands to a computer program in the form of lines of text. The program which handles the interface is called a command-line interpreter or command-line processor. Operating systems implement a command-line interface in a shell for interactive access to operating system functions or services.

Using detailed commands through a command-line interface can be faster and more efficient than scrolling across GUI tabs and dialogs. This can be particularly powerful when handling highly repetitive tasks across many systems.

AWS Command Line Interface

The AWS Command Line Interface (AWS CLI) is an open source tool that enables you to interact with AWS services using commands in your command-line shell. With minimal configuration, the AWS CLI enables you to start running commands that implement functionality equivalent to that provided by the browser-based AWS Management Console from the command prompt in your terminal program:

  • Linux shells — Use common shell programs such as bash, zsh, and tcsh to run commands in Linux or macOS.
  • Windows command line — On Windows, run commands at the Windows command prompt or in PowerShell.
  • Remotely — Run commands on Amazon Elastic Compute Cloud (Amazon EC2) instances through a remote terminal program such as PuTTY or SSH, or with AWS Systems Manager.

To know more about AWS command line interface, click here

For using AWS services through CLI, we need to download and install the AWS cli from here, [click].

[NOTE : run “aws — version” to confirm the installation is successful or not and also to know about the version of aws that is installed in your system]

Creating User for CLI using IAM Service:

We cannot configure our cli directly with AWS. Before configuring we need to go to the IAM service of AWS and create a user with certain permissions.

Steps to create IAM user in AWS:

  1. Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/.
  2. In the navigation pane, choose Users and then choose Add user.
  3. Type the user name for the new user. This is the sign-in name for AWS. If you want to add more than one user at the same time, choose Add another user for each additional user and type their usernames. You can add up to 10 users at one time.
  4. Select the type of access this set of users will have. You can select programmatic access, access to the AWS Management Console, or both.
  • Select Programmatic access if the users require access to the API, AWS CLI, or Tools for Windows PowerShell. This creates an access key for each new user. You can view or download the access keys when you get to the Final page.
  • For Console password, choose one of the following:

→ Autogenerated password. Each user gets a randomly generated password that meets the account password policy in effect (if any). You can view or download the passwords when you get to the Final page.

→ Custom password. Each user is assigned the password that you type in the box.

5. Choose Next: Permissions.

6. On the Set permissions page, specify how you want to assign permissions to this set of new users. Choose one of the following three options:

→ Add user to group. Choose this option if you want to assign the users to one or more groups that already have permissions policies. IAM displays a list of the groups in your account, along with their attached policies. You can select one or more existing groups, or choose Create group to create a new group. For more information, see Changing permissions for an IAM user.

7. Choose Next: Tags.

8. Add metadata to the user by attaching tags as key-value pairs. For more information about using tags in IAM, see Tagging IAM users and roles.

9. Choose Next: Review to see all of the choices you made up to this point. When you are ready to proceed, choose Create user.

10. To view the users’ access keys (i.e. access key IDs and secret access keys), choose Show next to each password and access key that you want to see. To save the access keys, choose Download .csv and then save the file to a safe location.

[NOTE: Copy and save the access key IDs and secret access keys in any text editors as we need those while configuring AWS cli]

Configuring AWS CLI:

To use AWS services directly from the cli we need to configure it by using the following command:

“Aws configure”

After running the above command you need to give the access key IDs and secret access keys that we have saved while creating the IAM. Along with that you have to give the region name and output format. In region name you have to provide the region id (e.g. ap-south-1 is region name/id for mumbai ) to create your services in the region.

=> Now that we have configured the CLI with AWS, we can access all the services of AWS right from the CLI of our P.C.

Creating Key-pair for AWS using CLI:

We can create the “key pair” by running the following command:

“Aws ec2 create-key-pair — key-name <string>”

Creating Security group for AWS using CLI:

Using the following command we can create the security group for out instance:

“aws ec2 -create-security-group — group-name <string> — description <string>”

We can see Information about Security Group in CLI using command :

“aws ec2 describe-security-group — group-id <security_group_id>”

Creatine EC2 instance using the above create key pair and security group:

For creating a EC2 instance using CLI we need certain information about the instances like:

  1. AMI (Amazon Machine Image) ID — An Unique ID for individual operating system
  2. Instance Type — It refers to Compute Engine, or Resources like RAM & CPU, etc.
  3. Key Pair — For Authentication of AMI.
  4. Security Group — The Amazon Web Services policy or firewall to secure networks.
  5. Subnet Id — It refers to Availability Zone from the list of AZs of the region.

[NOTE: You can get all the above ids from AWS web-UI or from google]

After gathering all the information use the following command to create the instances,

“asw ec2 run-instances — image-id ami-0e306788ff24xxxxx — instance-type t2.micro — count <string> — key-name <string> — security-group-ids sg-01780a990e57xxxxx — subnet-id subnet-c3cxxxxx”

Adding an EBS volume after creating the instance:

Before creating the storage we need to check the availability zone and volume type of the storage you want to create.

Now use the following command to create the volume:

“aws ec2 create-volume — size 1 — volume-type gp2 — availability-zone ap-south-1a”

Attaching the above created volume to the EC2 instance:

We need to get the volume id, instance id and device name to attach the above created ebs volume.

we need to run the following command to attach the EBS storage,

“aws ec2 attach-volume — volume-id vol-02d26f0837baaa3b6 — instance-id i-06861e8bbf16f900b — device /dev/sdf”

You can use CLI to perform all the AWS operations more efficiently than AWS web-UI. You can check the browser for commands for different operation or you can always use the “help” command after AWS to know about all the commands, operations and how to use them.

THANK YOU!!

--

--

Yashraj Panda
Yashraj Panda

Written by Yashraj Panda

A B.tech undergrad, enthusiastic towards learning new technologies in the market and integrate the technologies with each other.

No responses yet