如何在Amazon EC2中设置环境变量

PJ *_*ron 32 debian environment-variables amazon-ec2 amazon-web-services

我在AWS控制台上为我的一个EC2实例创建了一个标记.

在此输入图像描述

但是,当我查看服务器时,没有设置此类环境变量.

弹性豆茎也是如此.env显示我在控制台上创建的标签.

$ env
 [...]
 DB_PORT=5432
Run Code Online (Sandbox Code Playgroud)

如何在Amazon EC2中设置环境变量?

Guy*_*Guy 28

您可以从元数据中检索此信息,然后运行您自己的set environment命令.

您可以从元数据中获取实例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
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用预安装的AWS CLI调用describe-tags(或将其安装在AMI上)

aws ec2 describe-tags --filters "Name=resource-id,Values=i-5f4e3d2a" "Name=Value,Values=DB_PORT"
Run Code Online (Sandbox Code Playgroud)

然后您可以使用OS set environment variable命令

export DB_PORT=/what/you/got/from/the/previous/call
Run Code Online (Sandbox Code Playgroud)

您可以在用户数据脚本中运行所有这些操作.详情请见:http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html

  • 我建议您为实例使用IAM角色 - 这将允许您在不指定凭据的情况下执行API调用.Guy给出的答案正是我们在生产中使用的答案. (6认同)

小智 7

如果您的 ec2 实例使用 Linux 或 Mac 操作系统,

进入根目录并写入命令:

vim .bash_profile
Run Code Online (Sandbox Code Playgroud)

您可以看到您的 bash_profile 文件,现在按“i”插入一行,然后添加

export DB_PORT="5432"
Run Code Online (Sandbox Code Playgroud)

添加此行后,您需要保存文件,因此按“Esc”按钮,然后按“:”,在冒号后写入“w”,它将保存文件而不退出。

要退出,请在写入“退出”后再次按“:”,现在您将从文件中退出。

运行source ~/.bash_profile以确保立即应用更改并更新当前 shell 会话的环境

要检查您的环境变量是否已设置,请编写以下命令:

python
>>>import os
>>>os.environ.get('DB_PORT')
>>>5432 
Run Code Online (Sandbox Code Playgroud)


Mar*_*les 6

我使用了以下工具的组合:

  • 安装jq库(sudo apt-get install -y jq)
  • 安装EC2实例元数据查询工具

以下代码的要点如果我将来更新它: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"
Run Code Online (Sandbox Code Playgroud)


小智 6

最近,AWS Parameter Store 似乎是一个更好的解决方案。

现在甚至还有一个秘密管理器,它可以自动管理敏感配置,如数据库密钥等。

请参阅使用基于GuyPJ Bergeron先前解决方案的 SSM Parameter Store 的脚本。

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


PJ *_*ron 4

按照Guy给出的说明,我编写了一个小 shell 脚本。该脚本使用 AWS CLI 和jq. 它允许您将 AWS 实例和 AMI 标签作为 shell 环境变量导入。

我希望它可以帮助一些人。

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