Question

Difficulty: EasyAWS SDKs and Credential Management

A developer is writing a Node.js application that will run on an Amazon EC2 instance. The application needs to retrieve objects from an Amazon S3 bucket using the AWS SDK. To follow security best practices, the developer wants to avoid managing long-term AWS credentials on the instance.

Which configuration should the developer use to allow the AWS SDK to access the S3 bucket securely?

  1. Attach an IAM role with S3 read permissions to the EC2 instance, and initialize the AWS SDK client without passing any credentials.Answer
  2. B
    Hardcode an IAM User's access key ID and secret access key directly in the AWS SDK client constructor within the application source code.
  3. C
    Store the IAM User's access key ID and secret access key in Systems Manager Parameter Store as plaintext, and retrieve them during client initialization.
  4. D
    Create an IAM role for the EC2 instance, but define the Amazon S3 permission statements directly within the role's trust policy.

Answer

Attach an IAM role with S3 read permissions to the EC2 instance, and initialize the AWS SDK client without passing any credentials.
Attaching an IAM role to the EC2 instance allows the AWS SDK to retrieve temporary security credentials from the EC2 Instance Metadata Service (IMDS). Because the default credential provider chain automatically queries IMDS when no other credentials are found, initializing the client without arguments is the most secure and native approach.

Step-by-Step Solution

1
Attach the IAM role to the EC2 instance.
The EC2 instance is associated with an instance profile containing the IAM role.
This allows the EC2 Instance Metadata Service (IMDS) to distribute temporary credentials to applications running on the instance.
2
Initialize the AWS SDK S3 client without passing credentials.
The SDK client is created using the default constructor.
When no credentials are explicitly provided, the default credential provider chain automatically searches for credentials, eventually falling back to the EC2 instance metadata.

Key Concept

AWS SDK Default Credential Provider Chain and IAM Roles for EC2
Estimated Time:1m 0s
Rate this question