33

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

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" }
]

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.

wkl
  • 77,184
  • 16
  • 165
  • 176
sithumc
  • 3,254
  • 8
  • 27
  • 46

2 Answers2

61

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"
}

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

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.

Passing JSON in-line

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 (...)

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.

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

Passing a file path argument

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 (..)

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 (..)

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 (..)
wkl
  • 77,184
  • 16
  • 165
  • 176
  • Does terraform document this behavior anywhere? I can only find file() function which is not the same as you describe: https://www.terraform.io/docs/configuration/functions/file.html – Zhenya May 13 '19 at 16:10
  • 2
    @Ievgen No, because this is not a terraform behavior, it's the AWS CLI. If you want the Terraform equivalent, you specify something as part of [`container_definitions`](https://www.terraform.io/docs/providers/aws/r/ecs_task_definition.html#container_definitions) when creating your ECS Task Definition. – wkl May 13 '19 at 19:02
  • i dont see a way to override image name. Is that correct? – РАВИ Jul 01 '20 at 19:02
  • @РАВИ you can not override the image via `containerOverrides` - `image` is a property of the container itself. Think of `containerOverrides` more as "apply these resource/environment changes to an already configured contained." – wkl Jul 02 '20 at 12:46
  • Are overrides options available in ECS console? – Sych Feb 28 '23 at 10:49
1

A small addition to 逆さま's answer. You might also want to add the details of the cluster. When I ran the above I got a ClusterNotFoundException.

This worked for me:

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • 1
    Try commenting on answers when you wish to add to them rather than write another answer. Your comment will actually follow the answer and be seen more appropriately :). – Sebastien De Varennes Oct 13 '21 at 14:49