首页 > 解决方案 > AWS Elastic Beanstalk 和 Secret Manager

问题描述

有谁知道是否可以在弹性豆茎中将秘密值作为环境变量传递?显然,替代方案是在我们的代码库中使用 sdk,但我想先探索环境变量方法

干杯达米安

标签: amazon-web-servicesamazon-elastic-beanstalkaws-secrets-manager

解决方案


根据@Ali 的回答,此时它不是内置的。但是,使用.ebextensions和 AWS cli相对容易。这是一个根据 MY_ENV 环境变量将机密提取到文件的示例。然后可以将此值设置为环境变量,但请记住环境变量是特定于 shell 的。您需要将它们传递给您正在启动的任何东西。

  10-extract-htpasswd:
    env:
      MY_ENV: 
        "Fn::GetOptionSetting":
          Namespace: "aws:elasticbeanstalk:application:environment"
          OptionName: MY_ENV
    command: |
      aws secretsmanager get-secret-value --secret-id myproj/$MY_ENV/htpasswd --region=us-east-1 --query=SecretString --output text > /etc/nginx/.htpasswd
      chmod o-rwx /etc/nginx/.htpasswd
      chgrp nginx /etc/nginx/.htpasswd

这还需要向 EB 服务角色授予对密钥的 IAM 权限。即这样的政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "xxxxxxxxxx",
            "Effect": "Allow",
            "Action": "secretsmanager:GetSecretValue",
            "Resource": "arn:aws:secretsmanager:us-east-1:xxxxxxxxxxxx:secret:myproj*"
        }
    ]
}

推荐阅读