Thursday, June 9, 2016

Finding stuff in aws cli


Finding default routes: aws ec2 describe-route-tables --query "RouteTables[*].Routes[?DestinationCidrBlock==\`\`]"
Finding Routes that terminate in instances: aws ec2 describe-route-tables --query "RouteTables[*].Routes[?DestinationCidrBlock==\`\`].{Destination:DestinationCidrBlock, Instance:InstanceId, State:State}"
Finding the VPC name for a route that terminates on an instance: 
aws ec2 describe-vpcs --vpc-ids $(aws ec2 describe-route-tables  | 
     jq '.RouteTables[] |  { VpcId, Instance: .Routes[] | 
       select( .InstanceId != null)  }' | 
     jq '.VpcId' | sed 's/\"//g')  |
     jq '.Vpcs[] |
       { VpcId, Name: .Tags[] |
       select ( .Key == "Name") }'
Finding the newest snapshot
aws --profile=prod ec2 describe-snapshots --filter 'Name=volume-id,Values=vol-1c6e8b1a' | jq '.[]|max_by(.StartTime)|.SnapshotId'

Find out how many 0b files in an s3 bucket

aws s3 ls --recursive s3://<folder_name> | awk '{if ($3 == 0) print}'

In my new gig, I'm learning lots more AWS, so I'll be sharing some AWS love here now. The last few weeks, I have been replacing NAT instances with NAT Gateways. It is a great way for your VPC VM's to get to the internet without having to manage a VM.

Well, I thought I was done so I decided to find a way to audit my assumptions. Well, I was wrong. The TL;DR above shows what it took to find the default routes that terminated specifically in instances, then from there find the name of the VPC. Now that I have found those, I know how many more NAT gateways I need to build. 

Based on a recommendation from a coworker, I add jq which is a very powerful JSON processor that blends awk, sed and grep for JSON objects. It was a bit complex to learn, but now that I have some skills, it will be very handy in the future!

1 comment: