Skip to content

Instantly share code, notes, and snippets.

@shiplu
Created December 15, 2016 10:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save shiplu/a203bcddef5926c56240aa660c4493e5 to your computer and use it in GitHub Desktop.
Lists all the ec2 instance's ip by name. The name can be a glob pattern, means if its '*a' then it'll match any instance that has name ending with letter a. It outputs only the PrivateIpAddress. But if you can also use PublicIpAddress.
#!/bin/bash
IP_SOURCE=PrivateIpAddress
aws ec2 describe-instances --output text \
--query 'Reservations[*].Instances[*].{ip:['$IP_SOURCE'],name:Tags[?Key==`Name`].Value}' \
--filter "Name=tag:Name,Values=$1" |
awk '$1 ~ /IP/{ip=$2} $1 ~ /NAME/{printf "%-15s %s\n", ip, $2}' |
sort -k2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment