Getting Started with AWS CLI
Have you ever imagined if you had to run 100s of instances at a time to solve a particular use-case.
We all know much tedious(somewhat) is to select the required options always to launch a single instance in AWS. Noways everywhere we need automation. And in the present world DevOPs is playing vital role in the Automation world. Within seconds entire setup is done. This cant be achieved by WebUI. Hence we use command interface.Even major cloud services like AWS , Google CLoud etc have their own CLI. With the help of one single command we launch N number of instances.
In this article I will be going to perform some basic tasks using AWS CLI
I will use AWS CLI to create a Key-pair, Security group and an EBS volume (also attach it)and use these to launch an EC2 instance in the AWS cloud
Creating a Key Pair
Use the below command to create a Key Pair
aws ec2 create-key-pair --key-name MyKeyPair
This command generates a Key Pair which is a 2048-bit RSA key pair with the specified name. Amazon EC2 stores the public key
and displays the private key for
you to save to a file.
The private key is returned as an unencrypted PEM encoded
PKCS#1 private key.
We can confirm about the KeyPair by running the below command
aws ec2 describe-key-pairs
We can check it AWS Web console as well
Hurray!!! we have made a Key Pair using AWS CLI
Creating a Security Group and Configuring it
Creating a Security Group:
aws ec2 create-security-group --group-name my-sg --description "MySG"
This returns a id of Security Group in JSON format
Checking in the web console
Go to EC2 Dashboard --> Security Groups
We can that the Inbound Rules shows that there no permissions given which means no one from outside world are allowed
Configuring the Security Group
Lets add SSH rule using AWS CLI
For adding SSH rule
aws ec2 authorize-security-group-ingress --group-id sg-0ae53f599ea33dd12 --protocol tcp --port 22 --cidr 0.0.0.0/0
In the web console we can see that one permission is added in the inbound rules
Hurray!!! we have created and configured the Security Group
Launching an AWS Instance
Now we will launch an AWS instance using the generated key and security group.
aws ec2 run-instances --image-id ami-04b1ddd35fd71475a --instance-type t2.micro --key-name MyKeyPair --subnet-id subnet-85a3aded --count 1 --security-group-ids sg-0ae53f599ea33dd12
This one command will launch an AWS instance using the key MyKeyPair and the given Security Group ID.
We check the info of the instance using the below command
aws ec2 describe-instances
This command will give the entire info about the instance(even if they are running or stopped)
Checking the AWS Web console
Now we will make an EBS volume of 1 GiB and attach it to the running instance using the AWS CLI
aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone ap-south-1a
This command will create an EBS volume of 1GiB in the Availability Zone : ap-south-1a
NOTE: The Volume created and the instance should be in the same Availability Zone as we have to attach the volume to the instance.
Attaching the Volume created to the instance running
Now we will attach the created EBS volume to the running instance using the below command.
aws ec2 attach-volume --volume-id vol-0e79b46247a85efa4 --instance-id i-0b77dfa7af12c1a92 --device /dev/sdf
Here we need to specify the instance-id and volume-id
Hence we have attached the EBS volume to the running instance.
Hence we have created a Key-Pair, configured a Security Group and using it we have started an instance and attached a EBS volume to it.
Thanks to Vimal Daga Sir for giving this task
References:
- AWS documentation on key-pairs, creating volumes and run an instance.
Thanks for giving it a read.