Skip to content

Instantly share code, notes, and snippets.

@dgulinobw
Created February 20, 2019 16:15
Show Gist options
  • Select an option

  • Save dgulinobw/94e174d337b92eff11b92d9681b3e308 to your computer and use it in GitHub Desktop.

Select an option

Save dgulinobw/94e174d337b92eff11b92d9681b3e308 to your computer and use it in GitHub Desktop.
ec2_instance_name_from_id.sh
#!/bin/bash
InstanceId=$1
#for region in `aws ec2 describe-regions --output text | cut -f3` #unoptimized list order
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
Name=$(aws ec2 describe-instances --region $region --instance-ids=$InstanceId | jq -r '.Reservations[].Instances[].Tags[] | select(.Key=="Name").Value')
if [ "$Name" != "" ];
then
echo $Name
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment