A developer is deploying a containerized application to Amazon ECS on AWS Fargate. The application code is designed to use the AWS SDK to retrieve database credentials from AWS Secrets Manager at startup.
The ECS task definition is configured with the following parameters:
- taskRoleArn set to ecs-app-task-role
- executionRoleArn set to ecs-app-execution-role
The developer attached an IAM policy allowing secretsmanager:GetSecretValue to the ecs-app-execution-role. However, when the container starts, the application throws an AccessDeniedException when executing the GetSecretValue SDK call.
What should the developer do to resolve this authorization failure?
- AUpdate the trust policy of the ecs-app-execution-role to allow the Secrets Manager service principal (secretsmanager.amazonaws.com) to assume the role.
- Attach the IAM policy allowing secretsmanager:GetSecretValue to the ecs-app-task-role.Cevap
- CConfigure the container application to retrieve credentials directly from the ECS Task Execution Role metadata endpoint.
- DHardcode the AWS access key and secret key of a dedicated IAM user with Secrets Manager access in the application's SDK client initialization.
Cevap
Attach the IAM policy allowing secretsmanager:GetSecretValue to the ecs-app-task-role.
The correct action is to attach the permission policy allowing secretsmanager:GetSecretValue to the ECS Task Role (ecs-app-task-role). When an application runs inside an ECS container and makes calls to AWS services using the AWS SDK, the SDK retrieves credentials from the task's credential provider, which are associated with the ECS Task Role. The ECS Task Execution Role is only used by the ECS container agent to perform lifecycle tasks on behalf of the container, such as pulling container images from Amazon ECR or writing logs to Amazon CloudWatch.
Adım Adım Çözüm
Anahtar Kavram
Distinction between ECS Task Role and ECS Task Execution Role for resolving runtime SDK authorization failures.