Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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