Getting started with AWS CLI....
This is an small article on explanation of getting started with Command line interface with some easy and helpful examples after once gathering a basic knowledge of AWS EC2 and S3 services..
What is AWS CLI ?
AWS CLI is one of the software provided by AWS to get connected with the AWS by using their programmable API endpoints.
Download and Install from here 👇
Once downloaded its just a couple of click and you are ready to explore the new world with command line interface
Since AWS is an account based service so authentication is always what always needed so here we can use IAM services from AWS to create an user with programmable powers
**Make sure you Tick the programmatic access and if you want , you can also allow AWS Management Console access.
Now we need to attach some policies which will restrict our IAM user and For restricting our to access various services like disallowing billing part and IAM services.
Referring to this use case, we can attach AdministatorAccess and PowerUserAccess policies.
Just skip this part ,we don't need it really much.
In the next page just download the Access key and Secret key provided by AWS and this will look something like 👇
Then finally keys will be provided in some csv file named new_user_credentials.csv and this part you have to do yourself ofcourse I'm not going to share my credentials 😅
So we got one user so lets start our AWS-CLI with the configuration and so use the following command and add your Access Key and Secret Keys
aws configure
Everything is setup and now lets have some examples to get a basic look of how and what we can achieve using AWS CLI
Examples:
1) Understanding AWS cli
Just type aws help and here they will tell you what to do and you will get a basic syntax i.e.
aws [service name] <command> <subcommand> [parameters]
2) Creating a key pair for EC2
As we are here studying different ways to get started so just take help of aws and write the service name and get more help and keep on going till we get to some conclusion
aws ec2 help
This will tell us more and then we get more idea and moving further we can see option for creating key
and using this you will get output something like 👇
Actually we need to capture the contents into a pem file with proper unicode so we can redirect output to a pem file using following command
aws ec2 create-key-pair --key-name MyKeyPair --query KeyMaterial --output text > MyKeyPair.pem
Actually query is an approach using which we can filter the options and thereby I have redirected the output to YourKeyPair.pem
3) Creating Security Group
Similar approach can be used to create security-group and therefore use the following:
aws ec2 create-security-group help
Here you will get to know that you need to specify your vpc on which you want to create security group so you can find it by :
aws ec2 describe-vpcs
See above , security group is created but however no ingress rules are bound to it so we can authorize them by searching for
aws ec2 authorize-security-group-ingress help
* Note - CIDR represents the range of IPs a particular service can accommodate and that we have to specify
I'm going to allow SSH for every IPv4 , so I will use following command
aws ec2 authorize-security-group-ingress --group-id sg-018f8f1c3e8c75f54 --protocol tcp --port 22 --cidr 0.0.0.0/0
4) Launching an instance
Using similar approach, time to launch an instance:
aws ec2 run-instances help
So use command with your created security group and created key:
aws ec2 run-instances --image-id ami-052c08d70def0ac62 --count 1 --instance-type t2.micro --key-name YourKeyPair --security-group-ids sg-018f8f1c3e8c75f54
And its launched we can see above........
5) Creating EBS volume
Similarly we can add an EBS volume of particular size
aws ec2 create-volume help
As per my requirement I will be adding an General purpose ssd disk of 1GB and I'll be launching this on our default availability zone - ap-south-1a
aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone ap-south-1a
And Here it is launched..........
6) Attaching EBS to EC2 instance
Finally I also want to attach this block storage to my previously launched OS so I can do that by getting help from
aws ec2 attach-volume help
Therefore using command as follows:
This figure 👇 explains that volume has been mounted successfully .
Well that explains it and this much is enough for now , I may be upcoming with more examples if I will found out to be special. That's it for now .........