An application deployed on an Amazon EC2 instance uses the AWS SDK to perform read and write operations on an Amazon DynamoDB table. An IAM instance profile named `DynamoDB-Write-Role` is attached to the EC2 instance. However, the developer has also configured a shared credentials file under `/home/ec2-user/.aws/credentials` that contains a default profile with static credentials that only allow read-only access to DynamoDB. The environment variable `AWS_PROFILE` is not set. During application execution, all write operations to DynamoDB fail with an `AccessDeniedException`. Which of the following describes the root cause of this failure and the correct resolution?
- AThe SDK attempts to use the EC2 instance profile credentials first, but defaults to the shared credentials file because the application is running under the default `ec2-user` system account. The developer must run the application under a different system account that does not have a `.aws` directory.
- BThe SDK evaluates the EC2 instance profile first, but falls back to the shared credentials file because the instance profile credentials do not have a matching session token. The developer must restart the EC2 instance to force IMDS credential regeneration.
- The SDK resolves credentials from the shared credentials file first because it precedes the EC2 instance profile in the default credential provider chain. The developer must remove the static credentials from the shared credentials file to allow the chain to fall back to the instance profile.Answer
- DThe SDK merges the permissions from the shared credentials file and the EC2 instance profile, and defaults to the most restrictive permission set. The developer must modify the IAM trust policy of the instance profile to allow the default profile to assume it.
Answer
The SDK resolves credentials from the shared credentials file first because it precedes the EC2 instance profile in the default credential provider chain. The developer must remove the static credentials from the shared credentials file to allow the chain to fall back to the instance profile.
The default credential provider chain evaluates the shared credentials file (`~/.aws/credentials`) before checking for EC2 instance profile credentials. Because a default profile exists in the shared credentials file, the chain successfully resolves credentials from that source and stops its search. It does not fall back to subsequent credentials in the chain when an authorization failure (such as `AccessDeniedException`) occurs. Removing the static credentials from the shared credentials file allows the provider chain to reach the EC2 instance profile evaluation step.
Step-by-Step Solution
Key Concept
AWS SDK Default Credential Provider Chain Order of Precedence
Estimated Time:3m 0s