Question #602
A developer is debugging an AWS Lambda function and wants to trace errors across different invocations. The developer needs a unique identifier to link each error to a specific invocation. The Lambda function's code includes the following handler:python<br>def handler(event, context):<br> ...<br>
Which approach will achieve this?
Obtain the request ID from the context's AWS request ID field and log to standard output.
Obtain the request ID from the event's AWS request ID field and log to a file.
Obtain the request ID from the event's AWS request ID field and log to standard output.
Obtain the request ID from the context's AWS request ID field and log to a file.
Explanation
Answer A is correct because:
1. Context Object: The AWS Lambda context parameter contains runtime information, including context.aws_request_id, a unique identifier for each invocation.
2. Logging to Standard Output: Lambda automatically sends logs from standard output (stdout) to CloudWatch, making it the recommended way to capture logs.
Why other options are incorrect:
- B & C: The event parameter contains input data, not the request ID. AWS does not include a request ID in the event.
- D: While context.aws_request_id is correct, logging to a file is unnecessary and not automatically captured by Lambda. Standard output is the proper channel.
Key Takeaway: Use context.aws_request_id for the unique invocation ID and log to stdout for CloudWatch integration.
Answer
The correct answer is: A