Skip to main content


This middleware hydrates lambda's context.metrics property with an instance of MetricLogger. This instance can be used to easily generate custom metrics from Lambda functions without requiring custom batching code, making blocking network requests or relying on 3rd party software.

Metrics collected with this logger are then available for querying within AWS CloudWatch Log Insights

You can explore all the MetricLogger APIs following aws-embedded-metrics documentation.


To install this middleware you can use NPM:

npm install --save @middy/cloudwatch-metrics


  • namespace (string) (optional): Defaults to aws-embedded-metrics. Sets the CloudWatch namespace that extracted metrics should be published to.
  • dimensions (Record<String, String> | Record<String, String>[]) (optional): Explicitly overrides all dimensions. This will remove the default dimensions. You can provide an empty array to record all metrics without dimensions. For dimensions defaults and configuration see the aws-embedded-metrics docs.

Sample usage

const middy = require('@middy/core')
const cloudwatchMetrics = require('@middy/cloudwatch-metrics')

const lambdaHandler = (event, context) => {
context.metrics.putMetric('ProcessingLatency', 100, 'Milliseconds')
export const handler = middy()
namespace: 'myAppliction',
dimensions: [{ Action: 'Buy' }]