'how to provide environment variables to AWS ECS task definition?

In the task definition on ECS, I have provided environment variable as following:

Key as HOST_NAME and the value as something.cloud.com

On my local I use this docker run command and I'm able to pass in my env variables, but through task definition the variables are not being passed to container.

The docker run command below works on local, but how do I set it up in the task definition in AWS ECS?

docker run -e HOST_NAME=something.cloud.com sid:latest



Solution 1:[1]

You should call it name and not key, see example below

 {
  "name": "nginx",
  "image": "",
  "portMappings": [
    {
      "containerPort": 80,
      "hostPort": 80
    }
  ],
  "environment": [
    {
      "name": "HOST_NAME",
      "value": "something.cloud.com"
    }
    ]
 }

Solution 2:[2]

If you used the new docker compose integration with ECS, then you will need to update the stack.

It is smart enough to update only the parts that changed. For my case, this was the task definition not picking new environment variables set on a .env file and mounted on the docker container

Run the same command you used to create the stack, only that this time round it'll update it(only parts that changed)

docker compose --context you-ecs-context up -f your.docker-compose.yml

For more: https://docs.docker.com/engine/context/ecs-integration/#rolling-update

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris
Solution 2 Kaka Ruto