Search code examples
amazon-web-servicesamazon-ecsaws-fargate

Do environment variables in ECS Fargate task definitions support interpolation?


Given an .env object in S3 with some environment variables:

POSTGRES_HOSTNAME=staging.cxxxxx.us-east-1.rds.amazonaws.com
POSTGRES_PASSWORD=xxxxxx
DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@${POSTGRES_HOSTNAME}:5432/postgres?schema=newjitsu

And an ECS task definition configured to use that object to specify the environment variables for the container.

When I deploy the task, I receive the following error in the container logs:

Error: P1001: Can't reach database server at ${POSTGRES_HOSTNAME}:5432

Leading me to believe that variable interpolation is not supported in ECS/Fargate.

The AWS ECS docs state:

For more information about the environment variable file syntax, see Declare default environment variables in file

With a link to the Docker Compose variable documentation that does describe variable interpolation.

So true the AWS docs do not explicitly state that interpolation is supported, but they do not explicitly state its not supported.

Can someone confirm whether or not ECS/Fargate supports environment variable interpolation?

Thank you!


Solution

  • No, it doesn't natively support variable interpolation. You would still need something that preprocesses the .env file before your application uses it. You can bet that if it was a feature they supported, they would advertise/document it.

    You could set your task definition environment variables (or secrets) to pull from SSM Parameter Store or Secrets Manager. Then those will be available in your container, but if you need them in a .env file you would have to get them there.

    Also see AWS ECS Task Definition: How do I reference an environment variable in another environment variable?