Question

Difficulty: MediumMonitoring and Analyzing Logs with Amazon CloudWatch

A developer is troubleshooting a serverless application where an AWS Lambda function has a configured timeout of 15 seconds. The function is designed to write a custom JSON log entry to Amazon CloudWatch Logs at the end of its execution, structured as { "requestID": "123-456", "status": "COMPLETED", "executionTimeMs": 1250 }. If a database delay occurs, the function catches it and logs { "requestID": "123-456", "status": "ERROR", "errorType": "DatabaseTimeout" }. To track performance issues and failures, the developer configures a CloudWatch Metric Filter with the pattern { (.status="ERROR")(.status = "ERROR") || (.executionTimeMs > 15000) }. During testing, several invocations time out, but the metric filter does not register any data points. Why is the metric filter failing to capture these timeout events?

  1. A
    The metric filter pattern is invalid because CloudWatch Logs JSON filter expressions require the double equality operator '==' rather than a single '=' for string equality evaluations.
  2. When a Lambda function times out, the execution is abruptly terminated by the Lambda runtime, preventing the custom log statement from being written. The metric filter must instead match the platform-generated log string 'Task timed out'.Answer
  3. C
    Lambda execution environments run in isolated sandboxes that prevent CloudWatch metric filters from reading log streams until the sandbox is completely recycled.
  4. D
    The metric filter is failing because JSON property names containing mixed case characters, such as 'executionTimeMs', must be converted to uppercase within the filter pattern expression.

Answer

When a Lambda function times out, the execution is abruptly terminated by the Lambda runtime, preventing the custom log statement from being written. The metric filter must instead match the platform-generated log string 'Task timed out'.
The correct answer is correct because AWS Lambda enforces execution timeouts at the platform level. If the function times out, the execution container is terminated immediately, preventing any application-level catch blocks or log statements from executing. As a result, the custom JSON log containing the execution time is never written. The developer must instead match the platform-generated log line which contains the phrase 'Task timed out'.

Step-by-Step Solution

1
Analyze how AWS Lambda handles timeouts at the runtime level.
When the timeout threshold (15 seconds) is reached, the Lambda service immediately halts container execution.
This prevents any subsequent application-level code (including catch blocks or logging libraries) from running.
2
Determine where the log entries originate when a timeout occurs.
The application's custom JSON logs are not written. Instead, the AWS Lambda service writes a standard platform message containing the string 'Task timed out after 15.00 seconds'.
Since the application did not write the log, the metric filter looking for custom JSON properties like status or executionTimeMs will find nothing.
3
Formulate a metric filter pattern to capture these timeout events.
Create a metric filter that matches the string 'Task timed out' in the log group.
This platform-generated string is guaranteed to be logged by the Lambda service when a timeout occurs.

Key Concept

CloudWatch Logs filters and Lambda timeout log generation
Rate this question