- Newest
- Most votes
- Most comments
Adding to the previous answer, while sidecar containers (like GuardDuty or FireLens) are indeed a very common cause for early exits, I noticed a detail in your provided describe-tasks output that might point in a different direction.
The snippet shows "containers": [{ "exitCode": 1, "reason": null }]. Since there is only one object in the array, it suggests this is a single-container task. If a sidecar were failing, we would typically expect to see at least two containers listed in that output. Assuming it is just your application container, the issue might be happening within the container boundary itself. Since your application logs a wonderfully clean shutdown sequence but ECS still reports exitCode 1, I suspect it could be related to one of the following areas:
-
PID 1 and Signal Handling: If you are using a wrapper script (e.g., entrypoint.sh) to start Uvicorn, the script itself might be running as PID 1. When ECS sends a SIGTERM to stop the container, Uvicorn might shut down cleanly (as your logs show), but the wrapper script might subsequently exit with a non-zero code. It might be worth verifying if you are using exec in your entrypoint script to ensure Uvicorn runs as PID 1 and handles the signals directly.
-
ECS Container Health Checks: You confirmed the ALB health checks never reach the container. However, I wonder if there is a native Docker HEALTHCHECK defined in your Dockerfile or an ECS-level Health Check in your Task Definition. If an aggressive health check is configured there and fails immediately during startup, the ECS agent will forcefully stop the container, which often results in a clean app shutdown but a container exit code of 1.
-
Development Mode Flags: I noticed the log states [development]. If Uvicorn is being started with the --reload flag, the underlying file watcher can sometimes behave unpredictably in stateless Fargate environments, which can trigger an unexpected shutdown loop.
Regarding the secondary issue with the Nginx container: I completely agree with the previous assessment. The "unable to assume role" error during run-task is very often not an issue with the Task Execution Role's trust policy, but rather that the IAM User or CI/CD pipeline executing the command lacks the iam:PassRole permission for that specific execution role.
See also:
- https://repost.aws/knowledge-center/ecs-unable-to-assume-role
- https://repost.aws/knowledge-center/ecs-task-stopped
- https://aws.amazon.com/blogs/security/using-amazon-guardduty-ecs-runtime-monitoring-with-fargate-and-amazon-ec2/
PS: Hope this gives you a few additional angles to investigate!
Based on the symptoms you're describing—where your application logs show clean startup and shutdown but ECS reports exit code 1—this pattern is consistent with a GuardDuty Runtime Monitoring sidecar container issue rather than a problem with your application itself.
When GuardDuty Runtime Monitoring is enabled for ECS Fargate, it injects a sidecar container into your tasks. If this sidecar fails to start or exits prematurely (often due to being unable to pull its container image from ECR), it can cause your entire task to be stopped with "EssentialContainerExited" even though your application container is running perfectly. The timing you're seeing (0.3-0.5 seconds after your app starts) aligns with a sidecar initialization failure.
Key troubleshooting steps to investigate:
-
Check if GuardDuty Runtime Monitoring is enabled: Review your GuardDuty settings in the console to see if ECS runtime monitoring is active for your cluster.
-
Verify ECR permissions: Your task execution role needs specific ECR permissions to pull the GuardDuty sidecar image. Ensure it has the necessary
ecr:GetAuthorizationToken,ecr:BatchCheckLayerAvailability,ecr:GetDownloadUrlForLayer, andecr:BatchGetImagepermissions. -
Check network connectivity: The Fargate tasks must be able to reach ECR, either through public internet access or properly configured VPC endpoints. If your tasks are in private subnets without NAT Gateway or VPC endpoints for ECR, the sidecar cannot download.
-
Review security group configuration: Your security group must allow outbound access to the S3 managed prefix list on port 443, which is required for pulling container images from ECR.
-
Examine all container logs: Use
describe-tasksto check if there are multiple containers in your task definition. If you see a GuardDuty sidecar container, check its specific exit code and reason.
These three components (permissions, network connectivity, and security group configuration) are all necessary for successfully downloading the GuardDuty container image, and a failure in any one will cause the behavior you're experiencing.
Regarding the intermittent IAM role assumption issue with the nginx test: this could be a separate timing or eventual consistency issue, but it's worth verifying that your IAM user or role performing the ECS operations has iam:PassRole permission for the execution role.
Sources
Runtime coverage and troubleshooting for Amazon ECS clusters - Amazon GuardDuty
Relevant content
asked 5 years ago
asked 2 years ago
asked 3 years ago
- AWS OFFICIALUpdated 7 months ago

If my answer was helpful, I would appreciate it if you could mark it as the accepted answer.