EC2 Access to S3 Using IAM Role — Least Privilege
A DevOps engineer needs to ensure that EC2 instances can access an S3 bucket without storing AWS credentials on the instances. Which solution meets this requirement?
Quick Answer
The correct answer is to attach an IAM role to the EC2 instance with an S3 access policy. This works because the EC2 instance retrieves temporary, rotating security credentials from AWS Security Token Service (STS) through the instance metadata service (IMDS), eliminating the need to store any long-term access keys on the instance. On the AWS Certified DevOps Engineer Professional DOP-C02 exam, this scenario tests your understanding of the least privilege principle combined with secure credential management—a core DevOps pattern for automating infrastructure without hardcoded secrets. A common trap is selecting an option that stores IAM user keys on the instance or uses an S3 bucket policy with a principal that doesn't account for instance roles. Remember the mnemonic: **R.I.P.** — Role, Instance Metadata, and Proxy (STS) — to recall that the role provides temporary credentials via IMDS, not stored keys.
⚠ Common exam trap
Many exam-takers think storing credentials in user data or application code is acceptable, but the DOP-C02 exam specifically tests the principle of using IAM roles to avoid long-term credential storage on EC2 instances.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Attach an IAM role to the EC2 instance with an S3 access policy.
Attaching an IAM role to an EC2 instance allows the instance to obtain temporary security credentials from the AWS STS service via the instance metadata service (IMDS). The EC2 instance can then use these credentials to access the S3 bucket without any long-term AWS credentials being stored on the instance. This is the AWS-recommended best practice for granting permissions to AWS services running on EC2.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use an S3 bucket policy that grants access to the EC2 instance's public IP.
Why it's wrong here
Bucket policies cannot grant access based on EC2 instance IP alone; they require IAM roles.
- ✗
Store access keys in the EC2 user data script.
Why it's wrong here
Storing keys in user data exposes credentials and is not secure.
- ✗
Create an IAM user and embed credentials in the application code.
Why it's wrong here
Embedding credentials in code is a security risk and not a best practice.
- ✓
Attach an IAM role to the EC2 instance with an S3 access policy.
Why this is correct
The IAM role provides temporary credentials via the instance metadata service.
Quick reference
AWS S3 Storage Class Comparison
| Storage Class | Min Duration | Retrieval | Use Case |
|---|---|---|---|
| S3 Standard | None | Immediate | Frequently accessed data |
| S3 Standard-IA | 30 days | Immediate | Infrequent access, rapid retrieval |
| S3 One Zone-IA | 30 days | Immediate | Non-critical infrequent data |
| S3 Intelligent-Tiering | None | Immediate–hours | Unknown or changing access patterns |
| S3 Glacier Instant | 90 days | Milliseconds | Archive with instant retrieval |
| S3 Glacier Flexible | 90 days | Minutes–hours | Archive, flexible retrieval |
| S3 Glacier Deep Archive | 180 days | Hours | Long-term compliance archive |
Go deeper
Related to this question
About these practice questions
One of 251 original DOP-C02 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
Same concept, more angles
2 more ways this is tested on DOP-C02
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. An application running on EC2 needs to access an S3 bucket. To follow the principle of least privilege, what is the recommended approach?
easy- A.Store AWS access keys in the application configuration
- ✓ B.Create an IAM role with a policy allowing only necessary S3 actions and attach it to the EC2 instance
- C.Use an S3 bucket policy to allow access from the EC2 instance's public IP
- D.Configure the EC2 security group to allow outbound access to S3
Why B: The best practice is to create an IAM role with a policy that grants only the required S3 actions and attach the role to the EC2 instance. This avoids using long-term credentials. Access keys are long-term and insecure. Bucket policy can be used but is not the most secure for instance access. Security group is for network access, not API access.
Variation 2. A company uses AWS CodeBuild for CI/CD. The build project needs to access a private S3 bucket to download artifacts. What is the MOST secure way to grant access?
easy- A.Embed the access keys in the buildspec.yml file.
- ✓ B.Create an IAM role with read access to the bucket and attach it to the CodeBuild project.
- C.Use an S3 bucket policy that grants public read access.
- D.Store AWS access keys in CodeBuild environment variables.
Why B: Using an IAM role with a trust policy for CodeBuild is the most secure method because it avoids long-term credentials. Option B is correct. Option A is wrong because embedding access keys in the buildspec.yml file is insecure and exposes credentials. Option C is wrong because granting public read access to the S3 bucket would allow anyone to access the artifacts. Option D is wrong because storing access keys in environment variables is less secure than using an IAM role, as environment variables can be exposed.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This DOP-C02 practice question is part of Courseiva's free Amazon Web Services certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the DOP-C02 exam.