All posts

· Max ShipIt

Secrets in ECS: Secrets Manager vs Parameter Store

How to pass secrets to AWS ECS tasks with the secrets field, how Secrets Manager and SSM Parameter Store compare on cost and features, and how rotation reaches running tasks.

Putting a database password into a task definition’s environment block works, but anyone who can call DescribeTaskDefinition can read it. ECS has a better way built in: the secrets field, backed by either AWS Secrets Manager or SSM Parameter Store.

How the secrets field works

Instead of a value, you give ECS a reference:

"secrets": [
  {
    "name": "DATABASE_PASSWORD",
    "valueFrom": "arn:aws:secretsmanager:eu-central-1:123456789012:secret:prod/api/db-AbCdEf"
  },
  {
    "name": "STRIPE_KEY",
    "valueFrom": "arn:aws:ssm:eu-central-1:123456789012:parameter/prod/api/stripe-key"
  }
]

When a task starts, ECS fetches each value using the task execution role and sets it as an environment variable in the container. The task definition only holds the ARN, so the secret never appears in the API, the console or your infrastructure code.

A container can mix both stores. In Bayplan’s task definition view, the secrets of a container show as a table of variable names and the ARNs they come from:

Bayplan's task definition view showing a container's secrets: DATABASE_URL from a Secrets Manager secret with the JSON key url, and API_TOKEN from an SSM parameter.
One container, two stores: DATABASE_URL reads the url key of a Secrets Manager secret, API_TOKEN an SSM parameter.

The execution role needs secretsmanager:GetSecretValue or ssm:GetParameters on those resources, plus kms:Decrypt if they’re encrypted with a customer-managed key. See task role vs execution role for the details, or generate the policy with the ECS IAM policy generator.

Picking one key from a JSON secret

Secrets Manager secrets are often JSON objects with several fields. You can pull out a single key by appending it to the ARN:

arn:aws:secretsmanager:eu-central-1:123456789012:secret:prod/api/db-AbCdEf:password::

The format is secret-arn:json-key:version-stage:version-id, and empty fields mean the current version. On Fargate this needs platform version 1.4.0 or later.

Worked example: one secret of each kind

Create a JSON secret in Secrets Manager and a SecureString parameter:

aws secretsmanager create-secret --name prod/api/db \
  --secret-string '{"username":"api","password":"s3cr3t-example"}'
{
  "ARN": "arn:aws:secretsmanager:eu-central-1:123456789012:secret:prod/api/db-AbCdEf",
  "Name": "prod/api/db",
  "VersionId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"
}
aws ssm put-parameter --name /prod/api/stripe-key --type SecureString --value sk_live_example
{ "Version": 1, "Tier": "Standard" }

Note the -AbCdEf at the end of the secret’s ARN. Secrets Manager appends six random characters to every secret ARN, so an IAM policy for secret:prod/api/db matches nothing. AWS recommends secret:prod/api/db-??????, which matches exactly six characters. db-* would also match a secret named prod/api/db-old. For every secret of a service, secret:prod/api/* is fine. Parameters have no suffix, but the leading / of the name disappears in the ARN: parameter/prod/api/stripe-key.

Then reference both in the container definition:

"secrets": [
  { "name": "DATABASE_PASSWORD", "valueFrom": "arn:aws:secretsmanager:eu-central-1:123456789012:secret:prod/api/db-AbCdEf:password::" },
  { "name": "STRIPE_KEY", "valueFrom": "arn:aws:ssm:eu-central-1:123456789012:parameter/prod/api/stripe-key" }
]

and give the execution role access to exactly those two:

{
  "Effect": "Allow",
  "Action": ["secretsmanager:GetSecretValue", "ssm:GetParameters"],
  "Resource": [
    "arn:aws:secretsmanager:eu-central-1:123456789012:secret:prod/api/db-??????",
    "arn:aws:ssm:eu-central-1:123456789012:parameter/prod/api/stripe-key"
  ]
}

Both use the AWS managed key here (aws/secretsmanager and aws/ssm), so no kms:Decrypt is needed.

Why won’t my task start? Four failure cases

When ECS can’t fetch a secret, the task stops before your container runs, with a ResourceInitializationError as the stopped reason. Get it with:

aws ecs describe-tasks --cluster production --tasks <task-id> \
  --query 'tasks[0].stoppedReason' --output text

The rest of the message tells the cases apart:

The stopped reason mentionsCauseFix
not authorized to perform: secretsmanager:GetSecretValue (or ssm:GetParameters)The execution role’s policy doesn’t cover the secret, often because the resource ARN misses the suffixFix the resource ARN in the execution role’s policy
An access denied error for the KMS keyThe secret uses a customer-managed KMS keyAdd kms:Decrypt on that key to the execution role, and allow the role in the key policy
The secret’s ARN and that it can’t be retrievedThe secret, parameter or JSON key doesn’t exist, or is in another RegionCheck the name, the key (it’s case-sensitive) and the Region in the ARN
dial tcp, i/o timeout or context deadline exceededThe task is in a private subnet with no route to Secrets Manager or SSMAdd a NAT gateway or VPC endpoints for secretsmanager or ssm

The last one is common on Fargate tasks in private subnets that already have an ECR endpoint: ECS needs a route to every service the task definition references, not only the registry.

AWS lists more variants on its ResourceInitializationError troubleshooting page.

Secrets Manager or Parameter Store: which should you use?

Secrets ManagerParameter Store
Price$0.40 per secret per month, plus $0.05 per 10,000 API callsStandard parameters free; advanced parameters $0.05 per month
Size limit64 KB4 KB standard, 8 KB advanced
Built-in rotationYes, with Lambda rotation functions, including ready-made ones for RDSNo
Cross-account accessYes, with a resource policyNot for standard parameters
EncryptionAlways, with KMSSecureString parameters use KMS
VersioningVersion stages such as AWSCURRENT and AWSPREVIOUSNumbered versions and labels

Prices are for us-east-1 as of November 2026, from the Secrets Manager and Systems Manager pricing pages. Size limits are in the Secrets Manager quotas and the parameter tiers docs.

The rule of thumb many teams use: Secrets Manager for credentials that rotate (database passwords, API keys you can rotate), Parameter Store SecureString for everything else (configuration, static tokens, feature flags). Twenty secrets in Secrets Manager cost about $8 a month. The same values in standard parameters cost nothing.

API calls barely matter for ECS, which reads the values when a task starts: a service with 10 tasks, 20 secrets and 50 deployments a month makes about 10,000 GetSecretValue calls, or $0.05. Standard parameters have no charge per call at the default throughput.

What happens to running tasks when a secret rotates?

This is the part that catches people out. ECS reads secrets once, when the task starts. When a secret rotates, running tasks keep the old value until they are replaced.

Your options:

  1. Redeploy after rotation. An EventBridge rule on the rotation event invokes a small Lambda function that calls UpdateService with forceNewDeployment. EventBridge can’t update a service by itself. Tasks restart with the new value.
  2. Overlap old and new credentials. Secrets Manager’s alternating-users rotation switches between two database users, so the previous credentials keep working until the next rotation and tasks can be replaced gradually.
  3. Read the secret in your application with the AWS SDK and the task role, and refresh it from time to time. This gives you live rotation, at the cost of more code.

For most services, option 1 combined with option 2 is the least work.

To check whether running tasks still use an old value, compare when the secret last rotated with when the tasks started:

aws secretsmanager describe-secret --secret-id prod/api/db --query LastRotatedDate
aws ecs list-tasks --cluster production --service-name api --query taskArns --output text \
  | xargs aws ecs describe-tasks --cluster production --query 'tasks[].[taskArn,startedAt]' --output text --tasks

Any task that started before LastRotatedDate has the previous value. A forced deployment replaces them:

aws ecs update-service --cluster production --service api --force-new-deployment

Things to avoid

  • Secrets in environment. They’re visible to anyone with read access to ECS.
  • Secrets baked into images. Anyone who can pull the image can read them, forever.
  • One wildcard policy for all secrets. Scope the execution role to a path prefix per service, such as prod/api/*.
  • Logging environment variables at startup. Some frameworks do this by default, which puts your secrets into CloudWatch Logs.

Secrets for log routers

The same mechanism works for log configuration. secretOptions in a container’s logConfiguration lets you pass an API key to a log driver or FireLens destination without putting it in the task definition.

With the secrets field, a leaked task definition reveals only ARNs, never the values behind them.