Skip to content

Instantly share code, notes, and snippets.

@rafaelfelini
Created September 22, 2014 17:18
Show Gist options
  • Save rafaelfelini/5467c3a69d72c677bbed to your computer and use it in GitHub Desktop.
Save rafaelfelini/5467c3a69d72c677bbed to your computer and use it in GitHub Desktop.
Get aws ec2 instance name via tag.
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
TAG_NAME="Name"
INSTANCE_ID="`wget -qO- http://instance-data/latest/meta-data/instance-id`"
REGION="`wget -qO- http://instance-data/latest/meta-data/placement/availability-zone | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
TAG_VALUE="`aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" "Name=key,Values=$TAG_NAME" --region $REGION --output=text | cut -f3`"
echo "hostname=$TAG_VALUE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment