我在 AWS 控制台上为我的一个 EC2 实例创建了一个标签。
但是,当我查看服务器时,没有设置这样的环境变量。
同样的事情也适用于弹性豆茎。
env
显示我在控制台上创建的标签。
$ env
[...]
DB_PORT=5432
如何在 Amazon EC2 中设置环境变量?
您可以从元数据中检索此信息,然后运行您自己设置的环境命令。
您可以从元数据中获取实例 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
然后您可以使用预安装的 AWS CLI 调用描述标签(或将其安装在您的 AMI 上)
aws ec2 describe-tags --filters "Name=resource-id,Values=i-5f4e3d2a" "Name=Value,Values=DB_PORT"
然后就可以使用OS设置环境变量命令了
export DB_PORT=/what/you/got/from/the/previous/call
您可以在用户数据脚本中运行所有这些。有关详细信息,请参阅此处:http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
最近,AWS Parameter Store 似乎是一个更好的解决方案。
现在甚至还有一个秘密管理器,可以自动管理数据库密钥等敏感配置..
请参阅使用基于 Guy 和 PJ Bergeron 之前的解决方案的 SSM 参数存储的脚本。
我使用了以下工具的组合:
这是下面代码的要点,以防我将来更新它:https://gist.github.com/marcellodesales/a890b8ca240403187269
######
# Author: Marcello de Sales ([email protected])
# 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"
如果您的 ec2 实例使用 Linux 或 Mac 操作系统,
进入根目录并写入命令:
vim .bash_profile
您可以看到您的 bash_profile 文件,现在按“i”插入一行,然后添加
export DB_PORT="5432"
添加此行后,您需要保存文件,因此按“Esc”按钮,然后按“:”,在冒号后写“w”,它将保存文件而不退出。
要退出,请在写入“退出”后再次按“:”,现在您将从文件中退出。
运行
source ~/.bash_profile
以确保立即应用更改并更新当前 shell 会话的环境
要检查您的环境变量是否已设置,请编写以下命令:
python
>>>import os
>>>os.environ.get('DB_PORT')
>>>5432
如果您的 ec2 实例使用 Linux 或 mac 操作系统,请转到根目录并运行以下命令:
vim .bash_profile
您将看到您的 bash_profile 文件在文本编辑器“vim”中打开。接下来按“i”进入“插入”模式(或按“o”在新行上进入插入模式),然后附加以下内容:
export DB_PORT="5432"
添加此行后,您需要保存文件。因此,按“Esc”键退出插入模式,然后按“:”启动新命令,在冒号后写入“w”,然后按 Enter 保存文件。它将保存文件而不退出。要退出,请在写入“quit”后再次按“:”,文件将关闭。或者,您可以输入“x”而不是“w”来一次性保存并关闭文件。
请参阅此处获取 vim 备忘单。
之后在 shell 中运行命令“source ~/.bash_profile”。 要检查您的环境变量是否已设置,请在 shell 中输入以下命令以通过 python 检查:
python
>>>import os
>>>os.environ.get('DB_PORT')
>>>5432
或在您的 shell 中执行以下内容以进行本机检查:
echo DB_PORT