AWS CLI Explained with Practical Examples [Tutorial]


Written by - Mahnoor Malik
Reviewed by - Deepak Prasad

In this tutorial, we will learn about AWS CLI and use it for the creation of an EC2 instance and S3 Bucket. Note that we will be using Windows Operating System for the hands-on part. Before getting our hands dirty let's have a quick overview of AWS CLI.

 

AWS CLI Overview

According to the official website of AWS .

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.

Now let's get started with the implementation part.

 

AWS CLI Setup

First, we will download the AWS CLI MSI installer for Windows from the official website then we will install it. To confirm that the installation has been done properly we will enter the following command on cmd.

aws --version

AWS CLI Explained with Practical Examples [Tutorial]

Now we will create Access Key. We will use Access Keys to make programmatic calls to AWS from the AWS CLI. First, we will go to the Users section in IAM then click on the username for which we want to create the Access Key. After that, we will go to the Security Credentials tab and click on Create Access key. Then we can download the Access Key.

Note that we should never share our secret keys with anyone.

AWS CLI Decoded with Practical Examples [Tutorial]

 

Now we will configure our AWS CLI. First, we will write aws configure on cmd, hit enter, then we will provide our Access Key ID, Secret Access Key we have just downloaded. Then we will provide our default region name. For Default output format we will only hit enter. The Default output format specifies how the results will be formatted. If we don't specify an output format, JSON is used as the default.

AWS CLI Decoded with Practical Examples [Tutorial]

With this, we have configured our AWS CLI.

 

Create EC2 instance with AWS CLI

Now we will create an EC2 instance with AWS CLI. First, we will select an Image ID, then we will select an instance type for our EC2 instance. After that we will create a Key pair and in the end we will create a Security Group and set an Inbound rule for it. For the rest of the configuration, we will leave it to default for now.

 

Step-1: Select an AMI

We can find our required Amazon Machine Image from the following command. It will return all the images available to us.

aws ec2 describe-images

AWS CLI Explained with Practical Examples [Tutorial]

 

From the list of available images, we will select the following image with the Image id ami-002068ed284fb165b.

AWS CLI Explained with Practical Examples [Tutorial]

We can check describe-images official documentation from AWS for more information.

 

Step-2: Select an Instance Type

We can get the list of the available instance types with the following command.

aws ec2 describe-instance-types

AWS CLI Explained with Practical Examples [Tutorial]

For this tutorial, we will select t2.micro Instance Type.

AWS CLI Explained with Practical Examples [Tutorial]

We can check describe-instance-types official documentation from AWS for more information.

 

Step-3: Create a Key Pair

We can create a key pair with the following command.

aws ec2 create-key-pair --key-name MyDemoKeyPair

AWS CLI Explained with Practical Examples [Tutorial]

We can view the newly created Key pair in AWS Management Console.

 

AWS CLI Explained with Practical Examples [Tutorial]

We can check create-key-pair official documentation from AWS for more information.

 

Step-4: Create a Security Group

We can create a security group with the following command.

aws ec2 create-security-group --group-name MyDemoSecurityGroup --description "My Demo security group"

AWS CLI Explained with Practical Examples [Tutorial]

We can check create-security-group official documentation from AWS for more information.

Now we will create an Inbound rule for our newly created Security Group with the following command. The following example enables inbound traffic on TCP port 22 (SSH) from the CIDR IP range 0.0.0.0/0. We can check authorize-security-group-ingress official documentation from AWS for more information.

aws ec2 authorize-security-group-ingress --group-name MyDemoSecurityGroup --protocol tcp --port 22 --cidr 0.0.0.0/0

AWS CLI Explained with Practical Examples [Tutorial]

 

We can view our Security Group in the AWS Management Console with the Inbound rules.

AWS CLI Explained with Practical Examples [Tutorial]

 

Step-5: Create an EC2 instance

Now we are ready to create an EC2 instance with the Image ID and Instance Type we have selected and the Key Pair and Security Group we have created with the following command.

aws ec2 run-instances --image-id ami-002068ed284fb165b --count 1 --instance-type t2.micro --key-name MyDemoKeyPair --security-group-ids  sg-07b74523797263314

AWS CLI Explained with Practical Examples [Tutorial]

 

We can view our EC2 instance in the AWS Management Console.

https://bc664b2a.flyingcdn.com/wp-content/uploads/8-24-e1639494009939.png

Here we have created an EC2 instance with the following configurations.

  • image id = ami-002068ed284fb165b
  • count = 1
  • instance-type = t2.micro
  • key-name = MyDemoKeyPair
  • security-group-ids = sg-07b74523797263314

We can create an EC2 instance according to our requirements by providing the values accordingly. We can check run-instances official documentation from AWS for more information.

Note that we can refer to our last tutorial to create an EC2 instance using the AWS Management console.

 

Step-6: List EC2 instances

We can list EC2 instances with the following command.

aws ec2 describe-instances

AWS CLI Decoded with Practical Examples [Tutorial]

 

Through this command, we can list all our instances, or filter the results based on the instances that we are interested in. We can check describe-instances official documentation from AWS for more information.

 

Step-7: Terminate EC2 instance

We can delete an AWS instance with the following command.

aws ec2 terminate-instances --instance-ids i-06c1283261aeb19b7

AWS CLI Explained with Practical Examples [Tutorial]

We can see that our instance has been successfully terminated.

 

AWS CLI Explained with Practical Examples [Tutorial]

 

We can check terminate-instances official documentation from AWS for more information.

 

Amazon S3 with AWS CLI

Create Bucket

We can use the following command to create an S3 Bucket using AWS CLI.

aws s3 mb s3://{my-bucket-name}

AWS CLI Decoded with Practical Examples [Tutorial]

Here we will provide our bucket name that should be globally unique.

Note that we can refer to our last tutorial to create s3 Buckets using the AWS Management console.

 

Upload Object

We can use the following command to upload an object to an Amazon S3 Bucket using AWS CLI.

aws s3api put-object --bucket my-s3-cli-bucket --key dir-1/aws.png --body aws.png

AWS CLI Decoded with Practical Examples [Tutorial]

Here we will provide the name of our bucket i.e. my-s3-cli-bucket then the key i.e. dir-1/aws.png and the name of the object i.e. aws.png which lies in the path we are currently in. We can check put-object official documentation from AWS for more information.

 

List Bucket and Objects

We can use the following command to list buckets and the objects inside them.

aws s3 ls s3://{my-bucket-name}
aws s3 ls s3://{my-bucket-name}/{folder-name}/

AWS CLI Decoded with Practical Examples [Tutorial]

We can check ls official documentation from AWS for more information.

 

Delete Bucket and Object

To delete an object in the bucket we can use the following command.

aws s3 rm s3://{my-bucket-name}/{folder-name}/{file-name}

AWS CLI Decoded with Practical Examples [Tutorial]

To delete all the objects that lie inside a folder in the bucket we can use the following command.

aws s3 rm s3://{my-bucket-name}/{folder} --recursive

AWS CLI Decoded with Practical Examples [Tutorial]

Note that through --recursive the command is performed on all files or objects under the specified directory or prefix. We can check rm official documentation from AWS for more information.

To delete an S3 Bucket along with objects and prefixes, we can use the following command.

aws s3 rb s3://{my-bucket-name} --force

AWS CLI Decoded with Practical Examples [Tutorial]

If we do not use --force first we will have to make the bucket empty only then we will be able to delete it. Note that if we have versioning enabled with a bucket that contains previously deleted but retained objects, this command will not work. We can check rb official documentation from AWS for more information.

 

Conclusion

With this, we have come to the end of our tutorial. In this tutorial, we learned about AWS CLI. First, we learned that how we can set up AWS CLI on our PC. After that, we walked through the process of creation of  EC2 instance and S3 Bucket using AWS CLI and played around with a few of the many options available in AWS CLI.

Feel free to give any feedback or ask any query in the comments section and stay tuned for some more informative tutorials coming ahead.

Happy learning!

 

Mahnoor Malik

She is a Computer and Information Systems Engineer with a master's degree in Computer Networks and Systems Security. She excels in backend development, specializing in Java with the Spring Boot framework, and is proficient in other languages such as C, C++, and Python. You can reach out to us on Facebook page.

Categories AWS

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment

X