How to set an environment variable in Amazon EC2

2019-01-31 00:22发布

问题:

I created a tag on the AWS console for one of my EC2 instances.

However, when I look on the server, no such environment variable is set.

The same thing works with elastic beanstalk. env shows the tags I created on the console.

$ env
 [...]
 DB_PORT=5432

How can I set environment variables in Amazon EC2?

回答1:

You can retrieve this information from the meta data and then run your own set environment commands.

You can get the instance-id from the meta data (see here for details: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html#instancedata-data-retrieval)

curl http://169.254.169.254/latest/meta-data/instance-id

Then you can call the describe-tags using the pre-installed AWS CLI (or install it on your AMI)

aws ec2 describe-tags --filters "Name=resource-id,Values=i-5f4e3d2a" "Name=Value,Values=DB_PORT"

Then you can use OS set environment variable command

export DB_PORT=/what/you/got/from/the/previous/call

You can run all that in your user-data script. See here for details: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html



回答2:

I used a combination of the following tools:

  • Install jq library (sudo apt-get install -y jq)
  • Install the EC2 Instance Metadata Query Tool

Here's the gist of the code below in case I update it in the future: https://gist.github.com/marcellodesales/a890b8ca240403187269

######
# Author: Marcello de Sales (marcello.desales@gmail.com)
# Description: Create Create Environment Variables in EC2 Hosts from EC2 Host Tags
# 
### Requirements:  
# * Install jq library (sudo apt-get install -y jq)
# * Install the EC2 Instance Metadata Query Tool (http://aws.amazon.com/code/1825)
#
### Installation:
# * Add the Policy EC2:DescribeTags to a User
# * aws configure
# * Souce it to the user's ~/.profile that has permissions
#### 
# REboot and verify the result of $(env).

# Loads the Tags from the current instance
getInstanceTags () {
  # http://aws.amazon.com/code/1825 EC2 Instance Metadata Query Tool
  INSTANCE_ID=$(./ec2-metadata | grep instance-id | awk '{print $2}')

  # Describe the tags of this instance
  aws ec2 describe-tags --region sa-east-1 --filters "Name=resource-id,Values=$INSTANCE_ID"
}

# Convert the tags to environment variables.
# Based on https://github.com/berpj/ec2-tags-env/pull/1
tags_to_env () {
    tags=$1

    for key in $(echo $tags | /usr/bin/jq -r ".[][].Key"); do
        value=$(echo $tags | /usr/bin/jq -r ".[][] | select(.Key==\"$key\") | .Value")
        key=$(echo $key | /usr/bin/tr '-' '_' | /usr/bin/tr '[:lower:]' '[:upper:]')
        echo "Exporting $key=$value"
        export $key="$value"
    done
}

# Execute the commands
instanceTags=$(getInstanceTags)
tags_to_env "$instanceTags"


回答3:

Following the instructions given by Guy, I wrote a small shell script. This script uses AWS CLI and jq. It lets you import your AWS instance and AMI tags as shell environment variables.

I hope it can help a few people.

https://github.com/12moons/ec2-tags-env



回答4:

Lately, it seems AWS Parameter Store is a better solution.

Now there is even a secrets manager which auto manages sensitive configurations as database keys and such..

See this script using SSM Parameter Store based of the previous solutions by Guy and PJ Bergeron.

https://github.com/lezavala/ec2-ssm-env



回答5:

I normally load tags as environment variables on boot by running a UserData script. Depending on the instance, I change the --query and --filter parameters to the describe-instances call but otherwise the script stays the same. NOTE: The example below excludes the tag Name and tags containing : - change this behaviour to suit your needs.

#!/bin/bash -v
apt-get update
apt-get -y install awscli

# add boot script which loads environment variables
cat > /etc/profile.d/export_instance_tags.sh << 'EndOfMessage'
# fetch instance info
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
INSTANCE_AZ=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
INSTANCE_REGION="`echo \"$INSTANCE_AZ\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"

# export instance tags
export_statement=$(aws ec2 describe-tags --region "$INSTANCE_REGION" --filters "Name=resource-id,Values=$INSTANCE_ID" --query 'Tags[?!contains(Key, `Name`) && !contains(Key, `:`)].[Key,Value]' --output text | sed -E 's/^([^\s\t]+)[\s\t]+([^\n]+)$/export \1="\2"/g')
eval $export_statement

# export instance info
export INSTANCE_ID
export INSTANCE_AZ
export INSTANCE_REGION
EndOfMessage

It runs describe-tags to list all tags, reformats the output to a sequence of export statements with sed then runs the result using eval