A DevOps team is developing a new serverless application using AWS Lambda functions. They need to collect custom application metrics, such as the number of successful order placements and the duration of specific business transactions, directly from their Lambda functions. These metrics should be available in CloudWatch for dashboarding and alarming, and the developers want to achieve this with minimal code complexity and overhead. Which method is most efficient for publishing these custom metrics?
- ADeploy a custom Prometheus node exporter within the Lambda environment to push metrics to Amazon Managed Service for Prometheus (AMP).
- BSend custom metrics as log events to CloudWatch Logs and then create CloudWatch Log Metric Filters.
- CUse the CloudWatch PutMetricData API call directly within the Lambda function code for each metric.
- DEmploy the CloudWatch Embedded Metric Format (EMF) to publish structured log events from Lambda.
Show answer & explanationAnswer & explanation
Correct answer: D. Employ the CloudWatch Embedded Metric Format (EMF) to publish structured log events from Lambda.
The CloudWatch Embedded Metric Format (EMF) is specifically designed for publishing custom metrics from serverless applications with minimal overhead. Developers can embed metric data within structured log events, and CloudWatch automatically extracts and ingests these metrics without requiring explicit PutMetricData API calls for each metric, reducing code complexity and improving efficiency compared to direct API calls or log metric filters for multiple metrics.
Why the other options are wrong
- A. Deploying a Prometheus exporter within a Lambda environment is not a native or efficient pattern for serverless; Lambda's ephemeral nature and execution model are not well-suited for traditional Prometheus scraping or push-based exporters in this manner, and it adds significant complexity.
- B. Creating a CloudWatch Log Metric Filter for each custom metric can become cumbersome for many metrics and adds a layer of indirection, which is less efficient than EMF for direct metric publishing.
- C. Direct PutMetricData calls can be chatty and incur overhead, especially for multiple metrics within a single Lambda invocation, increasing code complexity and potential for throttling.
CloudWatch Embedded Metric Format (EMF)
A specification for publishing custom metrics to Amazon CloudWatch by embedding them within structured JSON log events. CloudWatch automatically extracts these metrics.
- Embeds multiple metrics in a single log event.
- Reduces API calls and overhead for custom metrics.
- Simplifies metric publishing for serverless applications.
Memory trick: EMF Embeds Metrics Effortlessly from Lambda.