aws-fargate

How to change environment variables in ECS/Fargate?


I have a Node.js app running on ECS/Fargate. I want to set some environment variables, and from what I've read, this should be done in the Task definition. However, there does not seem to be any way to edit environment variables after the task is initially defined. When I view the task, they are not editable, and there does not seem to be any way to edit the task. Is there a way to do this?


Solution

  • Container solutions are built to be immutable, which means any form of change, should force a new deployment. This leaves us with the option of retrieving the current TaskDefinition, updating its Environment variables, and updating the Service with the new definition:

    aws ecs describe-task-definition --task-definition my_task_def 
    

    This retrieves the ACTIVE Task Definition. From here you can update Environment variables and register a new Task Definition:

    aws ecs register-task-definition \
        --cli-input-json file://<path_to_json_file>/task_def.json
    

    Then Update the service

    aws ecs update-service --service my-service --task-definition my_task_def
    

    This will pick up ACTVE Task Definition.

    I used CLI for illustration but using SDKs like Boto3 might be much easier handling JSON.