Skip to content

Instantly share code, notes, and snippets.

@nvogel nvogel/aws-cli.md

Last active Jun 12, 2019
Embed
What would you like to do?
aws-cli

References

Region and az

List regions

aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text

List az :

aws ec2 describe-availability-zones --query "*[].{Name:ZoneName}" --output text --region=us-east-1

List all regions and az :

for r in $(aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text); do echo "****** $r ********" ; aws ec2 describe-availability-zones --query "*[].{Name:ZoneName}" --output text --region=$r ; done

Images

Amazon Linux Ami

List by date Amazon linux ami matching the pattern "amzn-ami-hvm-*-x86_64-gp2"

aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn-ami-hvm-*-x86_64-gp2" --query 'sort_by(Images, &CreationDate)[].[Name, ImageId, Description]'

Or if you want to find only the latest :

aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2

The latest CentOS AMI image in your region

Based on the fact that the Owner ID for the official CentOS organisation remains 679593333241 (https://aws.amazon.com/marketplace/seller-profile?id=16cb8b03-256e-4dde-8f34-1b0f377efe89)

aws ec2 describe-images \
  --owners 679593333241 \
  --filters \
      Name=name,Values='CentOS Linux 7 x86_64 HVM EBS*' \
      Name=architecture,Values=x86_64 \
      Name=root-device-type,Values=ebs \
  --query 'sort_by(Images, &CreationDate)[-1].ImageId' \
  --output text

Snapshots

List my snapshots with Name,Id and Time

aws ec2 describe-snapshots --query 'Snapshots[*].{ID:SnapshotId,Time:StartTime,Name:Tags[?Key==`Name`]|[0].Value}' --owner-ids self

Instances

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

Describe instances with Azs that have a particular tag and value :

aws ec2 describe-instances --output table --query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value,InstanceId,InstanceType,State.Name,Placement.AvailabilityZone,PublicIpAddress,PrivateIpAddress]' --filters "Name=tag:Role,Values=Slave"

List number of running instances by type :

aws ec2 describe-instances \
        --filter Name=instance-state-name,Values=running \
        --query 'Reservations[].Instances[].{InstanceType:InstanceType}' \
        | jq -r '.[].InstanceType' \
        | sort \
        | uniq -c \
        | sort -r;

Iam

Assume Role function

function assume_role() {
local role=$1
duration=$2

unset AWS_SECRET_ACCESS_KEY ; unset AWS_ACCESS_KEY_ID ; unset AWS_SESSION_TOKEN
role_data=$(aws sts assume-role --role-arn $role --role-session-name $(date "+%m%d%H%M%Y") --duration-seconds $duration)
echo "Process assume role json outut"
eval $(
    jq -r ' "export AWS_SECRET_ACCESS_KEY=" + .Credentials.SecretAccessKey +
            "\nexport AWS_SESSION_TOKEN=" + .Credentials.SessionToken +
            "\nexport AWS_ACCESS_KEY_ID=" + .Credentials.AccessKeyId
          ' <<<"$role_data"
          )
echo "Switched to role ${role}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.