如何在部署时使用 Elastic Beanstalk 指定敏感环境变量
How to specify sensitive environment variables at deploy time with Elastic Beanstalk
我正在使用 Elastic Beanstalk 部署 Python Flask 应用程序。我有一个配置文件 /.ebextensions/01.config
,其中我设置了一些环境变量——其中一些应该是秘密的。
文件看起来像这样:
packages:
yum:
gcc: []
git: []
postgresql93-devel: []
option_settings:
"aws:elasticbeanstalk:application:environment":
SECRET_KEY: "sensitive"
MAIL_USERNAME: "sensitive"
MAIL_PASSWORD: "sensitive"
SQLALCHEMY_DATABASE_URI: "sensitive"
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "app/static/"
将某些值保密的最佳做法是什么?目前 .ebextensions
文件夹处于源代码管理之下,我喜欢它,因为它与所有人共享,但与此同时,我不想将敏感值置于源代码管理之下。
有没有办法在部署时通过 EB CLI 工具指定一些环境变量(例如 eb deploy -config ...
)?或者 AWS 部署工具如何涵盖此用例?
您应该能够从 eb web 控制台将敏感值指定为环境变量:您的 EB 应用程序 -> 您的 EB 环境 -> 配置 -> 软件配置 -> 环境属性
或者,您可以使用这个:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-setenv.html
编辑:虽然这是 2015 年被接受的答案,但这不应该是你处理它的方式。现在您可以为此目的使用 AWS Secrets Manager。
我一直在使用另一个 shell 脚本 ./deploy_production.sh 来设置环境特定变量。在 shell 脚本中,您可以使用 "eb setenv NAME1=VAR1 NAME2=VAR2 ..." 设置环境变量。
并且此文件不需要进入 git 存储库。
这个问题已经有了答案,但我想为这个问题贡献一个替代解决方案。我不必将秘密保存在环境变量中(然后必须在版本控制之外的某个地方进行管理和存储,而且你需要记住在部署时设置它们),我将所有秘密放在一个加密的 S3 存储桶中,只能从EB 的角色是 运行 作为。然后我在启动时获取秘密。这样做的好处是可以将部署与配置完全分离,并且您永远不必再在命令行中 fiddle 使用秘密。
如果需要(例如,如果在应用程序设置期间需要机密,例如获取代码的存储库的密钥),您还可以使用带有 S3Auth
指令的 .ebextensions
配置文件来轻松将上述 S3 存储桶的内容复制到您的本地实例;否则只需使用 AWS SDK 在启动时从应用程序中获取所有机密。
编辑:截至 2018 年 4 月,AWS 为机密管理提供专用托管服务; AWS Secrets Manager。它以字符串或 json 格式、版本控制、阶段、轮换等方式方便地安全存储秘密。它还消除了一些涉及 KMS、IAM 等的配置,以加快设置速度。我认为没有真正的理由使用任何其他 AWS 服务来存储静态敏感数据,例如私钥、密码等。
AWS documentation recommends storing sensitive information in S3因为环境变量可能以多种方式公开:
Providing connection information to your application with environment
properties is a good way to keep passwords out of your code, but it's
not a perfect solution. Environment properties are discoverable in the
Environment Management Console, and can be viewed by any user that has
permission to describe configuration settings on your environment.
Depending on the platform, environment properties may also appear in
instance logs.
下面的例子来自文档,您应该参考它以获得完整的细节。简而言之,您需要:
- 使用最小权限将文件上传到 S3,possibly encrypted。
授予对您的 Elastic Beanstalk 自动缩放组的实例配置文件角色的读取权限。该政策如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "database",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::my-secret-bucket-123456789012/beanstalk-database.json"
]
}
]
}
在您的应用程序包根目录中将名称类似于 s3-connection-info-file.config
的文件添加到 /.ebextensions
,其中包含以下内容:
Resources:
AWSEBAutoScalingGroup:
Metadata:
AWS::CloudFormation::Authentication:
S3Auth:
type: "s3"
buckets: ["my-secret-bucket-123456789012"]
roleName: "aws-elasticbeanstalk-ec2-role"
files:
"/tmp/beanstalk-database.json" :
mode: "000644"
owner: root
group: root
authentication: "S3Auth"
source: https://s3-us-west-2.amazonaws.com/my-secret-bucket-123456789012/beanstalk-database.json
然后更新您的应用程序代码以从文件 /tmp/beanstalk-database.json
(或您决定将其放入实际配置的任何位置)中提取值。
其他一些答案提到使用 Parameter Store / Secrets Manager 可能有更好的方法。
我在这个答案中描述了我是如何使用 AWS Systems Manager Parameter Store(它还为您提供了 Secrets Manager 的接口)来做到这一点的:。基本上,您授予您的 Beanstalk ECS IAM 角色访问相关参数的权限,然后在启动时从您的应用程序代码加载它。
我正在使用 Elastic Beanstalk 部署 Python Flask 应用程序。我有一个配置文件 /.ebextensions/01.config
,其中我设置了一些环境变量——其中一些应该是秘密的。
文件看起来像这样:
packages:
yum:
gcc: []
git: []
postgresql93-devel: []
option_settings:
"aws:elasticbeanstalk:application:environment":
SECRET_KEY: "sensitive"
MAIL_USERNAME: "sensitive"
MAIL_PASSWORD: "sensitive"
SQLALCHEMY_DATABASE_URI: "sensitive"
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "app/static/"
将某些值保密的最佳做法是什么?目前 .ebextensions
文件夹处于源代码管理之下,我喜欢它,因为它与所有人共享,但与此同时,我不想将敏感值置于源代码管理之下。
有没有办法在部署时通过 EB CLI 工具指定一些环境变量(例如 eb deploy -config ...
)?或者 AWS 部署工具如何涵盖此用例?
您应该能够从 eb web 控制台将敏感值指定为环境变量:您的 EB 应用程序 -> 您的 EB 环境 -> 配置 -> 软件配置 -> 环境属性
或者,您可以使用这个:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-setenv.html
编辑:虽然这是 2015 年被接受的答案,但这不应该是你处理它的方式。现在您可以为此目的使用 AWS Secrets Manager。
我一直在使用另一个 shell 脚本 ./deploy_production.sh 来设置环境特定变量。在 shell 脚本中,您可以使用 "eb setenv NAME1=VAR1 NAME2=VAR2 ..." 设置环境变量。
并且此文件不需要进入 git 存储库。
这个问题已经有了答案,但我想为这个问题贡献一个替代解决方案。我不必将秘密保存在环境变量中(然后必须在版本控制之外的某个地方进行管理和存储,而且你需要记住在部署时设置它们),我将所有秘密放在一个加密的 S3 存储桶中,只能从EB 的角色是 运行 作为。然后我在启动时获取秘密。这样做的好处是可以将部署与配置完全分离,并且您永远不必再在命令行中 fiddle 使用秘密。
如果需要(例如,如果在应用程序设置期间需要机密,例如获取代码的存储库的密钥),您还可以使用带有 S3Auth
指令的 .ebextensions
配置文件来轻松将上述 S3 存储桶的内容复制到您的本地实例;否则只需使用 AWS SDK 在启动时从应用程序中获取所有机密。
编辑:截至 2018 年 4 月,AWS 为机密管理提供专用托管服务; AWS Secrets Manager。它以字符串或 json 格式、版本控制、阶段、轮换等方式方便地安全存储秘密。它还消除了一些涉及 KMS、IAM 等的配置,以加快设置速度。我认为没有真正的理由使用任何其他 AWS 服务来存储静态敏感数据,例如私钥、密码等。
AWS documentation recommends storing sensitive information in S3因为环境变量可能以多种方式公开:
Providing connection information to your application with environment properties is a good way to keep passwords out of your code, but it's not a perfect solution. Environment properties are discoverable in the Environment Management Console, and can be viewed by any user that has permission to describe configuration settings on your environment. Depending on the platform, environment properties may also appear in instance logs.
下面的例子来自文档,您应该参考它以获得完整的细节。简而言之,您需要:
- 使用最小权限将文件上传到 S3,possibly encrypted。
授予对您的 Elastic Beanstalk 自动缩放组的实例配置文件角色的读取权限。该政策如下:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "database", "Action": [ "s3:GetObject" ], "Effect": "Allow", "Resource": [ "arn:aws:s3:::my-secret-bucket-123456789012/beanstalk-database.json" ] } ] }
在您的应用程序包根目录中将名称类似于
s3-connection-info-file.config
的文件添加到/.ebextensions
,其中包含以下内容:Resources: AWSEBAutoScalingGroup: Metadata: AWS::CloudFormation::Authentication: S3Auth: type: "s3" buckets: ["my-secret-bucket-123456789012"] roleName: "aws-elasticbeanstalk-ec2-role" files: "/tmp/beanstalk-database.json" : mode: "000644" owner: root group: root authentication: "S3Auth" source: https://s3-us-west-2.amazonaws.com/my-secret-bucket-123456789012/beanstalk-database.json
然后更新您的应用程序代码以从文件 /tmp/beanstalk-database.json
(或您决定将其放入实际配置的任何位置)中提取值。
其他一些答案提到使用 Parameter Store / Secrets Manager 可能有更好的方法。
我在这个答案中描述了我是如何使用 AWS Systems Manager Parameter Store(它还为您提供了 Secrets Manager 的接口)来做到这一点的: