Last active
February 13, 2024 13:32
-
-
Save gmoon/d8fe5bc59db43ea21cca731fe543fcec to your computer and use it in GitHub Desktop.
Inject AWS instance metadata into environment variables on your EC2 instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can we prepare something like this that will inject all the available metadata into env variables?