DVA-C02 Development with AWS Services Practice Question
A developer is building a serverless application using AWS Lambda. The function needs to access a private S3 bucket in the same AWS account. What is the BEST way to grant the Lambda function access to the bucket?
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 execution role with an S3 access policy and attach it to the Lambda function.
The correct answer. The best practice for granting an AWS Lambda function access to an S3 bucket in the same account is to create an IAM execution role with a policy that allows the necessary S3 actions, and then attach that role to the Lambda function. This provides temporary credentials via STS, follows the principle of least privilege, and avoids hardcoding credentials. Option B is incorrect because storing AWS credentials in environment variables is insecure and can lead to accidental exposure. AWS recommends using IAM roles for temporary credentials. Option C is incorrect because Lambda functions do not support attaching IAM policies directly. Policies must be attached to an IAM role, and that role is assigned to the function. Option D is incorrect because while a bucket policy could grant access based on the function's ARN, it is not the best approach for same-account access. Using an execution role is more scalable, easier to manage, and follows the principle of least privilege.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Create an IAM execution role with an S3 access policy and attach it to the Lambda function.
Why this is correct
Creating an IAM execution role with an S3 access policy and attaching it to the Lambda function is the standard and most secure method. This role provides the Lambda function with temporary, scoped credentials to interact with other AWS services like S3, adhering to the principle of least privilege. It ensures that the function only has the necessary permissions without exposing sensitive, long-lived credentials.
- ✗
Store AWS credentials in environment variables and use them in the function code.
Why it's wrong here
Storing AWS credentials directly in environment variables within a Lambda function is a significant security anti-pattern. These credentials would be exposed if the function's configuration is accessed, and they grant persistent, potentially overly broad access, violating the principle of least privilege. This method bypasses the secure, temporary credential management provided by IAM roles, increasing the risk of compromise.
- ✗
Attach an inline IAM policy directly to the Lambda function.
Why it's wrong here
Attaching an inline IAM policy directly to a Lambda function is not a supported AWS mechanism. Lambda functions do not directly accept IAM policies; instead, they assume an IAM execution role. All permissions for the function, whether defined by inline or managed policies, must be attached to this designated execution role, not the function resource itself.
- ✗
Add a bucket policy to the S3 bucket allowing the Lambda function's ARN.
Why it's wrong here
Adding a bucket policy directly allowing the Lambda function's ARN is incorrect because Lambda functions assume an IAM Role for permissions within the same account. The standard and most secure approach is to attach an IAM policy to the Lambda function's execution role, granting S3 access. Bucket policies are primarily designed for granting cross-account access to S3 buckets, where the principal would be an ARN from another account, or for defining public access. It is tempting as bucket policies do control S3 access, and are essential for external principals.
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 DVA-C02 question is part of Courseiva's 724-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 DVA-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 DVA-C02 exam.