DVA-C02 Question 8
Select 2You are developing a serverless application hosted on AWS using AWS Lambda. The application needs to process messages from an Amazon SQS queue. To ensure the Lambda function can read messages from the queue, which of the following steps must you take?
- A
Attach an IAM policy to the Lambda execution role that grants the 'sqs:ReceiveMessage' permission on the specific queue.
- B
Enable the event source mapping between the SQS queue and the Lambda function.
- C
Configure the SQS queue to trigger the Lambda function by adding the Lambda function's ARN to the queue's resource policy.
- D
Configure the Lambda function to use the default execution role provided by AWS Lambda without any additional permissions.
- E
Manually poll the SQS queue for messages within the Lambda function code.
Show answer and explanation
Correct answers: A, B
Explanation
To enable an AWS Lambda function to process messages from an Amazon SQS queue, you need to ensure the Lambda execution role has proper permissions (such as 'sqs:ReceiveMessage') to access the queue and configure an event source mapping between the SQS queue and the Lambda function. This setup allows Lambda to automatically poll and invoke the function when new messages are available in the queue.
- A. Correct.
Correct. The Lambda execution role needs permissions to interact with the SQS queue. Attaching an IAM policy with 'sqs:ReceiveMessage' ensures the function can pull messages from the queue.
- B. Correct.
Correct. Enabling the event source mapping establishes the connection between the SQS queue and the Lambda function, allowing automatic invocation of the function when new messages arrive in the queue.
- C. Incorrect.
Incorrect. While resource policies are used to control access to SQS queues, they are not required when Lambda is consuming messages from an SQS queue in the same account. The permissions are managed via the Lambda execution role.
- D. Incorrect.
Incorrect. The default execution role provided by AWS Lambda does not include permissions for accessing SQS queues. Additional permissions must be explicitly attached to the role.
- E. Incorrect.
Incorrect. Lambda automatically polls the SQS queue when event source mapping is configured, so manual polling is unnecessary and not recommended.