Sub topic 9
Injecting secrets and configuration
This chapter assumes you can read a Fargate task definition, know the difference between the task execution role and the task role, and can read an IAM policy. It builds on the web-app task definition from earlier chapters.
Two ways to pass settings
A container definition has two fields for settings:
environmentholds plain name and value pairs. They are stored in the task definition in clear text, so anyone who can describe the task definition can read them. Use this field for values such asAPP_ENV=production.secretsholds a name and avalueFromreference. ECS looks up the value and sets it as an environment variable inside the container. The task definition contains only the reference.
The earlier task definition already used this:
"secrets": [
{
"name": "DB_PASSWORD",
"valueFrom": "arn:aws:secretsmanager:us-east-1:123456789012:secret:prod/db-AbCdEf"
}
]valueFrom accepts a Secrets Manager secret ARN or a Systems Manager Parameter Store parameter ARN. Parameter Store works well for ordinary configuration and SecureString values. Secrets Manager adds rotation and cross-account sharing.
Who fetches the secret
The agent that starts your task fetches the secret, not your application code. It acts as the task execution role. That role therefore needs read access to the secret. The task role is not involved.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "secretsmanager:GetSecretValue",
"Resource": "arn:aws:secretsmanager:us-east-1:123456789012:secret:prod/db-AbCdEf"
}
]
}Attach this policy to ecsTaskExecutionRole in addition to AmazonECSTaskExecutionRolePolicy. For Parameter Store, grant ssm:GetParameters on the parameter ARN. If the secret is encrypted with a customer managed KMS key, also grant kms:Decrypt on that key.
Pulling one key from a JSON secret
Many secrets are JSON objects. Append the key name to the ARN, using the form secret-arn:json-key:version-stage:version-id. Leave the last two parts empty to get the current version:
{
"name": "DB_USER",
"valueFrom": "arn:aws:secretsmanager:us-east-1:123456789012:secret:prod/db-AbCdEf:username::"
}Behaviors that cause surprises
- Values are read once. ECS resolves secrets when the task starts. If you rotate a secret, running tasks keep the old value. Start new tasks, for example with
aws ecs update-service --force-new-deployment. - A failed lookup stops the task. The task never starts, and the stopped reason shows a
ResourceInitializationError. Check the execution role's permissions first. - The task needs a network path. The agent must reach Secrets Manager or SSM. A task in a private subnet needs a NAT gateway or a VPC endpoint for the service.
- Secrets are visible inside the container. Anyone with
ecs execute-commandaccess can read the environment variables. Never log them.
Exercise
A new task fails to start with a ResourceInitializationError about retrieving a secret. The task role already allows secretsmanager:GetSecretValue. What is the most likely fix?
Show answer
ECS fetches the secret with the task execution role, not the task role. Add secretsmanager:GetSecretValue (and kms:Decrypt for a customer managed key) to the execution role. If permissions are correct, check that the task's subnet can reach Secrets Manager through a NAT gateway or a VPC endpoint.