Skip to content

Instantly share code, notes, and snippets.

@dgulinobw
Last active February 20, 2019 15:59
Show Gist options
  • Select an option

  • Save dgulinobw/9a0ee6a142d6b3ae8e43b9967b2a95ee to your computer and use it in GitHub Desktop.

Select an option

Save dgulinobw/9a0ee6a142d6b3ae8e43b9967b2a95ee to your computer and use it in GitHub Desktop.
ec2_instance_id_from_name.sh
#!/bin/bash
InstanceName=$1
#for region in `aws ec2 describe-regions --output text | cut -f3` #unoptimized list order, but dynamic
for region in us-east-1 us-east-2 us-west-1 us-west-2 eu-north-1 ap-south-1 eu-west-3 eu-west-2 eu-west-1 ap-northeast-2 ap-northeast-1 sa-east-1 ca-central-1 ap-southeast-1 ap-southeast-2 eu-central-1;
do
Id=$(aws ec2 describe-instances --region $region --filters Name=tag-value,Values=$InstanceName | jq -r .Reservations[].Instances[].InstanceId)
if [ "$Id" != "" ];
then
echo $Id
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment