A backend application running on Amazon EC2 instances must access an Amazon DynamoDB table using the AWS SDK. The developer needs to configure the application to retrieve temporary credentials automatically without using any long-lived credentials. Which configuration should the developer implement?
- AStore the access key and secret access key of an IAM user in AWS Secrets Manager, and configure the application to retrieve them at startup to initialize the SDK client.
- BQuery the Instance Metadata Service (IMDS) endpoint directly from the application code to retrieve temporary credentials, and pass them explicitly to the SDK client constructor.
- Attach an IAM role with DynamoDB permissions to the EC2 instance profile, and construct the SDK client using the default constructor without passing explicit credentials.Answer
- DCreate an IAM execution role, assign its ARN to the AWS_ROLE_ARN environment variable, and write application code that uses the AWS Security Token Service (STS) to assume the role at startup.
Answer
Attaching an IAM role with DynamoDB permissions to the EC2 instance profile, and constructing the SDK client using the default constructor without passing explicit credentials.
Attaching an IAM role to the EC2 instance profile is the recommended best practice. The AWS SDK's default credential provider chain automatically looks for credentials provided by the EC2 Instance Metadata Service (IMDS) when no credentials are explicitly configured in the code. This completely removes the need for long-lived credentials and manages the rotation of temporary credentials automatically.
Step-by-Step Solution
Key Concept
AWS SDK Default Credential Provider Chain and EC2 Instance Profiles