AWS Fargate | Environment variable is not setting in fargate task

Viewed 931

I'm using ECS Fargate Platform 1.4

I'm setting environment variable while creating Task definition in a cluster but When I tried to access that environment variable in containers but environment is missing container's environment.

I tried all possible way to set and get environment. Even I tried to set env variable using command option but it failed.

Please help me out.

1 Answers

I know this is an older question, but I'll share what I've done.

I use terraform, so in my aws_ecs_task_definition container_definitions block I have environment defined like this:

environment = [
    {
      name = "MY_ENVIRONMENT_VARIABLE",
      value = "MY_VALUE"
    }
  ]

Then in my app, which is a Flask application, I do this:

from os import environ

env_value = environ.get("MY_ENVIRONMENT_VARIABLE", None) # "MY_VALUE"

In the ECS console, if you click on a task definition, you should be able to view the container definition, which should show the environment variables you set. It's just a sanity check.

Related