Getting your hands dirty with AWS CLI

Getting your hands dirty with AWS CLI

What's AWS CLI?

The AWS Command Line Interface (CLI) is a tool that provides access to multiple AWS services in one central console.

Why use CLI when we have a much user-friendly WebUI?

Well, this is a very relevant thing to ask and let me tell you there's no clear winner. It depends on what type of user you are and what's your use-case. For example, if you're just exploring things or doing some basic stuff then WebUI will probably make things easier for you. But if you're looking for more power and control, say for automation, then you definitely need to use the CLI. Also, knowing the CLI will get you a better grasp over the underlying technology.

Installing AWS CLI

We'll be installing the AWS CLI version 2 on RHEL8:

Step 1: Downloading the zip file.

$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

Step 2: Unzipping the file.

$ unzip awscliv2.zip

Step 3: And finally installing it.

$ sudo ./aws/install


Getting started with AWS CLI

To perform any task first we need to login in the AWS. Typically while doing manual login we use a username and a password for authentication but here we use an access key id and a secret access key. And for that first we need to create a user. So, let's quickly create a user using IAM.

Step 1: Go to Services and search for IAM.

No alt text provided for this image

Step 2: Go to Users and select Add user.

No alt text provided for this image

Step 3: Giver a user name and select Programmatic Access.

No alt text provided for this image

Step 4: Now to give power or privileges to your user you need to set permissions by adding policy. Here, we're going with PowerUserAccess. It gives every power to the user except the power to access the IAM services and your billing dashboard.

No alt text provided for this image

Step 5: You can add tags if you want. Then review and after that you're ready to go. You'll get an Access Key ID and a Secret Access Key.

No alt text provided for this image

Now, back to the CLI.

Step 1: Authentication

# aws configure

Use the above command and provide your AWS Access Key ID, AWS Secret Access Key and Default region name. Leave the Default output format blank (it'll remain in JSON format by default).

No alt text provided for this image

Step 2: Security Group

# aws ec2 create-security-group --group-name my-sg --description "My security group"

Create a security group using default vpc-id.

No alt text provided for this image
No alt text provided for this image

Now, we have to add rules to our security group. So, we'll be adding TCP rule (in order to remotely login through SSH) rule to our Security Group. SSH works on port 22 and here we're allowing all IP but you can go with a custom public IP. Also, you can add as many rules as you want.

No alt text provided for this image
No alt text provided for this image

Step 3: Key

# aws ec2 create-key-pair --key-name my_aws_key --query 'KeyMaterial' --output text > my_aws_key.pem

Create a key pair and save it in a .pem file using the above command.

No alt text provided for this image
No alt text provided for this image

Step 4: Launching instance

First we need to gather some information as per our requirements. This can be done either through CLI or WebUI. We'll be using the same key and security group that we recently created.

-> image id         :    ami-0e306788ff2473ccb
-> instance type    :    t2.micro
-> count            :    1
-> key name         :    aws_cli_key
-> security group   :    sg-08a789aa14829388b
-> subnet-id        :    subnet-8b2b27e3

Use the following command to launch the instance with above configurations:

# aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --count 1 --subnet-id subnet-8b2b27e3 --key-name aws_cli_key --security-group-ids  sg-08a789aa14829388b

No alt text provided for this image
No alt text provided for this image

Step 5: Creating EBS volume

We're going to create EBS (Elastic Block Store) volume which is a persistent storage.

# aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone ap-south-1a

Using the above command we'll be launching an EBS volume of 1 GB in size at ap-south-1a.

No alt text provided for this image
No alt text provided for this image

Step 6: Configuring EBS

The volume has been created but it's not usable as of now. First we need to attach it to the instance then in order to write data over it we need to create a partition, format and mount it. And for that we'll connect to the CLI of our instance through putty.

No alt text provided for this image

Creating the partition:

# fdisk /dev/xvdf

No alt text provided for this image

Formatting the partition:

# mkfs -t ext4 /dev/xvdf

No alt text provided for this image

Creating a directory and mounting the volume in it.

# mkdir /new_ebs_volume
# mount /dev/xvdf /new_ebs_volume/

No alt text provided for this image

Now, you can use the volume to store your data.

No alt text provided for this image

For unmounting the volume use:

# unmount /dev/xvdf

No alt text provided for this image

And if you are on free tier and you're just here exploring stuff then you should terminate the instance else you might get charged once you've exhausted your free tier account limit.

# aws ec2 terminate-instances --instance-ids i-0524d69395e9d81aa


Thanks for reading...

Hope you've enjoyed...and if you really did then do hit the like button and comment what you think!

See you soon...!!!

要查看或添加评论,请登录

Ankit Kumar的更多文章

社区洞察

其他会员也浏览了