DOP-C02 Security and Compliance Practice Question
A developer wants to grant an EC2 instance read-only access to a specific S3 bucket. Which AWS mechanism should they use to securely provide credentials to the instance?
⚠ Common exam trap
Many candidates think storing credentials in user data or an AMI is acceptable for automation, but the exam emphasizes that any static, long-term credentials on an instance are insecure and violate AWS best practices, whereas IAM roles provide secure, temporary, and automatically rotated credentials.
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
✓
Create an IAM role with the required permissions and attach it to the EC2 instance as an instance profile.
IAM roles with instance profiles provide temporary, automatically rotated credentials to EC2 instances via the AWS STS service. This eliminates the need to hardcode or store long-term access keys on the instance, adhering to the principle of least privilege and improving security posture.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables in the AMI.
Why it's wrong here
Baking long-term AWS access key environment variables into an AMI means the credentials are embedded in the snapshot and copied to every instance launched from it, making rotation a matter of rebuilding and republishing the image and creating a persistent credential leak surface. Anyone with read access to the AMI or who can inspect an instance's environment through processes or SSM can harvest the keys, which also violate least privilege because they are never scoped to a specific instance role. Unlike instance-profile temporary credentials fetched from the metadata service, these long-lived keys never auto-rotate, so a compromise remains valid indefinitely.
- ✓
Create an IAM role with the required permissions and attach it to the EC2 instance as an instance profile.
Why this is correct
An IAM role attached as an instance profile is the AWS-recommended mechanism because the EC2 instance automatically assumes the role through the instance metadata service (IMDS), receiving temporary credentials from AWS STS that are valid for a limited duration and refreshed automatically by the SDK or CLI. This eliminates the need to store or manage long-lived access keys while letting you scope the role with a narrowly defined read-only policy, such as allowing only s3:GetObject or ec2:DescribeActions. The instance profile acts as the container for the role, and the per-instance trust policy ensures only instances with that profile can use the role, giving centralized control, easy revocation, and cross-account access when needed.
- ✗
Store AWS access keys in the EC2 user data script.
Why it's wrong here
Storing AWS credentials in EC2 user data is dangerous because user data is accessible via the instance metadata service at http://169.254.169.254/latest/user-data, so any process or attacker with local code execution on the instance can trivially read the embedded access key ID and secret. Additionally, user data is visible in the EC2 console to any IAM principal with the ec2:DescribeInstanceAttribute permission, remains in plaintext for the entire instance lifecycle, and is often included in launch templates or CloudFormation resources, widening exposure to anyone who can view those templates. These long-lived keys do not rotate and persist even after the instance is terminated, making a breach difficult to detect and remediate.
- ✗
Retrieve the credentials from AWS Systems Manager Parameter Store using a custom script.
Why it's wrong here
Retrieving credentials from Parameter Store via a custom script would require the EC2 instance to have an IAM role that grants access to the Parameter Store itself, which introduces a circular dependency: the instance still needs an initial identity to fetch the S3 credentials. The correct mechanism—an IAM role attached directly to the EC2 instance—provides temporary credentials automatically via the instance metadata service, eliminating the need for any custom script or stored secret. This option is tempting because Parameter Store is designed for securely storing configuration data and secrets, and it would be the correct choice if the developer needed to rotate or centrally manage static credentials for an application that cannot use IAM roles, such as a legacy system running outside AWS.
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
This DOP-C02 question is part of Courseiva's 251-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
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.