100% found this document useful (1 vote)
222 views5 pages

Aws

The document provides instructions for configuring and using the AWS CLI to manage EC2 resources. It explains how to install and configure the AWS CLI with access keys, then lists EC2 commands to describe regions and availability zones, create key pairs, security groups, and instances, add tags, and clean up resources. It also includes examples of additional commands for ELB, Auto Scaling, filtering output, and using HTTP proxies.

Uploaded by

sarath Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
222 views5 pages

Aws

The document provides instructions for configuring and using the AWS CLI to manage EC2 resources. It explains how to install and configure the AWS CLI with access keys, then lists EC2 commands to describe regions and availability zones, create key pairs, security groups, and instances, add tags, and clean up resources. It also includes examples of additional commands for ELB, Auto Scaling, filtering output, and using HTTP proxies.

Uploaded by

sarath Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

For Mac Refer: https://docs.aws.amazon.

com/cli/latest/userguide/awscli-install-
bundle.html
Configure the AWS CLI Tools on Linux/Windows/Mac

In your bash shell / CMD prompt /Mac terminal

aws configure

Here you need to specify:

AWS Access Key ID [None]:Type-Your-Access-Key-here PRESS-ENTER


AWS Secret Access Key [None]:Type-Your-Secret-Access-Key-here PRESS-ENTER
Default region name [None]: us-west-2 PRESS-ENTER
Default output format [None]: table PRESS-ENTER

EC2 COMMAND LEVEL Reference-sample commands


== == == == == == Forwarded message == == == == == ==
aws ec2 describe-regions
aws ec2 describe-availability-zones --region ap-south-1

aws ec2 create-key-pair --key-name SarathKeyPair > SarathKeyPair.pem

also try the following and mark the difference


aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text >
MyKeyPair.pem

Reference only: aws ec2 import-key-pair --key-name keyname_test --public-key-


material file:///cldvds/sagu/id_rsa.pub

aws ec2 create-security-group --group-name SarathSecurityGroup --description "My


security group"

aws ec2 authorize-security-group-ingress --group-name SarathSecurityGroup


--protocol tcp --port 22 --cidr 0.0.0.0/0

aws ec2 run-instances --image-id ami-4836a428 --count 1 --instance-type t2.micro


--key-name SarathKeyPair --security-groups SarathSecurityGroup

aws ec2 describe-instances --instance-id i-0aa71646286423c41

aws ec2 create-tags --resources i-0aa71646286423c41 --tags


Key=Name,Value=SarathServer

Cleanup steps:

aws ec2 terminate-instances --instance-id i-0aa71646286423c41

aws ec2 delete-key-pair --key-name SarathKeyPair

aws ec2 delete-security-group --security-groups SarathSecurityGroup

Note the instance ID from the output of last statement and replace it
respectively in below statement:

aws ec2 describe-instances --filter Name=instance-type,Values=t2.micro


--filter Name=instance-id,Values=i-0d5a000bf975b2402 --filters
"Name=tag:Name,Values=*"
Linux/Mac only:List the required info of ec2 instances
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`]
| [0].Value, InstanceId, State.Name, PrivateIpAddress, PublicIpAddress ]' --output
table

aws ec2 create-tags --resources i-0d5a000bf975b2402 --tags Key=Name,Value=may19-


anil

aws ec2 describe-instances --filter Name=instance-type,Values=t2.micro -filters


"Name=tag:Name,Values=may19-anil"

Home Work/Self practice in your account:

aws ec2 create-volume --size 1 --region us-west-2 --availability-zone us-west-2c


--volume-type gp2

aws ec2 attach-volume --volume-id vol-0f69888cb25d25fc4 --instance-id i-


0d5a000bf975b2402 --device /dev/sdf

====================For additional reference ================


aws elb create-load-balancer --load-balancer-name my-load-balancer --listeners
"Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=80"
--security-
groups sg-0fd60889e58e88746 --availability-zones us-west-2a us-west-2b

aws autoscaling create-launch-configuration --launch-configuration-name my-launch-


config --key-name cloudclass --image-id ami-09ba526ae945df22e --security-groups
sg-0fd60889e58e88746 --instance-type t2.micro

aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-auto-


scaling-group --launch-configuration-name my-launch-config --load-balancer-names
my-load-balancer --health-check-type ELB --health-check-grace-period 300 --default-
cooldown 300 --min-size 1 --max-size 2 --desired-capacity 1 --availability-zones
us-west-2a us-west-2b

aws autoscaling set-instance-health --instance-id i-08e88842e8ee3cb8f --health-


status Unhealthy

==============================================List-instances-running -in all aws


regions==================================
Usage examples:
sudo ./aws-x.sh ec2 describe-instances
sudo ./aws-x.sh ec2 describe-instances | grep PrivateDnsName

#!/bin/bash

echo "Getting AWS Avaiable Regions:"


sudo aws ec2 describe-regions --region eu-west-2 |grep RegionName |cut -d '"' -f 4
> /tmp/regions.txt
cat /tmp/regions.txt

#Start executing
while read region; do
echo "Executing " $@ " on " $region ":"
aws --region $region $@
done</tmp/regions.txt
#cleanup
rm /tmp/regions.txt

reference:===========================additional
commands============================
aws ec2 describe-images --filters "Name=tag:Name,Values=AUTO_SCALING-GOLDCOPY2018"

aws ec2 describe-images --region us-west-2 --filter "Name=is-public,Values=false"


--filter "Name=architecture, Values=x86_64" --query "Images[*].{size:
[BlockDeviceMappings[].Ebs.VolumeSize],date:CreationDate}"

aws ec2 describe-images --owner amazon --query 'Images[?Name!=`null`]|[?


starts_with(Name, `aws-elasticbeanstalk`) == `true`]|[?contains(Name,
`tomcat7java6-pv`) == `true`].[CreationDate,ImageId,Name]' --output text

Filtering to get instance Name and ID alone:


aws ec2 describe-instances --output text --query 'Reservations[].Instances[].
[Tags[?Key==`Name`].Value|[0],InstanceId]'

Filtering to get Instance ID and Private IP only


aws ec2 describe-instances --output table --query 'Reservations[].Instances[] | [?
Placement.AvailabilityZone==`us-west-2a`] | [].{ID: InstanceId, IP:
PrivateIpAddress}'

listing instances with no Name tag -- all details


aws ec2 describe-instances --query 'Reservations[].Instances[?!not_null(Tags[?Key
== `Name`].Value)] | []' --output text

listing instances with no Name tag -- just instance id


aws ec2 describe-instances \
--output text \
--query 'Reservations[].Instances[?!not_null(Tags[?Key == `Name`].Value)] | [].
[InstanceId]'

running alone:
aws ec2 describe-instances \
--output text \
--filters Name=instance-state-name,Values=running \
--query 'Reservations[].Instances[?!not_null(Tags[?Key == `Owner`].Value)] | [].
[InstanceId]'

selects from one region


queries several fields including two custom tags, in my case “Name” and
“instance_role”
wildcard filter on custom tag in my case Name like *TEST*
outputs as easy to read sorted table
aws ec2 describe-instances --output table --region us-west-2
--query 'Reservations[].Instances[].[ Tags[?Key==`Name`].Value | [0], Tags[?
Key==`instance_role`].Value | [0], PublicIpAddress, PrivateIpAddress, State.Name,
Placement.AvailabilityZone, InstanceId, InstanceType, LaunchTime ]' --filters
'Name=tag:Name,Values=*TEST*' | sort -n -k 2

=================================================================================
Locate the AWS Completer
The location can vary depending on the installation method used.

$ which aws_completer
Sample Output: /usr/local/bin/aws_completer
Enable Command Completion
Run a command to enable command completion. The command that you use to enable
completion depends on the shell that you are using. You can add the command to your
shell's RC file to run it each time you open a new shell.

bash – use the built-in command complete.

$ complete -C '/usr/local/bin/aws_completer' aws

Add the command to ~/.bashrc to run it each time you open a new shell. Your
~/.bash_profile should source ~/.bashrc to ensure that the command is run in login
shells as well.

=================================================================================
Using an HTTP Proxy
If you need to access AWS through proxy servers, you should configure the
HTTP_PROXY and HTTPS_PROXYenvironment variables with the IP addresses for your
proxy servers.

Linux, macOS, or Unix

$ export HTTP_PROXY=http://a.b.c.d:n
$ export HTTPS_PROXY=http://w.x.y.z:m

Windows

> set HTTP_PROXY=http://a.b.c.d:n


> set HTTPS_PROXY=http://w.x.y.z:m

In these examples, http://a.b.c.d:n and http://w.x.y.z:m are the IP addresses and


ports for the HTTP and HTTPS proxies.

Authenticating to a Proxy
The AWS CLI supports HTTP Basic authentication. Specify a username and password in
the proxy URL like this:

Linux, macOS, or Unix

$ export HTTP_PROXY=http://username:[email protected]:n
$ export HTTPS_PROXY=http://username:[email protected]:m

Windows

> set HTTP_PROXY=http://username:[email protected]:n


> set HTTPS_PROXY=http://username:[email protected]:m

Note

The AWS CLI does not support NTLM proxies. If you use an NTLM or Kerberos proxy,
you may be able to connect through an authentication proxy like Cntlm.
Thanks and regards,

Anil Kumar

You might also like