Skip to content

Instantly share code, notes, and snippets.

@WintersMichael
Created February 14, 2017 19:20
Show Gist options
  • Save WintersMichael/9de7e1781723d60e6ae34016b9185180 to your computer and use it in GitHub Desktop.
Save WintersMichael/9de7e1781723d60e6ae34016b9185180 to your computer and use it in GitHub Desktop.
Bash to get Name tag on EC2 instances
# Gets any tag value, default 'Name'. Assumes that `aws` CLI is
# installed, and the IAM role has Describe-Tags permissions.
TAG=Name
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep region | awk -F\" '{print $4}')
TAG_VALUE=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" "Name=key,Values=$TAG" --region=$REGION --output=text | cut -f5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment