AWS CLI — A complete guide to use AWS CLI and launch instance through it.
Hello Guys, In today’s blog I am gonna show you how to launch ec2 instances, create key pairs, security groups etc. only with the help of AWS CLI.
So Lets start today’s blog with setting up AWS CLI for windows.
Step 1 :- First download a AWS CLI setup file from amazon web services for windows or download from the below link.
After downloading the above setup just double click on it and Install the setup.
Click on Next button and then install, After installing CLI open command promopt and type aws in cmd to see successful installation. If the installation is successful you will see a output like the image below.
Step 2 -After installing AWS CLI you have to setup your login credentials , To setup login in cli run the following command.
After running the above command enter the AWS Access key ID and AWS Secret Access Key following Default region name. To get this access key You have to create a I am user in amazon with the respective permissions or you can ask your group head to provide you the access keys.
Create a key pair.
A key pair is the set of keys which validate a user. In this pair there are two a private and a public key contains. In which public key is accessible by anyone but the private key can only be with the owner. You can get this with the example of Lock and Key, where Lock act likes as a public key and key likes a private key. Just like lock can only be access through its key a public key containing instance can only be accessed through its private key.
To create a private Key run the following command in the terminal.
“ aws ec2 create-key-pair — key-name key-aws”
After running the above command it will prompt you a output, Copy that output in a file and securely safe it with yourself. It is your private access key which will help you further to connect to your instance.
Create a Security Group.
A security group is a set of inbound and outbound rules which secure the instances connected through it. You can think a security group is just like a firewall, It will only allow a limited range of IP’s set by user to the instances.
To create a security group run the following command in cmd.
aws ec2 create-security-group — group-name ‘Name of your choice’ — description none
After creating the security groups you have to add rules , or allow IP addresses to come or out from your instances. Currently I am allowing all the traffic to my instance coming through SSH (port number 22) but you can limit it in your way.
For creating rules run the following command.
aws ec2 authorize-security-group-ingress — group-id ‘Id you get from above command’ — group-name ‘name you created before’ — protocol tcp — port 22 — cidr 0.0.0.0/0
Here I allow incoming traffic only for a specific port, which is port number 22(port for SSH protocol). SSH is a way to securely connect to linux instances through remote login. Here ingress stands for incoming traffic and egress stand for outgoing traffic.
Launching a ec2 instance.
Now we are all set to launch our ec2 (Elastic Cloud Compute) instance.
To launch a instance we need a key pair, a security group, image id(A image Id is a unique id of a image determines the Operating system), and a Subnet id(Subnet Id is generally the ID of zone where you want to launch your instance.)
To launch a instance run the following command -
aws ec2 run-instances — image-id ‘Os you want it’s id’ — instance-type t2.micro — key-name ‘key you created before’ — security-group-ids ‘Id you get after creating a security group’ — subnet-id ‘Id of the zone you want to launch your instance’ — count 1
After running the above command you will see a output in JSON format, this output shows you all the details about your instance.
Creating a EBS volume and attaching it to previous instance.
Now, I am going to show you how to create a EBS volume and attach it through AWS CLI to a running instance.
To create a EBS volume run the following command
aws ec2 create-volume — volume-type gp2 — size 1 — availability-zone ‘zone you want your volume to be in.’
After creating you have to attach the volume to the instance. To attach run the following command.
aws ec2 attach-volume — instance-id ‘Id you get after creating your instance’ — volume-id ‘Id you get after creating EBS volume’ — device /dev/xvdf
Your EBS volume is attached to the instance, Now for seeing the volume you can run the below command to describe instance status.
aws ec2 describe-instances
Guys thanks for reading the above article hope you find all this information valuable. If you find the above insightful don’t by shy to express your support in comments and following. I also will like to thank Mr. Vimal Daga sir for asking me to write this blog.
Thank you Guys!!