Containers
4,222 failed tasks on a first deploy to a new region
A service crash-looped on its first deploy to Sydney. Memory and network were fine. The real cause was a 403 during startup, because two services disagreed about which tenant they belonged to.
- Stack
- Amazon ECS, Application Load Balancer health checks, NestJS, AWS Secrets Manager
- My role
- Root cause and the fix
- Outcome
- The service was stable right after the fix. A second service with the same problem was fixed in minutes.
What people saw
Tasks were exiting with code 137 after failing load balancer health checks, thousands of times over. Exit 137 means the process was killed with SIGKILL. That often points to the out-of-memory killer, but memory was at 8%. Here, ECS was stopping tasks the load balancer had marked unhealthy: it sends SIGTERM, waits for the stop timeout, then sends SIGKILL to anything still running.
Why the port never opened
The container logs showed the app never finished starting. During startup, the service fetched tenant configuration from the platform’s internal API, and that call returned 403 “Incorrect API key”. Startup waited on that call and never got past it, so the app never opened its port. The health check failed, ECS replaced the task, and the cycle repeated.
Two services, two answers to the same question
Both services choose their internal API key the same way. First they work out an org ID: from an environment variable if it’s set, otherwise from a map keyed by environment prefix. Then they fetch that org’s current key from Secrets Manager.
- The platform API had the region’s org ID set, so it fetched and rotated the real regional key.
- The new service had neither the environment variable nor a map entry for the new region’s prefix. It fell back to “default”, never fetched the regional key, and sent an outdated fallback value.
Other regions only worked because both services’ maps happened to include their prefix.
The fix
One environment variable on the task definition, so the service resolved the correct org. It recovered immediately. The next day, a second service hit the same problem. It used a different variable name for the same input, and because the pattern was already written down, it was fixed in minutes.
Follow-ups I proposed
- Put the variable in each service’s config source, so a CI rebuild can’t drop it.
- Add the new region’s prefix to the code map on both sides.
- Make the startup config fetch retry with backoff and log each failure loudly, so a blocked start is obvious in seconds instead of looking like a memory problem.
- Set a health-check grace period. It was zero.
What I’d do differently
- Turn on the ECS deployment circuit breaker, with rollback, for every service. It stops a rollout after repeated task failures and returns to the last working version. A broken first deploy would have stopped after a handful of tasks, not 4,222.
- Make sure the app exits on SIGTERM. Node running as PID 1 ignores it by default. Setting
initProcessEnabledin the task definition puts a small init process in front, so tasks stop cleanly instead of waiting for SIGKILL.
What I took from it
- Exit 137 is a symptom. Read the task’s stop reason before assuming it ran out of memory.
- A hard dependency during startup turns a config mistake into a crash loop.
- “It works in every other region” can be luck. Check whether the other regions have the same config, or just happen to work.