Why an ECS task stops before its container starts with a Secrets Manager error, how to tell a missing permission from a missing secret or a missing network route, and the execution role, KMS and endpoint settings that fix each one.

The task starts, ECS pulls the image, and then the task stops before your container runs, with a stopped reason that begins ResourceInitializationError: unable to pull secrets or registry auth. The rest of the line names the failing call, and that tail is what tells you which of four things is wrong.
The error means the ECS agent could not fetch a secret referenced in the task definition, so none of your code ran and there are no application logs to read.
Run aws ecs describe-tasks on a recently stopped task and read stoppedReason, as described in the stopped task documentation. The ending decides the fix.
TASK=$(aws ecs list-tasks --cluster prod --service-name api --desired-status STOPPED --query 'taskArns[0]' --output text)
aws ecs describe-tasks --cluster prod --tasks $TASK \
--query 'tasks[0].[stopCode,stoppedReason]' --output text
Per the ECS task execution role documentation, referencing Secrets Manager secrets in a task definition needs secretsmanager:GetSecretValue on the execution role, and kms:Decrypt as well when the secret is encrypted with a customer managed key. It is the execution role and not the task role, because the agent fetches secrets before your container exists, which is why adding the permission to the role your code uses changes nothing.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["secretsmanager:GetSecretValue"],
"Resource": ["arn:aws:secretsmanager:eu-west-1:111122223333:secret:prod/api/stripe-key-AbCdEf"]
}
]
}
Scope the resource to the exact secret ARN, including the six random characters Secrets Manager appends to the name, or use a prod/api/* pattern. A wildcard on all secrets lets one service's task read the others.
Check that the ARN in the task definition's secrets block names a secret in the same account and region as the cluster, since a copy-pasted ARN from another environment is the usual cause. For JSON secrets, the reference format is arn:aws:secretsmanager:region:account:secret:name:json-key:version-stage:version-id, and per the ECS documentation on Secrets Manager secrets the key and version parts need Fargate platform version 1.4.0 or later. A missing :: separator between the pieces reads as a different secret name and produces the same not-found error.
A Fargate task in a private subnet has no route to Secrets Manager unless the subnet has a NAT gateway or the VPC has an interface endpoint for com.amazonaws.REGION.secretsmanager. The endpoint's security group must allow inbound HTTPS from the task's security group. Tasks that also pull images from ECR in a private subnet need the ECR and S3 endpoints for the same reason, so a timeout on the secret often comes with a pull error on the next deployment.
After changing the role or the network, no new task definition is needed. Force a new deployment, since a task that stopped on an old failure does not retry on its own with new permissions.
Encore brings infrastructure from code, and each secret is declared in the service that reads it. Per the AWS infrastructure docs, the platform stores secrets in AWS Secrets Manager in your account, injects them into the service as secret environment variables, provisions an IAM role for the service, and builds the VPC with a NAT gateway for outbound access from the compute subnets.

Run in your terminal to get started locally.
An API that authenticates partners with keys held in one secret:
// partners/partners.ts
import { api, APIError, Gateway, Header } from "encore.dev/api";
import { authHandler } from "encore.dev/auth";
import { getAuthData } from "~encore/auth";
import { secret } from "encore.dev/config";
import { timingSafeEqual } from "node:crypto";
// Comma-separated list of partner keys, for example "acme:key1,globex:key2".
const partnerKeys = secret("PartnerApiKeys");
interface AuthParams {
key: Header<"X-Api-Key">;
}
interface AuthData {
userID: string;
partner: string;
}
const auth = authHandler<AuthParams, AuthData>(async (params) => {
const supplied = Buffer.from(params.key ?? "");
for (const entry of partnerKeys().split(",")) {
const [partner, key] = entry.split(":");
const expected = Buffer.from(key ?? "");
if (expected.length === supplied.length && timingSafeEqual(expected, supplied)) {
return { userID: partner, partner };
}
}
throw APIError.unauthenticated("invalid API key");
});
export const gateway = new Gateway({ authHandler: auth });
export const whoami = api(
{ expose: true, auth: true, method: "GET", path: "/partners/whoami" },
async (): Promise<{ partner: string }> => ({ partner: getAuthData()!.partner }),
);
Set the value with encore secret set --type prod PartnerApiKeys, as described in the secrets docs, and the auth docs cover the auth handler used above. A deploy fails when a declared secret has no value, so the task never starts without it, and secret values are not stored in the repo, so an AI coding agent working in it can read the secret name but not the keys. Locally, encore run starts the service and warns about a missing secret, and .secrets.local.cue supplies a test value for one machine.
To deploy, connect your AWS account as described in Connect your cloud account and push with git push encore.
A coding agent that adds a new integration key declares it in the same change as the code that uses it, and no secret ARN or execution role statement is part of the diff.
Best fit: Teams on ECS that keep losing deploys to secret permission and ARN mistakes and want secrets declared next to the code, with the AWS side created on deploy.
Read the end of stoppedReason and pick the row in the table. If it says access denied, look at the execution role and not the task role, and if it times out, look at the route from the subnet before touching IAM.
ECS could not fetch a secret from Secrets Manager or Parameter Store before starting the container, so the task stopped without running any of your code. The stopped reason ends with the underlying error, such as AccessDeniedException, ResourceNotFoundException or a timeout.
The task execution role, not the task role. The agent that starts the container uses the execution role to fetch secrets, so it needs secretsmanager:GetSecretValue on the secret, plus kms:Decrypt if the secret uses a customer managed key.
The task could not reach Secrets Manager over the network. A Fargate task in a private subnet needs a route to the service, either a NAT gateway or an interface VPC endpoint for Secrets Manager, and a security group that allows the traffic.
With Encore you declare secret() in code and set values with encore secret set, and on deploy Encore stores them in AWS Secrets Manager in your account and provisions an IAM role for the service. A deploy fails before any task starts if a declared secret has no value.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.