A developer is troubleshooting an application that streams JSON logs to an Amazon CloudWatch Logs log group. A representative log event in the log group is structured as follows:
{
"timestamp": "2026-07-14T12:00:00Z",
"level": "ERROR",
"error": {
"code": "DB_CONNECTION_FAILED",
"message": "Failed to connect to the database instance."
}
}
The developer created a CloudWatch metric filter with the pattern `{ $.error = "DB_CONNECTION_FAILED" }` to monitor database connection failures. However, the associated metric is not being populated, even though matching error logs are visible in the log group. What is the reason for this behavior?
- The filter pattern references the parent error object rather than the nested code property. It should be updated to { $.error.code = "DB_CONNECTION_FAILED" }.Answer
- BThe filter pattern must use the SQL-like LIKE operator instead of = for string matching within JSON structures, formatted as { $.error.code LIKE "%DB_CONNECTION_FAILED%" }.
- CThe filter pattern must be written in the space-delimited format [timestamp, level, error = "DB_CONNECTION_FAILED"] because CloudWatch cannot parse JSON objects natively without a pre-defined schema.
- DThe application's execution context is reusing stale connections that timed out, preventing CloudWatch Logs from receiving the status code payload entirely.
Answer
The filter pattern references the parent error object rather than the nested code property. It should be updated to { $.error.code = "DB_CONNECTION_FAILED" }.
The correct answer explains that CloudWatch Logs requires a fully qualified JSON path to target a leaf node. The developer's pattern references the parent object 'error' (which evaluates to a nested map/dictionary) instead of the string property 'code'. By correcting the path to $.error.code, CloudWatch is able to successfully perform the equality comparison with the string 'DB_CONNECTION_FAILED'.
Step-by-Step Solution
Key Concept
CloudWatch Logs JSON Metric Filter Path Syntax