Using AWS through CLI

Arth Task 3 given by Mr. Vimal Daga Sir

Kajal Kashyap
6 min readNov 19, 2020

Now-a-days cloud computing is the most demanded and trending technology, reading this blog will make you familiar with AWS basic commands through CLI because being a techie, we prefer CLI over GUI 😎 ❗

️Before we begin..
Let’s me tell you, what we are going to do..

👉 Create a key pair
👉 Create a security group
👉 Launch an instance using the above created key pair and security group.
👉 Create an EBS volume of 1 GB.
👉 The final step is to attach the above created EBS volume to the instance you created in the previous steps.

Let’s get rolling…

Brief about What is Cloud computing with AWS ?

Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform.
Instead of buying, owning, and maintaining physical data centers and servers, you can access technology services, such as computing power, storage, and databases, on an as-needed basis from a cloud provider like Amazon Web Services (AWS).
Millions of customers — including the fastest-growing startups, largest enterprises, and leading government agencies — are using AWS to lower costs, become more agile, and innovate faster.

🔴 Pre-requisite

👉 Create an AWS account here

👉 Download AWS CLI from here

✅ Let’s proceed…

👉 To verify if AWS CLI is properly configured

Command : aws --versionOutput : aws-cli/2.0.30 Python/3.7.7 Windows/10 botocore/2.0.0dev34

👉 To verify if AWS User is configured

Command : aws configureOutput : AWS Access Key ID [****************R7OA]:
AWS Secret Access Key [****************XwV2]:
Default region name [ap-south-1]:
Default output format [None]:

1️⃣ Create a Key Pair

A key pair, consisting of a private key and a public key, is a set of security credentials that you use to prove your identity when connecting to an instance

Having No key named “Task3_key”

◼ Right now, I have only 2 keys

Without using AWS WebUI, Just by using a single command I am going to create a Key Pair named “Task3_key

Command : aws ec2 create-key-pair --key-name Task3_keyOutput : {
"KeyFingerprint": "5a:e5:ae:be:53:57:b9:5e:34:d1:3d:d4",
"KeyMaterial": "-----BEGIN RSA PRIVATE KEY-----\nBAAKCAQEAnWTOOurIJdqdKbKWWTwt78aea674f5Qc6XtDtYf52IZCzyjW\nyl30DLeG3PwkuuvmxL7o19pV+7GWyaugoy0fuOccaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazX+vpvOBGeflx7ON5vErSDT\n7bAq2OW/YelSlKlY54HILoOWwILglmonArwEMWmwyUqt7buot672Cb0ha6jXJhP2\nAViXwTxYfhfjjefj9f4cn89t4ldjf9f\n-----END RSA PRIVATE KEY-----",
"KeyName": "Task3_key",
"KeyPairId": "key-037727jiehgi"
}
⭐ Key named “Task3_key” is created

Take a Note :
▪ Command to see how many key pairs we have :
aws ec2 describe-key-pairs
Command to delete key pair :
aws ec2 delete-key-pair --key-name <key_name>

2️⃣ Create a security group

A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic.

No Security Group named “Task3_SecurityGroup” is present

◼ While creating Security Group, we give them a unique identity called Security Group Name

◼ And a description, where we can write why this Security Group is created and so on

Command : 
aws ec2 create-security-group --description "This is Task3 Security group" --group-name Task3_SG
Output :
{
"GroupId": "sg-0b02f55e"
}
⭐Security Group named “Task3_SG” is created

Take a Note :
▪ Command to see how many security group we have :
aws ec2 describe-security-groups
Command to delete security group :
aws ec2 delete-security-group --group-name <value>

3️⃣ Launch an instance using the above created key pair and security group

Instances in AWS are basically virtual environments. It’s an On-demand service, i.e. a user can rent the virtual server(instances) on an hourly base and deploy their applications on it.

There is NO instance running

◼ For launching instance we need to specify

✔ Image name
✔ No. of instances
✔ Type of instance
✔ Key-pair name
✔ Subnet Id
✔ Security Group Id

Command : 
aws ec2 run-instances --image-id ami-026669ec456129a70 --instance-type t2.micro --key-name Task3_key --security-group-ids sg-0b02f55e --subnet-id subnet-06ccd021cb359 --count 1
Output :
{
"Groups": [],
"Instances": [
{
"ImageId": "ami-026669ec456129a70",
"InstanceId": "i-048ccf891e23",
"InstanceType": "t2.micro",
"KeyName": "Task3_key",
"LaunchTime": "2020-11-19T06:53:39+00:00",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "ap-south-1a",
"GroupName": "",
"Tenancy": "default"
},
......
)
Instance Launched Successfully⭐

Take a Note :
▪ Command to see how many instance we have :
aws ec2 describe-instances
Command to stop instance :
aws ec2 stop-instances --instance-ids <value_of_instance_id>
Command to start instance :
aws ec2 start-instances --instance-ids <value_of_instance_id>
Command to terminate instance :
aws ec2 terminate-instances --instance-ids <value_of_instance_id>

4️⃣ Create an EBS volume of 1 GB.

Amazon Elastic Block Store (EBS) is an easy to use, high performance block storage service designed for use with Amazon Elastic Compute Cloud (EC2) for both throughput and transaction intensive workloads at any scale.

No EBS Volume of 1 GiB is available

◼ In simple words, this is like creating a virtual pendrive

◼ Which can be attached or detached in any ec2-instance

◼ Data stored in EBS is persistent and doesn’t get effected by corruption/crash of EC2

Command : 
aws ec2 create-volume --availability-zone ap-south-1a --volume-type gp2 --size 1
Output :
{
"AvailabilityZone": "ap-south-1a",
"CreateTime": "2020-11-19T08:58:30+00:00",
"Encrypted": false,
"Size": 1,
"SnapshotId": "",
"State": "creating",
"VolumeId": "vol-03683ae6c8f5e6fad",
"Iops": 100,
"Tags": [],
"VolumeType": "gp2"
}
EBS Volume with 1 GB created

Take a Note :
▪ Command to see how many volume we have :
aws ec2 describe-volumes
Command to delete volume :
aws ec2 delete --volume — volume-id <value_of_volume_id>

5️⃣ Attach the above created EBS volume to the instance you created in the previous steps.

◼ Right Now, only one default volume is attached to the EC2 instance

◼ After attaching the EBS (pendrive like storage) the instance should have 2 volumes

Command : 
aws ec2 attach-volume --device /dev/xvdb --instance-id i-048c86f05cf891e23 --volume-id vol-03683ae6c8f5e6fad
Output :
{
"AttachTime": "2020-11-19T09:54:59.747000+00:00",
"Device": "/dev/xvdb",
"InstanceId": "i-048c86f05cf891e23",
"State": "attaching",
"VolumeId": "vol-03683ae6c8f5e6fad"
}
EBS with 1 GiB storage is attached successfully, Now it has 2 volumes attached

Take a Note :
▪ Command to detach volume :
aws ec2 detach-volume — volume-id <value_of_volume_id>

⭐ One more essential infomation..

These are just few commands and list of AWS commands and subcommands is Giant
So whenever you get stuck, you can always take help of aws documentation. It is simply great and easy to understand

To do so…
Just go to command prompt and type aws help

◼ This will give you entire documentation but complete document will not be visible all at once only a section of it.
◼ To read more press spacebar and to quit press q
◼ Now choose the option or command (say ec2)
◼ But if you want to know further subcommands of ec2 agian type aws ec2 help ànd keep doing untill you find your desired command

😎🤩 This blog is incomplete without Thanking my source of learning… I am greatly Thankful to Mr. Vimal Daga Sir, under whose mentorship i am able to learn these most demanded technologies with such an ease.

🙂 Hopefully, this helped you in getting familiar with AWS CLI for any doubts or suggestions, you can ping me on Linkedin I’ll be happy to help.

--

--

Kajal Kashyap
Kajal Kashyap

Written by Kajal Kashyap

When The Atmosphere Encourages Learning, Learning Is Irresistible..

No responses yet