INTERACTING WITH AWS-CLI, A STEP FOR AUTOMATION
Hello Connections,
First of all a warm greeting to all of you guys. So, This article covers the introduction to AWS. It covers the hands-on practical on AWS basic concepts. As AWS could be operated either using WebUI or the CLI. We will perform the task using the CLI as it gives the developer much more flexibility. AWS CLI gives the ability to automate the entire process of controlling and managing AWS services through scripts.
The article covers the following topics:
- Installing the AWS CLI on Linux Machine
- Configuring the AWS CLI
- Using the Command Line Help for aws-cli
- Creating a Key-Pair
- Creating a Security-Group
- Creating an EC2 instance with Amazon Linux 2 AMI
- Create an EBS volume and attach it to the Instance
Installing the AWS CLI on Linux Machine
To install the latest version of aws-cli version 2, we need to download the package in the zip format, this could be done by using the curl command.
$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
Next, unzip the downloaded zip file and install the package using the following command.
$ unzip awscliv2.zip
$ sudo ./aws/install
You can now run: /usr/local/bin/aws --version
Checking the version
$ aws --version
aws-cli/2.1.15 Python/3.7.3 Linux/4.18.0-80.el8.x86_64 exe/x86_64.rhel.8 prompt/off
Configuring the AWS-CLI
Once we are done with installing the CLI for Amazon Web Services. We have to configure the command line. For performing any operations, we need to login into our AWS account. We need to provide the AWS Access Key ID, AWS Secret Access Key for authentication. Also, we need to set the Default region we want to work in as well as the default output format. These are given by the Default region name and Default output format parameters.
aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: ap-south-1
Default output format [None]: json
Creating a Key-Pair
Once we have an instance, in order to login inside the instance we need a key. For this, we have to create a Key in AWS which will be used to authenticate inside the instance which we will be creating in a while. create-key-pair could be found in the ec2 module. The following commands create a Key-Pair named my_key_pair and query the KeyMaterial to a file named my-key-pair.pem
$ aws ec2 create-key-pair --key-name my-key-pair --query "KeyMaterial" --output text > my-key-pair.pem
Use Command Line help for AWS-CLI
To master any tool we have go through the documentation. The aws help command will be very useful for getting familiar with the CLI.
$ aws help
$ aws <command> help
$ aws <command> <subcommand> help
Suppose, we want to create a S3 bucket, collect the possible prerequisites like Service Name → Create S3 Bucket command → arguments required for creating the bucket. For example,
$ aws help
$ aws s3api help
$ aws s3api create-bucket help
Also, the examples at the end of the help would also be quite useful while working with the Command Line Interface. So the complete command to create a basic S3 bucket looks like this,
$ aws s3api create-bucket --bucket rohanparab516clibuck --create-bucket-configuration LocationConstraint=ap-south-1
Creating a Security-Group
The security groups are the firewalls for the instances on the AWS cloud. It is like a normal firewall, the Security-Groups have inbound and outbound rules. The following command creates a Security-Group that we will be attaching to the created we will be creating.
$ aws ec2 create-security-group — group-name MySecurityGroup — description “My security group created using CLI”
Creating an EC2 Instance
instance requirements
- AMI — Amazon Linux 2 AMI
- Instance Type — t2.micro as it’s the only instance available in the free tier
- Security-Group — MySecurityGroup
- Key-Pair — my-key-pair
Most of the basic requirements have been gathered by us. Note that we have to insert IDs in arguments. The IDs could be collected one time from the WebUI. In order to launch an instance, we also need to define the subnet we want to launch our instance. Note that unlike the Region, every account has a different subnet-ids, each for each Data-Centre. A subnet is an allocated space in the Data-Centre of your region. Every Data-Centre has a subnet allocated to you.
$ aws ec2 run-instances \
--image-id ami-08f63db601b82ff5f \
--instance-type t2.micro \
--subnet-id subnet-d20d04ba \
--security-group-ids sg-0c40d4a1ac8fh9 \
--keyname my-key-pair
Create an EBS volume and attach to the created Instance
When we run any instance, the img that is used for booting instance is copied on root volume. Root volume is ephemeral type i.e., once the instance is terminated, the root volume is gone forever. So the common practices are creating an EBS of the desired volume and attaching the volume to the instance. The EBS volume is like External storage device on your AWS instance.
The basic arguments required for creating an EBS volume is the size of the volume and the Availability Zone you want it to get from. We can also define the volume type. To know more about the volume types, visit https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-volume-types.html. The following command creates an EBS volume of volume-type gp2 and size 2 Gib in the Mumbai-1a subnet.
$ aws ec2 create-volume \
--volume-type gp2 \
--size 2 \
--availability-zone ap-south-1a
Attach the EBS volume to instance
To attach the instance the basic required parameters are device_name, Instance-id, and the Volume-id. The following command is used to attach the volume to the Instance.
$ aws ec2 attach-volumne \
--device /dev/sdv \
--instance-id i-0da2a50c255704371 \
--volume-id vol-0cd692c65f8200d79
Here the task completed, the purpose to get familiar with the AWS-CLI, If you want to do the automation then this the common practice one should do.
Thanks!