Sub topic 10
Injecting secrets and configuration
This chapter covers how a Fargate task receives configuration and secrets at start time. It assumes you can read a task definition, know the basics of IAM roles, and have used Secrets Manager or Systems Manager Parameter Store. The earlier chapters on task definitions and on the two IAM roles are the foundation here.
Configuration versus secrets
A container image should be the same in every environment. What changes between development and production is configuration: a database host, a feature flag, a log level. ECS supplies this in the container definition in three ways.
| Mechanism | Where the value lives | Visible in the task definition? | Typical use |
|---|---|---|---|
| `environment` | Inline in the task definition | Yes, as plain text | Non-sensitive settings such as `APP_ENV` |
| `secrets` | Secrets Manager or SSM Parameter Store | Only the reference (ARN) | Passwords, API keys, tokens |
| `environmentFiles` | A `.env` file in S3 | No, only the S3 ARN | Many non-sensitive variables kept outside the definition |
Anything that would cause harm if it appeared in a screenshot, a pull request, or describe-task-definition output belongs in secrets, not environment.
How the `secrets` field works
In the secrets array, each entry has a name (the environment variable the container will see) and a valueFrom (where to fetch the value). This is the relevant part of the task definition from earlier chapters:
"secrets": [
{
"name": "DB_PASSWORD",
"valueFrom": "arn:aws:secretsmanager:us-east-1:123456789012:secret:prod/db-AbCdEf"
}
]The value is not fetched by your application. The ECS agent on the Fargate task resolves it before your container process starts and sets it as an environment variable. Your code simply reads DB_PASSWORD.
The sequence matters for permissions, because it explains which IAM role is involved.
Which role needs the permission
The lookup happens during task startup, before your application code runs, so it uses the task execution role, not the task role. This is the most common mistake in this area. If the application itself also calls Secrets Manager through an SDK at runtime, then the task role needs the permission as well. That is a separate access path.
The managed policy AmazonECSTaskExecutionRolePolicy covers image pulls and log delivery, but it does not grant access to your secrets. Add an inline policy to the execution role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["secretsmanager:GetSecretValue"],
"Resource": "arn:aws:secretsmanager:us-east-1:123456789012:secret:prod/db-AbCdEf"
}
]
}Scope Resource to the specific secret ARNs the task uses. A wildcard on all secrets lets any task using that execution role read every secret in the account.
Two further permissions may be needed:
ssm:GetParametersfor Parameter Store references.kms:Decryptif the secret is encrypted with a customer managed KMS key. The defaultaws/secretsmanagerkey needs no extra grant for same-account use, but a customer managed key does.
Parameter Store references
For Parameter Store, valueFrom is the parameter ARN. If the parameter is in the same Region as the task, you can use just its name.
"secrets": [
{
"name": "API_TOKEN",
"valueFrom": "arn:aws:ssm:us-east-1:123456789012:parameter/prod/api-token"
},
{
"name": "LOG_LEVEL",
"valueFrom": "/prod/log-level"
}
]Parameter Store suits plain configuration that you still want to manage centrally, and SecureString parameters for lower-cost secrets. Secrets Manager adds built-in rotation for supported databases and cross-account sharing, at a per-secret monthly charge.
Pulling a single key from a JSON secret
Secrets Manager commonly stores JSON such as {"username":"app","password":"s3cret"}. You can inject one key by appending segments to the ARN, separated by colons: secret-arn:json-key:version-stage:version-id. Leave the unused segments empty.
"secrets": [
{
"name": "DB_USER",
"valueFrom": "arn:aws:secretsmanager:us-east-1:123456789012:secret:prod/db-AbCdEf:username::"
},
{
"name": "DB_PASSWORD",
"valueFrom": "arn:aws:secretsmanager:us-east-1:123456789012:secret:prod/db-AbCdEf:password::"
}
]If you omit the key, the container receives the whole JSON string and your code must parse it. Fargate platform version 1.4.0 or later is required for this feature, and that is the default for current tasks.
Environment files from S3
When a container needs many variables, environmentFiles loads them from a file in S3. The file contains lines of KEY=value, and the execution role needs s3:GetObject on the object and s3:GetBucketLocation on the bucket.
"environmentFiles": [
{
"value": "arn:aws:s3:::my-config-bucket/web-app/production.env",
"type": "s3"
}
]This file is not a secret store. Its content is not encrypted by ECS beyond what S3 provides, so keep passwords out of it.
Rotation, updates, and other edge cases
- Values are read once per task start. If you rotate a secret, running tasks keep the old value. Start new tasks to pick up the change, for example with
aws ecs update-service --cluster demo --service web --force-new-deployment. - Version pinning. Without a version stage, ECS reads
AWSCURRENT. Pin a stage or version ID only when you need a controlled cutover. - Startup failures. If the execution role cannot read a secret, the task stops before the container starts. The service events and the task's stopped reason show an error such as
ResourceInitializationError: unable to pull secrets or registry auth. Network access matters too. A task in a private subnet needs a NAT gateway or VPC endpoints for Secrets Manager (com.amazonaws.<region>.secretsmanager) or SSM, and for KMS if used. - Replacing a secret. If you delete and recreate a secret, its ARN suffix (
-AbCdEf) changes. A task definition holding the old ARN will fail. Referencing by partial ARN without the suffix is possible but ties you to name-based matching, so keep the full ARN unless you have a reason not to. - Exposure inside the container. Environment variables can appear in crash dumps, debug endpoints, and child processes. If that is a concern, have the application read the secret at runtime through the SDK using the task role, and keep it out of the environment.
- Task definition revisions. A change to
environmentorsecretsrequires a new task definition revision and a new deployment. The older revisions remain, so remove stale values fromenvironmentrather than assuming the previous revision is gone.
Choosing an approach
- Non-sensitive and few: use
environment. - Non-sensitive and many, or shared between services: use
environmentFilesor Parameter Store. - Sensitive: use
secretswith Secrets Manager or aSecureStringparameter. - Sensitive and you want to avoid environment variables: fetch at runtime with the task role.
Exercise
A task's container definition lists a Secrets Manager secret under secrets. The task stops at startup with "unable to pull secrets or registry auth". The application's task role already has secretsmanager:GetSecretValue on that secret. What should you check first, and why was the task role not enough?
Show answer
Check the task execution role, not the task role. ECS resolves secrets entries during task startup using the execution role, so that role needs secretsmanager:GetSecretValue on the secret (plus kms:Decrypt if a customer managed key is used). Also verify the task in a private subnet can reach Secrets Manager through a NAT gateway or a VPC endpoint.
Exercise
You rotate a database password in Secrets Manager, but the running service still connects with the old password. What action makes the tasks use the new value?
Show answer
Start new tasks, because ECS reads secrets only when a task starts. Running aws ecs update-service --cluster demo --service web --force-new-deployment replaces the tasks, and the new ones receive the current secret value.