Configure EC2, EBS, S3 in AWS using AWS CLI

Ritika Patidar
7 min readMay 23, 2022

--

In this Activity we will create an EC2 instance using AWS CLI and after creating EC2 instance we will create one EBS volume Using AWS CLI and than attach the above EBS volume to EC2 instance created. and Create one S3 bucket and upload object inside it.

Create an IAM user in AWS=>

Step 1) Login to AWS account and search IAM service.

Step 2) In the left side ,select users and click on Add user.

Step 3) Create a user and give the username And check the Access key –
Programmatic access checkbox and click on Next Permissions button.

Step 4) Now click on Attach Existing Policies directly and check Administrator Access policy and Click on Next Tags button.

Step 5) Add Tags is optional So you can skip this and click on Next: Review button.

Step 6) Click on Create user button.

User has been created successfully!!

Step 7) Now click on download.csv file ,it will download Access Key ID and Secret access Key.

After downloading access key id and secret access key click on close button.

After creating IAM user you need to download and install AWS CLI in your system.

After installing AWS CLI in your system open the CMD and type the following command to check that AWS CLI is installed or not:

aws –version

AWS CLI is installed

Configure AWS CLI=>

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

It will ask for AWS Access key ID, AWS Secret Access key, Default region name and Default output format one by one after filling each and pressing enter.

Step 2) Copy and paste the AWS Access Key and Secret Access key from the .csv file which you had downloaded earlier.

Step 3) Next go to AWS Management console and find your region from the menu bar and write it in Default region name. My region is us-east-1.

Step 4) Write json in Default output Format and press enter.

We have successfully configured AWS CLI!!

Launch an Amazon EC2 instance using AWS CLI=>

For launching an EC2 instance we just have to run a single command that is subnet of a VPC.

aws ec2 run-instances — image-id ami-xxxxxxxx — count 1 — instance-type t2.micro — key-name MyKeyPair — security-group-ids sg-903004f8 — subnet-id subnet-6e7f829e

But before that we require ami-id, key-name, security-group-id and subnet-id.

— Steps for getting ami-id —

Step 1) Open aws management console and search EC2 in the search bar near services menu and click on it.

Step 2) In the left side choose Instances and click on Launch Instance.

Step 3) Now copy the ami-id and paste it in a text editor for further use.

— Steps for getting key-name —

Step 1) Open command prompt and type the following command to create key pair in EC2.

aws ec2 create-key-pair key-name <MyKeyName>

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

Step 3 Copy keyName and paste it in the text editor for further use.

— Steps for getting security-group-id —

Step 1) Open command prompt and type the following command to create security-group in EC2.

use command :-

aws ec2 create-security-group --group-name <security group name> --description "My security group" –vpc-id vpc-1a2b3c4d

— Steps for getting vpc-id —

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.

After getting all these things go back to command prompt again and type the following command to launch an EC2 instance.

aws ec2 run-instances — image-id ami-xxxxxxxx — count 1 — instance-type t2.micro — key-name MyKeyPair — security-group-ids sg-903004f8 — subnet-id subnet-6e7f829e

Image id:- We have Copied Earlier

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.

Your EC2 instance have been successfully launched!! To check this

-> Go back to aws management console and open EC2 from services menu.

-> Choose instances from the left navigation pane and you can see an instance running in the table.

We have successfully launched Amazon ec2 instance using AWS CLI.

Now we will attach EBS Volume and mounted this to EC2 Instance created above 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. For attaching EBS volume to the EC2 instance first we have to create volume using AWS CLI.

Steps for creating volume

Open command prompt and write the following command to create volume.

aws ec2 create-volume — volume-type gp2 — size 8 — availability-zone ap-east-1b

Now, attach EBS volume to ec2 instance=>

Step 1) Open the command prompt and run the following command for attaching the created volume with the EC2 instance.

aws ec2 attach-volume --volume-id vol-1234567890abcdef0 --instance-id i-01474ef662b89480 --device /dev/sdf

Step 2) Instance Id:- Aws console -> EC2 -> Instances -> Click on checkbox of Running Instance -> You will find Instance ID

Copy the Instance Id and paste it in a text editor for further use.

Step 3) Volume Id :- Aws console -> EC2 -> Volumes -> Select the volume

Copy the Volume Id and paste it in the text editor for further use.

Step 4) Now Open the command prompt and run the above command

The volume has been attached with the EC2 instance successfully.

Now, We will create a S3 bucket and upload The objects inside it using AWS CLI=>

Step 1) Open command prompt and write the following command.Write any unique bucket name in place of bucket-name and your choice of region in place of ap-south 1

aws s3 mb s3://bucket-name --region ap-south-1

— Steps for uploading object in S3 bucket —

Step 1) Open command prompt and write the following command to upload object in s3 bucket.

aws s3 cp — acl public-read filename.txt s3://bucket-name

Step 2) Your object or file has been successfully uploaded in S3 bucket!!

Step 3) Now make object public using acl to access it.

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

Step 4) Now we can access the object publicly.

--

--