Getting started with AWS CLI

Skabhi
4 min readOct 28, 2020

Amazon Web Services (AWS) is the leading platform for cloud computing. It has nearly every conceivable thing you’d need. AWS is used extensively by top companies and startups because of its wide range of products, it’s super easy to dynamically scale, and it’s very secure.

Task Description

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

Step 1: Create a key pair

aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text | out-file -encoding ascii -filepath MyKeyPair.pem
  • Here aws is the main command.
  • ec2 is the service of aws under which create-key-pair option is for creating key.
  • — key-name option is for naming the key
  • — query option is for querying the output provided by AWS. KeyMaterial is a key which has key. Till here is AWS command and now to encode this key i have used JQ and for passing output of one command to other you have to use | this symbol
  • I encoded this key and store with same name by .pem extension

Step 2: Create a security group

aws ec2 create-security-group --description "Allow 22 & 80 Ports" --group-name "MySG"
  • Allow Port 22 for accessing the shh protocol which you will use to login with your instance remotely.
aws ec2 authorize-security-group-ingress --group-name "MySG" --protocol "tcp" --port 22 --cidr "0.0.0.0/0"
  • Allow port 80 to access the Webserver.
aws ec2 authorize-security-group-ingress --group-name "MySG" --protocol "tcp" --port 80 --cidr "0.0.0.0/0"

Step 3: Launch an instance using the above created key pair and security group.

aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type "t2.micro" --key-name "MyKeyPair" --security-groups "MySG"
  • Use the below command to create a tag for instance so that you can easily find out your instance.
aws ec2 create-tags --resource instance-id --tags Key=Name,Value=WebServer

Step-5)Create an EBS volume of 1 GB.

  • Type the following command:
aws ec2 create-volume --availability-zone "ap-south-1a" --size 1

Step-6)Attach the above created EBS volume to the instance you created in the previous steps.

  • Type the following command:
aws ec2 attach-volume --device "xvdb" --instance-id "i-0d3bffe6a474dddad" --volume-id "vol-070debe55de7932bb"
  • Now EBS Volume has been attached with instance now we have to log in with the instance with help of ssh protocol. So that we can mount the volume as well as do all those things which we will need to deploy the webserver.

Step-7)Create partition , format it and mount on /var/www/html

  • Now log in to your EC2-instance.
  • Type the following commands:
fdisk -l
  • Here we can see our EBS has been attached.
fdisk /dev/xvdb
  • Creating partition.
mkfs.ext4 /dev/xvdb1
  • Here I am formatting the partition type ext4.

Thank you hope you guys will like it.

--

--