Skip to content

Instantly share code, notes, and snippets.

@jeffjohnson9046
Last active October 10, 2022 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeffjohnson9046/86fc09b13a776b0169358786d7782073 to your computer and use it in GitHub Desktop.
Save jeffjohnson9046/86fc09b13a776b0169358786d7782073 to your computer and use it in GitHub Desktop.
Look up an EC2 Instance by public IP across more than one region
# This is the function I created that's a wee bit less shitty than the file above. I created a file called (oddly enough)
# ec2-instance-query.zsh and put it in my ~/.oh-my-zsh/custom directory. After sourcing my ~/zshrc, I have a fancy-pants
# function for querying EC2 instances and ElasticIP addresses for a given IP:
function ec2-instance-query() {
echo "Finding EC2 information for IP $1:"
for r in `aws ec2 describe-regions --output text | cut -f4 | grep "us-"`
do
aws ec2 describe-instances \
--region $r \
--filter "Name=network-interface.addresses.association.public-ip, Values=$1" \
--query 'Reservations[].Instances[].{ commonName: Tags[?Key == `Name`]|[0].Value, instanceId: InstanceId, privateIp: PrivateIpAddress, publicIp: PublicIpAddress, zone: Placement.AvailabilityZone, status: State.Name, startedAt: LaunchTime, keyPair: KeyName }' \
--output table
aws ec2 describe-addresses \
--region $r \
--public-ips $1 \
--query 'Addresses[].{ instanceId: InstanceId, publicIp: PublicIp, privateIp: PrivateIp, allocationId: AllocationId, associationId: AssociationId, zone: NetworkBorderGroup }' \
--output table 2> /dev/null # suppress the "Address not found" error (and...well... anything else, really)
done
}
# According to a brief search on the interwebs, it isn't possible to query for EC2 instances across multiple regions;
# you have to query against each region separately. This is an INCREDIBLY quick and dirty command for using the aws cli
# to look up an EC2 instance by public IP in two different regions. Eventually this will get put into something more
# robust, but for now here it is:
# Set the public IP address to something:
export IP=<the public IP address you're looking for>
# Run describe-instances against the us-west-2 and us-east-2 region and fetch some basic information:
aws ec2 describe-instances \
--region us-west-2 \
--filter "Name=network-interface.addresses.association.public-ip, Values=${IP}" \
--query 'Reservations[].Instances[].{ commonName: Tags[?Key == `Name`]|[0].Value, privateIp: PrivateIpAddress, publicIp: PublicIpAddress, zone: Placement.AvailabilityZone, status: State.Name, startedAt: LaunchTime, keyPair: KeyName }' \
--output table \
&& \
aws ec2 describe-instances \
--region us-east-2 \
--filter "Name=network-interface.addresses.association.public-ip, Values=${IP}" \
--query 'Reservations[].Instances[].{ commonName: Tags[?Key == `Name`]|[0].Value, privateIp: PrivateIpAddress, publicIp: PublicIpAddress, zone: Placement.AvailabilityZone, status: State.Name, startedAt: LaunchTime, keyPair: KeyName }' \
--output table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment