amazon-web-servicescontainersamazon-ecsecs-taskdefinition

How to provide same env with different values in ECS container?


I have to provide API_URL as the environment variable to ECS container. I created Task Definition and provide the API_URL environment variable as http://production.api.com.

It's working fine, but now we have to create staging environment and need to pass different value to API_URL.

I was following https://docs.aws.amazon.com/AmazonECS/latest/developerguide/taskdef-envfiles.html docs.

I get the idea that, we have to create different task definitions for production and staging.

Is there any other way to use same task definition and pass the API_URL value differently ?


Solution

  • If you are running an ECS service, then creating a separate task definition is the correct way to deploy your ECS service with different environment variables. If you are using an Infrastructure as Code (IaC) tool such as CloudFormation or Terraform, then this should be trivial. If you aren't using IaC, then I highly recommend you consider it.

    If you are running ECS Tasks directly via the RunTask API, not ECS Services, then there is the concept of passing Container Override settings. Even then, that sort of thing is best managed using an IaC tool instead of manual configuration.