AWS EC2 — CLI
Under this blog we will explore AWS command line interface(CLI). Please follow AWS-EC2-GUI to perform same set of process via Graphical user interface(GUI).
But Why CLI ?
AWS CLI is yet another service as provided by amazon to manage AWS. It’s basically a tool to manage or control aws modules via command line . In upcoming section we going to see how we setup and manage few of aws services via command line.
Prerequisite:
OS : Linux,Unix
Python 2 Version 2.6.5+ or Python 3 Version 3.3+
AWS CLI Setup
Below steps to install and setup AWS CLI for first time.
1. Download AWS Bundled Installer
$ curl “https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o “awscli-bundle.zip”
2. Unzip the package and install executable
$ unzip awscli-bundle.zip
$ sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
3. Check if AWS CLI is correctly setup and ready to use
$ aws — version
$ aws help
Setup is correct if you see aws as a system recognized command.
4. Configure AWS
Post installation, use aws configure to setup AWS CLI to use specific user credentials for particular account which is going to manage the services.
$ aws configure
[awscli@awscli ~]$ aws configureAWS Access Key ID [None]: XXXXXXXXXXXXXXXXAWS Secret Access Key [None]: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXDefault region name [None]: ap-south-1Default output format [None]: json
Run an instance via AWS CLI
Now we done with aws cli setup so, its time to use it :-)
Follow below steps to spin up an ec2 instance and to terminate an instance via aws cli.
- Generate Key-Pair
First we generate key-pair to access our EC2 instance that we going to launch in upcoming steps.
$ aws ec2 create-key-pair — key-name awsclinew — query ‘KeyMaterial’ — output text > awscli.pem
2. Create Security Group
Next Step is to generate security group for our instance to allow/set inbound and outbound traffic to/via EC2 instance.
$ aws ec2 create-security-group — group-name awscligroup — description “aws cli security group”
3. Rules For Security Group
We created a security group in our last step but rules ? . Use below steps to open some of the basic ports like port 22 (ssh) Or 80/443(http/https) to make an EC2 instance reachable to internal or external world.
$ aws ec2 authorize-security-group-ingress — group-name awscligroup — protocol tcp — port 22 — cidr 0.0.0.0/0
$ aws ec2 authorize-security-group-ingress — group-name awscligroup — protocol tcp — port 80 — cidr 0.0.0.0/0
$ aws ec2 authorize-security-group-ingress — group-name awscligroup — protocol tcp — port 443 — cidr 0.0.0.0/0
- port → Port for a service to be open
- cidr → allowed range of IP
- protocol →Protocol to be open
- group-name → Security Group
- authorize-security-group-ingress → used for incoming traffic
Run below command to check and validate if rules got attached to our security group.
$ aws ec2 describe-security-groups — group-names awscligroup
4. Create EC2 instance
Now it’s time to create an instance, use below command to initiate an instance request.
$ aws ec2 run-instances — image-id ami-cdbdd7a2 — count 1 — instance-type t2.micro — security-groups awscligroup — key-name awsclinew
Post successful execution of above command it will give details for a created instance , search for instance-id tag , that we will be going to use in next step.
- run-instances → to run a new ec2 instance.
- image-id → OS image id(AMI) to be use for an instance.
- count → number of instance to be created.
- instance-type → type of instance i.e. capacity.
- security-groups → security group to be attached to our new instance.
- key-name → key file to be used for login/authentication.
5. Login to EC2
Finally, we ready to login!
First, describe instance details via below command for detailed instance configuration.
$ aws ec2 describe-instances — instance-ids i-xxxxxxxxxxxxxxxxx
- i-xxxxxxxxxxxxxxxxx → instance id of an instance.
Pick IP from above details and jump on an ec2 instance using below command.
$ ssh -i awscli.pem ec2-user@xx.xxx.xx.xxx
ec2-user is an default user which is created for each ec2 instance and same is used for login.
- xx.xxx.xx.xxx → IP assigned to our instance.
6. Cleanup
Time for clean up as its good practice to terminate an instance i.e. not in use to save overall cost.
$ aws ec2 terminate-instances — instance-ids i-xxxxxxxxxxxxxxxx
Done. Use ‘aws help’ to check or explore other aws available commands.
— A blog by teckdevOps