I created a tag on the AWS console for one of my EC2 instances.
我在AWS控制台上为我的一个EC2实例创建了一个标记。
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显示我在控制台上创建的标签。
$ env
[...]
DB_PORT=5432
How can I set environment variables in Amazon EC2?
如何在Amazon EC2中设置环境变量?
5 个解决方案
#1
20
You can retrieve this information from the meta data and then run your own set environment commands.
您可以从元数据中检索此信息,然后运行您自己的set environment命令。
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)
您可以从元数据中获取实例ID(有关详细信息,请参阅此处: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 CLI调用describe-tags(或将其安装在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
然后您可以使用OS set environment variable命令
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
您可以在用户数据脚本中运行所有这些操作。详情请见:http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
#2
4
I used a combination of the following tools:
我使用了以下工具的组合:
- Install jq library (sudo apt-get install -y jq)
- 安装jq库(sudo apt-get install -y jq)
- Install the EC2 Instance Metadata Query Tool
- 安装EC2实例元数据查询工具
Here's the gist of the code below in case I update it in the future: https://gist.github.com/marcellodesales/a890b8ca240403187269
以下代码的要点如果我将来更新它: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
2
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.
按照Guy给出的说明,我写了一个小的shell脚本。此脚本使用AWS CLI和jq。它允许您将AWS实例和AMI标记导入为shell环境变量。
I hope it can help a few people.
我希望它可以帮助一些人。
https://github.com/12moons/ec2-tags-env
https://github.com/12moons/ec2-tags-env
#4
1
Lately, it seems AWS Parameter Store is a better solution.
最近,似乎AWS参数存储是一个更好的解决方案。
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.
根据Guy和PJ Bergeron之前的解决方案,使用SSM参数存储查看此脚本。
https://github.com/lezavala/ec2-ssm-env
https://github.com/lezavala/ec2-ssm-env
#5
0
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.
我通常通过运行UserData脚本在启动时将标记加载为环境变量。根据实例,我将--query和--filter参数更改为describe-instances调用,否则脚本保持不变。注意:以下示例排除标记名称和包含以下内容的标记: - 更改此行为以满足您的需要。
#!/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
它运行describe-tags列出所有标签,使用sed将输出重新格式化为一系列导出语句,然后使用eval运行结果
#1
20
You can retrieve this information from the meta data and then run your own set environment commands.
您可以从元数据中检索此信息,然后运行您自己的set environment命令。
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)
您可以从元数据中获取实例ID(有关详细信息,请参阅此处: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 CLI调用describe-tags(或将其安装在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
然后您可以使用OS set environment variable命令
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
您可以在用户数据脚本中运行所有这些操作。详情请见:http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
#2
4
I used a combination of the following tools:
我使用了以下工具的组合:
- Install jq library (sudo apt-get install -y jq)
- 安装jq库(sudo apt-get install -y jq)
- Install the EC2 Instance Metadata Query Tool
- 安装EC2实例元数据查询工具
Here's the gist of the code below in case I update it in the future: https://gist.github.com/marcellodesales/a890b8ca240403187269
以下代码的要点如果我将来更新它: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
2
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.
按照Guy给出的说明,我写了一个小的shell脚本。此脚本使用AWS CLI和jq。它允许您将AWS实例和AMI标记导入为shell环境变量。
I hope it can help a few people.
我希望它可以帮助一些人。
https://github.com/12moons/ec2-tags-env
https://github.com/12moons/ec2-tags-env
#4
1
Lately, it seems AWS Parameter Store is a better solution.
最近,似乎AWS参数存储是一个更好的解决方案。
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.
根据Guy和PJ Bergeron之前的解决方案,使用SSM参数存储查看此脚本。
https://github.com/lezavala/ec2-ssm-env
https://github.com/lezavala/ec2-ssm-env
#5
0
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.
我通常通过运行UserData脚本在启动时将标记加载为环境变量。根据实例,我将--query和--filter参数更改为describe-instances调用,否则脚本保持不变。注意:以下示例排除标记名称和包含以下内容的标记: - 更改此行为以满足您的需要。
#!/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
它运行describe-tags列出所有标签,使用sed将输出重新格式化为一系列导出语句,然后使用eval运行结果