aws fargate 为环境变量添加参数

aws fargate adding a parameter for environment variables

我正在尝试自动化 Fargate 实例的 Cloudformation 部署。如果我硬环境变量条目,我可以成功部署 cloudformation,但如果我尝试添加作为参数,键入字符串,它会抱怨它不是字符串。

这里是参数

"EnvVariables":{ "Description": "All environment Variables for Docker to run", "Type": "String" },

在我的任务定义中,我对容器定义进行了以下设置

     "Environment": [
      {
        "Name": "JAVA_OPTS",
        "Value": "-Djdbc.url=jdbc:dbdriver://xxxx.eu-west-1.rds.amazonaws.com:xxxx/xxxxxxxxx -Djdbc.user=xxxxx -Djdbc.password=xxxxx" 
      }
    ]

如果我通过 gui 在参数字段中输入以下内容

"-Djdbc.url=jdbc:dbdriver://xxxx.eu-west-1.rds.amazonaws.com:xxxx/xxxxxxxxx -Djdbc.user=xxxxx -Djdbc.password=xxxxx"

它抱怨它不是字符串。

我如何编辑它才能被接受为参数?

我认为您不能注入动态环境变量。即使可能,也请避免以明文形式输入密码。

我所做的是将值存储在安全的 SSM 参数中。然后,java 代码可以获取值并相应地进行初始化。

我通过使用 Jinja2 Python 模板脚本解决了这个问题,并使用

插入了环境变量
"Environment": [
    {% for environment in td.envVariables %}
    {
      "Name": "{{environment.name}}",
      "Value": "{{environment.value}}"
    },
    {% endfor %}

这样就可以应用一组环境变量。该脚本不能作为 cloudformation 脚本直接应用,而是需要包含在另一个 shell/python 脚本中。

使用任务定义(门户或 JSON),您可以在 "containerDefinitions" 部分中定义 "secrets",该部分将从机密管理器中检索。

Note: At the time of writing, Fargate only supports secrets that are a single value, not the JSON or key value secrets. So choose OTHER when creating the secret and just put a single text value there.

{ 
    "ipcMode": null,
    "executionRoleArn": "arn:aws:iam::##:role/roleName",
    "containerDefinitions": [
      {
         ...
        "secrets": [{
          "name": "SomeEnvVariable",
          "valueFrom": "arn:aws:secretsmanager:region:###:secret:service/secretname"
        }],
        ...
     }
    ],
    "requiresCompatibilities": [
      "FARGATE"
    ],
    "networkMode": "awsvpc",
    ...
}

注意:任务中定义的执行角色需要附加策略,例如SecretsManagerReadWrite

More info in docs