How to run AWS ECS Task overriding environment variables
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When running an AWS ECS task, you can override environment variables defined in the task definition by passing overrides to the run-task API call. This lets you use a single task definition for multiple purposes — different environments, one-off jobs, or configuration changes — without creating a new task definition revision each time. The override applies only to that specific task run and does not modify the task definition itself. Overrides are supported in both Fargate and EC2 launch types, and can be passed via the AWS CLI, SDK, or Console.
Task Definition with Default Environment Variables
These environment variables are the defaults for every task launched from this definition. Overrides replace specific variables while keeping the rest unchanged.
Overriding via AWS CLI
The containerOverrides.environment list replaces specified variables and adds new ones. Variables not listed in the override retain their task definition defaults. In this example, DATABASE_URL keeps its production value while NODE_ENV and LOG_LEVEL are overridden.
Overriding via AWS SDK (Node.js)
Overriding via AWS SDK (Python / Boto3)
Overriding CPU, Memory, and Command
Overriding command runs a different entry point — useful for one-off tasks like database migrations, data exports, or health checks using the same container image.
Using with Scheduled Tasks (EventBridge)
EventBridge (CloudWatch Events) scheduled rules can pass overrides via the Input field, enabling scheduled tasks with different configurations from the same task definition.
Common Pitfalls
- Wrong container name in overrides: The
nameincontainerOverridesmust exactly match the container name in the task definition. A mismatch silently ignores the override — no error, no warning, just the default values used instead. - Expecting overrides to modify the task definition: Overrides apply only to the specific task run. The task definition remains unchanged. If you need permanent changes, create a new task definition revision with
register-task-definition. - Environment override replaces entire variable, not appends: If your task definition has
DATABASE_URL=prod-urland you override withDATABASE_URL=staging-url, the production URL is completely replaced. There is no merge behavior for individual variables. - Using secrets in environment overrides instead of AWS Secrets Manager: Environment overrides appear in plain text in CloudTrail logs, task metadata, and the ECS console. For sensitive values (passwords, API keys), use
secretswith AWS Secrets Manager or SSM Parameter Store references instead ofenvironment. - Fargate CPU/memory override constraints: Fargate only supports specific CPU/memory combinations (e.g., 256 CPU with 512/1024/2048 memory). Overriding with an unsupported combination causes the task to fail with an
InvalidParameterException. Check the Fargate task size documentation for valid pairs.
Summary
- Pass
overrides.containerOverrides[].environmenttorun-taskto override environment variables for a single task run - The container
namein overrides must match the task definition exactly - Overrides do not modify the task definition — they are per-run only
- You can also override
command,cpu, andmemoryin the same overrides object - Use overrides for one-off tasks (migrations, reports) without creating new task definition revisions
- Store sensitive values in AWS Secrets Manager, not in environment overrides

