如何运行AWS ECS Task覆盖环境变量

问题描述:

要通过CLI覆盖环境变量,我们可以根据《 AWS ECS命令行参考》 .

To override environment variables via CLI we may use --overrides (structure) according to AWS ECS Commandline Reference.

如何在命令行中传递名称值对(结构或JSON)?

How to pass name value pairs (structure or JSON) in command line?

[
  { "name" : "NAME", "value" : "123" },
  { "name" : "DATE", "value" : "1234-12-12" },
  { "name" : "SCRIPT", "value" : "123456" }
]

我正在寻找一种使用AWS ECS CLI覆盖上述环境变量的方法. 像这样:

I'm looking for a way to override above environment variables using AWS ECS CLI. Something like:

aws ecs run-task --overrides <<just environment vars here>> --task-definition ...

文档不清晰.我用谷歌搜索,但无济于事.

Documentation is not clear. I googled but couldn't help.

您必须提供

You have to provide a JSON document as documented under the --overrides option.

{
  "containerOverrides": [
    {
      "name": "string",
      "command": ["string", ...],
      "environment": [
        {
          "name": "string",
          "value": "string"
        }
        ...
      ]
    }
    ...
  ],
  "taskRoleArn": "string"
}

您必须指定容器的name以获得环境覆盖,并指定environment个键值对的列表.

You have to specify the name of the container to get the environment override, and specify a list of environment key-value pairs.

您可以在参数中内联指定JSON文档,或将文件路径参数传递给任务.我将展示两种方式.

You can specify the JSON document in-line with your argument or pass a file path argument to the task. I will show both ways.

您的命令应如下所示(填写值CONTAINER_NAME_FROM_TASK).

Your command would look like this (fill in the value CONTAINER_NAME_FROM_TASK).

aws ecs run-task --overrides '{ "containerOverrides": [ { "name": "CONTAINER_NAME_FROM_TASK", "environment": [ { "name": "NAME", "value": "123" }, { "name": "DATE", "value": "1234-12-12" }, { "name": "SCRIPT", "value": "123456" } ] } ] }' --task-definition (...)

这看起来确实很丑陋,并且很烦人.它也仅适用于Unix-y系统,并且在Windows中需要将引号转义.

That does look rather ugly though, and would be annoying to edit. It also only works on Unix-y systems and would require quote escaping in Windows.

因此,您也可以将文件路径传递到AWS CLI,并使其从文件中加载覆盖JSON.

So alternatively, you can pass a file path to the AWS CLI and have it load your override JSON from a file.

创建一个文件,我们将其命名为overrides.json,然后将相同的JSON放入其中:

Create a file, let's call it overrides.json, and put the same JSON into it:

{
    "containerOverrides": [{
        "name": "CONTAINER_NAME_FROM_TASK",
        "environment": [{
            "name": "NAME",
            "value": "123"
        }, {
            "name": "DATE",
            "value": "1234-12-12"
        }, {
            "name": "SCRIPT",
            "value": "123456"
        }]
    }]
}

然后,假设您的文件位于当前目录中:

Then, assuming your file is in the current directory:

aws ecs run-task --overrides file://overrides.json --task-definition (..)

如果文件在文件系统中的其他位置,并且您使用的是 Linux/Unix-y系统:

If your file is elsewhere in the filesystem and you're on a Linux/Unix-y system:

aws ecs run-task --overrides file:///path/to/overrides.json --task-definition (..)

如果文件位于文件系统中的其他位置,而您正在 Windows 中进行此操作:

If your file is elsewhere in the filesystem and you're doing this in Windows:

aws ecs run-task --overrides file://DRIVE_LETTER:\path\to\overrides.json --task-definition (..)