Courseiva
Security and Compliance →mediumMultiple Choice

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

    S3 bucket policies evaluate IAM principals, not EC2 public IP addresses, and instance public IPs change on stop/start, so the policy cannot reliably identify the instance. It is tempting because IP-based conditions exist in bucket policies, and they would be correct for restricting access to a fixed corporate egress range.

  • ✗

    Store access keys in the EC2 user data script.

    Why it's wrong here

    User data is readable from instance metadata and stored in launch templates, so embedding access keys there still stores long-term credentials on the instance. It is tempting because user data can bootstrap configuration at launch, and it would be correct for installing packages or joining a domain, not for credential delivery.

  • ✗

    Create an IAM user and embed credentials in the application code.

    Why it's wrong here

    Embedding IAM user credentials in application code stores long-lived secrets on the instance and in source control, directly violating the no-stored-credentials requirement. It is tempting because a dedicated IAM user gives granular permissions, and it would be correct for an on-premises workload that cannot assume a role.

  • ✓

    Attach an IAM role to the EC2 instance with an S3 access policy.

    Why this is correct

    An IAM role attached to the instance delivers temporary credentials through the instance metadata service, so the AWS SDK retrieves them automatically. This satisfies the no-stored-credentials constraint, unlike embedding access keys in code or on the instance filesystem.

Quick reference

AWS S3 Storage Class Comparison

Storage ClassMin DurationRetrievalUse Case
S3 StandardNoneImmediateFrequently accessed data
S3 Standard-IA30 daysImmediateInfrequent access, rapid retrieval
S3 One Zone-IA30 daysImmediateNon-critical infrequent data
S3 Intelligent-TieringNoneImmediate–hoursUnknown or changing access patterns
S3 Glacier Instant90 daysMillisecondsArchive with instant retrieval
S3 Glacier Flexible90 daysMinutes–hoursArchive, flexible retrieval
S3 Glacier Deep Archive180 daysHoursLong-term compliance archive

About these practice questions

One of 1,298 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 →

How Courseiva writes practice questions · Editorial policy

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: Attaching an IAM role to the CodeBuild project allows CodeBuild to assume temporary credentials via the AWS STS service, eliminating the need to store any long-lived secrets. The role's policy can be scoped to only the specific S3 bucket and actions required, following least privilege. This is the AWS-recommended pattern for service-to-service authentication.

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.