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
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.
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.
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
From the list of available images, we will select the following image with the Image id ami-002068ed284fb165b.
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
For this tutorial, we will select t2.micro Instance Type.
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
We can view the newly created Key pair in AWS Management Console.
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"
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
We can view our Security Group in the AWS Management Console with the Inbound rules.
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
We can view our EC2 instance in the AWS Management Console.
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
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
We can see that our instance has been successfully terminated.
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}
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
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}/
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}
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
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
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!