How to configure EC2, EBS, and S3 in AWS using AWS CLI

How to create an IAM User in AWS?

Step 1) Search IAM services and an IAM dashboard window will open.

IAM Dashboard

Step 2) Now see the left navigation panel select “users”.

Step 3)Add a user. Click on Add User to navigate to a user detail form. Provide all details, such as the username and access type.

Step 4) Set the user permissions. Click Attach existing policies directly and then filter the policies by keyword

Step 5) Finish the user setup.Check the details of the username, AWS access type and permissions. Then, click Create user.

step 6) Now click on create user button.

Step 7) User is created successfully.

Now we will install AWS CLI software.

What is AWS CLI?

AWS Command Line Interface(AWS CLI) is a unified tool using which, you can manage and monitor all your AWS services from a terminal session on your client.

After installation open cmd and type the command:- aws — version

AWS CLI is successfully installed on your machine.

Step 1) Now we will configure AWS CLI

The aws configure command is used to set up your AWS CLI, open cmd type the following command.

Here enter the Access Key and Secret Access key which are provided when you create IAM user, then enter your default region and the last one you can skip.

Now if you have done all the steps successfully then you have successfully setup your profile and you are good to go.

Now we have successfully configured AWS CLI.

We will launch Amazon EC2 Instance using AWS CLI.

Step 1: Create key-pair : Now we have to create a key-pair. You can do easily by below command

aws ec2 create-key-pair — key-name <name-of-your-key>

Note: Copy this key into a new file with extension .pem as it will be used later. Format the above key in similar manner as shown below by replacing \n with next line.

Step 2: Create a Security group: Now before creating security group check your public IP by below command

curl https://checkip.amazonaws.com

So the CIDR notation will be <your-IP>/24. In my case the CIDR notation will be 49.36.150.128/24. It will restrict the access to your instances except IP having similar notation.

aws ec2 authorize-security-group-ingress — group-id <security-group -id> — protocol tcp — port <port-of-service-you-want-to-expose> — cidr <CIDR-Notation-of-IP>

Below command will create security group.

group-name: It could be any valid group name.

description:- It could be any related description.

vpc id :- You can find vpc from here → goto VPC services → click

Now we have successfully created a security group in Amazon EC2.

Step 3) Launching an EC2 Instance using AWS CLI.

  • ami-id of image denoted by image-id
  • no of instances to launch denoted by count
  • type of instance denoted by instance-type
  • name of key denoted by key-name
  • id number of security group denoted by security-group-ids
  • subnet id of the region denoted by subnet-id

So after collecting all the above information you can launch instance by below command

NOTE: We will use the same key-pair and security group we created earlier.

aws ec2 run-instances --image-id <ami-id> --count <no-of-count> --instance-type <type-of-instance> --key-name <name-of-key> --security-group-ids <security-group-id> --subnet-id <subnet-id>

key-name:- We have already created key pair.

Subnet-id:- To find subnet id goto → ec2 management console → click on launch instance → goto network settings → click edit (because it is deafult) and select any subnet other than 1c beacuse t2.micro does not work in 1c, and copy its id.

Security group:- we have already created a security group.

We have successfully launched Amazon ec2 instance using AWS CLI.

Now we will attach EBS Volume to EC2 Instance using AWS CLI.

Step 1: Create EBS volume: Now we have to create EBS volume to attach to our instance.You can easily create ebs volume by following command

aws ec2 create-volume --volume-type gp2 --size <storage-in-GB> --availability-zone <availability-zone-name>

Here also note down the VolumeId as it will be required later.

Step 2: Attaching EBS volume to EC2 instance:

aws ec2 attach-volume --volume-id <volume-id> --instance-id <instance-id> --device /dev/xvdh

So from above picture you can see i created one EBS volume of 1 GB and mounted to our ec2 instance

use command :- aws ec2 attach-volume — volume-id (your volume id) — instance-id (instance id) — device /dev/sdf

To upload an object to S3 bucket use command :- AWS s3 cp object_name s3://bucketname

Now make object public using acl to access it.

use command :- aws s3api put-object-acl — bucket (bucketname) — key (object-name) — acl-public-read.

So this is it. We performed all the points we wanted to achieve.

--

--

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