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

Raghvendra khatri
6 min readMay 18, 2022

--

Open the AWS website, and login into your account.

How to create an IAM User in AWS?

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

IAM Dashboard

Step 2) In the left navigation panel select users.

Step 3) Enter username and check Access key — Programmatic access. Click on add user

Step 4) Set permission for your user, and check administrator access. Click on add tags.

Step 5)Enter key-vale and click on next.

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?

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.

To download the software please visit the website.

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.

Now we need the AWS Access Key ID, we will get this id from the IAM user we have created, and copy-paste it from there.

Now copy-paste the AWS Secret Access Key from the IAM user we have created.

Now enter your region name you can check your region from here or press enter if already selected the region.

Now select output format -> JSON, press enter if already selected.

Now we have successfully configured AWS CLI.

We will launch Amazon EC2 Instance using AWS CLI.

Step 1) Creating key pair with AWS CLI

Enter the command to create a key pair aws ec2 create-key-pair —key-name

To display your key-pair use command:- aws ec2 describe-key-pairs — key-name NameOfYourKeyPair

Key-pair is created successfully.

Step 2) Creating Security Groups for Amazon EC2.

You can create a security group for your Amazon Elastic Compute Cloud (Amazon EC2) instances that essentially operates as a firewall, with rules that determine what network traffic can enter and leave.

use command :- aws ec2 create-security-group — group-name my-sg — description “My security group” — vpc-id vpc-1a2b3c4d

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 on VPCs

This is your vpc id use this to create a security group.

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

Step 3) Launching an EC2 Instance using AWS CLI.

To launch an Amazon EC2 instance using the AMI you selected, use the command:- aws ec2 run-instances — image-id ami-xxxxxxxx — count 1 — instance-type t2.micro — key-name MyOwnKeyPair — security-group-ids sg-903004f8 — subnet-id subnet-6e7f829e .

Image id:- To find the image id goto ec2 console → click on launch instance → select machine image and copy its image id(we are selecting amazon linux).

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.

You have to create an EBS volume in the same availability zone that of your EC2 instance to attach it to EC2 instance.

Use command :- aws ec2 create-volume — volume-type gp2 — size 8 — availability-zone us-east-1b

Now attach EBS volume to 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.

Now we can access the object publicly.

Thanks for reading, hope you liked it💖

--

--