Skip to content

Instantly share code, notes, and snippets.

@gmoon
Last active February 13, 2024 13:32
Show Gist options
  • Save gmoon/d8fe5bc59db43ea21cca731fe543fcec to your computer and use it in GitHub Desktop.
Save gmoon/d8fe5bc59db43ea21cca731fe543fcec to your computer and use it in GitHub Desktop.
Inject AWS instance metadata into environment variables on your EC2 instance
# Inject environment variables into current shell
# Note: using jq's from_entries on the Key/Value pairs (capital K and V) format of Tags requires version 1.5 or higher
export INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
export STACK_NAME=`aws ec2 describe-instances --instance-ids $INSTANCE_ID --region us-east-1 \
| jq --raw-output '.Reservations[0].Instances[0].Tags | from_entries | .["aws:cloudformation:stack-name"]'`
# Make it permanent
echo export INSTANCE_ID=$INSTANCE_ID > /etc/profile.d/awsinstance.sh
echo export STACK_NAME=$STACK_NAME >> /etc/profile.d/awsinstance.sh
@avi7611
Copy link

avi7611 commented Nov 8, 2022

can we prepare something like this that will inject all the available metadata into env variables?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment