Skip to content

Instantly share code, notes, and snippets.

@skwp
Created October 2, 2018 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skwp/5113cb51ec5f7ad1843e70d730c8bd13 to your computer and use it in GitHub Desktop.
Save skwp/5113cb51ec5f7ad1843e70d730c8bd13 to your computer and use it in GitHub Desktop.
AWS instance reads its own tags
# Magic AWS ip that gives you metadata about the instance
METADATA="http://169.254.169.254/latest"
# Read the AWS tag for the netmagic value that will control which
# Test network this instance will join
INSTANCE_ID="$(curl -s $METADATA/meta-data/instance-id)"
REGION="$(curl -s $METADATA/dynamic/instance-identity/document | grep region | awk -F\" '{print $4}')"
AWS_ACCOUNT="$(curl -s $METADATA/dynamic/instance-identity/document | grep accountId | awk -F\" '{print $4}')"
ECR_REGION=us-east-1
MY_IP=$(curl -s $METADATA/meta-data/public-ipv4)
# Grab all the tags on the instance and turn them into a list that looks like
# TAG_key=value
INSTANCE_TAGS=$(/usr/local/bin/aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" --region=$REGION | jq -r '.Tags[] | "TAG_\(.Key)=\"\(.Value)\""')
# Now load the tags into the environment so we can use them as variables below
eval $INSTANCE_TAGS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment