Skip to Content

DVA-C02: How to Log Key Events in AWS Lambda Function with Unique Identifier

Learn how to use the AWS request ID field in the context object to log key events in an AWS Lambda function. Find out the best way to write logs to standard output.

Table of Contents

Question

A developer is writing an AWS Lambda function. The developer wants to log key events that occur while the Lambda function runs. The developer wants to include a unique identifier to associate the events with a specific function invocation. The developer adds the following code to the Lambda function:

function handler (event, context) {

}

Which solution will meet this requirement?

A. Obtain the request identifier from the AWS request ID field in the context object. Configure the application to write logs to standard output. Most Voted

B. Obtain the request identifier from the AWS request ID field in the event object. Configure the application to write logs to a file.

C. Obtain the request identifier from the AWS request ID field in the event object. Configure the application to write logs to standard output.

D. Obtain the request identifier from the AWS request ID field in the context object. Configure the application to write logs to a file.

Answer

A. Obtain the request identifier from the AWS request ID field in the context object. Configure the application to write logs to standard output. Most Voted

Explanation

The correct answer to the question is A. Obtain the request identifier from the AWS request ID field in the context object. Configure the application to write logs to standard output.

The reason for this answer is that:

  • Option A: Obtaining the request identifier from the AWS request ID field in the context object and configuring the application to write logs to standard output is the best way to log key events that occur while the Lambda function runs and include a unique identifier to associate the events with a specific function invocation. The context object is an argument that is passed to the Lambda function handler along with the event object. The context object contains information about the invocation, function, and execution environment, such as the function name, memory limit, log group name, and log stream name. The context object also contains the AWS request ID field, which is a unique identifier for each invocation of a Lambda function. By accessing this field in the function code, you can obtain the request identifier and use it in your log statements. For example, in Python, you can use context.aws_request_id to get the request identifier. To write logs to standard output, you can use any method that prints to stdout or stderr, such as print or logging. The Lambda runtime environment will automatically send these logs to Amazon CloudWatch Logs, where you can view and analyze them.
  • Option B: Obtaining the request identifier from the AWS request ID field in the event object and configuring the application to write logs to a file is not a valid way to log key events that occur while the Lambda function runs and include a unique identifier to associate the events with a specific function invocation. The event object is an argument that is passed to the Lambda function handler along with the context object. The event object contains data about the source of the invocation, such as an API Gateway request, an S3 event notification, or a custom payload. The event object does not contain the AWS request ID field, which is only available in the context object. Therefore, you cannot obtain the request identifier from the event object. To write logs to a file, you would need to use a file system that is accessible by your Lambda function, such as /tmp or Amazon EFS. However, this is not recommended, as it adds complexity and overhead to your logging process. Moreover, writing logs to a file does not automatically send them to CloudWatch Logs, where you can view and analyze them.
  • Option C: Obtaining the request identifier from the AWS request ID field in the event object and configuring the application to write logs to standard output is not a correct way to log key events that occur while the Lambda function runs and include a unique identifier to associate the events with a specific function invocation. As explained in option B, you cannot obtain the request identifier from the event object, as it does not contain the AWS request ID field. You need to use the context object instead. Configuring the application to write logs to standard output is correct, as it allows you to send logs to CloudWatch Logs automatically. However, this alone is not sufficient, as you also need to include the request identifier in your log statements.
  • Option D: Obtaining the request identifier from the AWS request ID field in the context object and configuring the application to write logs to a file is not an optimal way to log key events that occur while the Lambda function runs and include a unique identifier to associate the events with a specific function invocation. As explained in option A, you can obtain the request identifier from the context object, as it contains the AWS request ID field. However, configuring the application to write logs to a file is not recommended, as it adds complexity and overhead to your logging process. Moreover, writing logs to a file does not automatically send them to CloudWatch Logs, where you can view and analyze them. You should use standard output instead.

The latest AWS Certified Developer – Associate DVA-C02 certification actual real practice exam question and answer (Q&A) dumps are available free, which are helpful for you to pass the AWS Certified Developer – Associate DVA-C02 exam and earn AWS Certified Developer – Associate DVA-C02 certification.

AWS Certified Developer - Associate DVA-C02 Exam Questions and Answers