CloudWatch lets you publish custom metrics from your applications. These are metrics that are not provided by the AWS services themselves and usually make sense from a business perspective, not an infrastructure operations perspective, like orders processed per minute or abandoned shopping carts, etc.
Traditionally, custom metrics were published to CloudWatch by applications by calling CloudWatch’s PutMetricData API, most commonly through the use of AWS SDK for the language of your choice.
With the new CloudWatch Embedded Metric Format (EMF), you can simply embed the custom metrics in the logs that your application sends to CloudWatch, and CloudWatch will automatically extract the custom metrics from the log data. You can then graph these metrics in the CloudWatch console and even set alerts and alarms on them like other out-of-the-box metrics.
This works anywhere you publish CloudWatch logs from, ie, EC2 instances, on-prem VMs, Docker/Kubernetes containers in ECS/EKS, Lambda functions, etc.
In this article, we’ll see how to use the EMF to publish custom metrics from Lambda functions. The logs you push to CloudWatch, need to be in a particular format for this to work. You can either generate this JSON format in your application code or use helper libraries available for Node.js, Python, Java, and C#.
EMF in Node.js Lambda
AWS provides the aws-embedded-metrics NPM library to generate logs in the proper format with embedded custom metrics. Simply use it in your Lambda function code to put metrics.
The Lambda function below sends a random valued custom metric every second to CloudWatch:
Privacy & Cookies Policy
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.