Monitor Event Function performance with Amazon CloudWatch Lambda Insights
Amazon CloudWatch Lambda Insights collects and aggregates Lambda Event Function runtime performance metrics and logs for your serverless applications. This page describes how to enable and use Lambda Insights to diagnose issues with your Lambda Event Functions.
How Lambda Insights monitors serverless applications
CloudWatch Lambda Insights is a monitoring and troubleshooting solution for serverless applications running on AWS Lambda. The solution collects, aggregates, and summarizes system-level metrics including CPU time, memory, disk and network usage. It also collects, aggregates, and summarizes diagnostic information such as cold starts and Lambda worker shutdowns to help you isolate issues with your Lambda Event Functions and resolve them quickly.
Lambda Insights uses a new CloudWatch Lambda Insights extension, which is provided as a Lambda layer. When you enable this extension on a Lambda Event Function for a supported runtime, it collects system-level metrics and emits a single performance log event for every invocation of that Lambda Event Function. CloudWatch uses embedded metric formatting to extract metrics from the log events. For more information, see Using AWS Lambda extensions.
The Lambda Insights layer extends the CreateLogStream and PutLogEvents for the /aws/lambda-insights/ log group.
Pricing
When you enable Lambda Insights for your Lambda Event Function,
Lambda Insights reports 8 metrics per Event Function and every Event Function invocation sends about 1KB of log data to CloudWatch.
You only pay for the metrics and logs reported for your Event Function by Lambda Insights.
There are no minimum fees or mandatory service usage policies.
You do not pay for Lambda Insights if the Event Function is not invoked.
For a pricing example, see Amazon CloudWatch pricing
Supported runtimes
You can use Lambda Insights with any of the runtimes that support Lambda extensions.
Enabling Lambda Insights in the Lambda console
You can enable Lambda Insights enhanced monitoring on new and existing Lambda Event Functions. When you enable Lambda Insights on an Event Function in the Lambda console for a supported runtime, Lambda adds the Lambda Insights extension as a layer to your Event Function, and verifies or attempts to attach the CloudWatchLambdaInsightsExecutionRolePolicy
To enable Lambda Insights in the Lambda console
Open the Event Functions page
of the Lambda console. -
Choose your Event Function.
-
Choose the Configuration tab.
-
On the left menu, choose Monitoring and operations tools.
-
On the Additional monitoring tools pane, choose Edit.
-
Under CloudWatch Lambda Insights, turn on Enhanced monitoring.
-
Choose Save.
Enabling Lambda Insights programmatically
You can also enable Lambda Insights using the AWS Command Line Interface (AWS CLI), AWS Serverless Application Model (SAM) CLI, CloudFormation, or the AWS Cloud Development Kit (AWS CDK). When you enable Lambda Insights programmatically on an Event Function for a supported runtime, CloudWatch attaches the CloudWatchLambdaInsightsExecutionRolePolicy
For more information, see Getting started with Lambda Insights in the Amazon CloudWatch User Guide.
Using the Lambda Insights dashboard
The Lambda Insights dashboard has two views in the CloudWatch console: the multi-function overview and the single-function view. The multi-function overview aggregates the runtime metrics for the Lambda Event Functions in the current AWS account and Region. The single-function view shows the available runtime metrics for a single Lambda Event Function.
You can use the Lambda Insights dashboard multi-function overview in the CloudWatch console to identify over- and under-utilized Lambda Event Functions. You can use the Lambda Insights dashboard single-function view in the CloudWatch console to troubleshoot individual requests.
To view the runtime metrics for all Event Functions
-
Open the Multi-function
page in the CloudWatch console. -
Choose from the predefined time ranges, or choose a custom time range.
-
(Optional) Choose Add to dashboard to add the widgets to your CloudWatch dashboard.
To view the runtime metrics of a single Event Function
-
Open the Single-function
page in the CloudWatch console. -
Choose from the predefined time ranges, or choose a custom time range.
-
(Optional) Choose Add to dashboard to add the widgets to your CloudWatch dashboard.
For more information, see Creating and working with widgets on CloudWatch dashboards.
Example workflow to detect Event Function anomalies
You can use the multi-function overview on the Lambda Insights dashboard to identify and detect compute memory anomalies with your Event Function. For example, if the multi-function overview indicates that an Event Function is using a large amount of memory, you can view detailed memory utilization metrics in the Memory Usage pane. You can then go to the Metrics dashboard to enable anomaly detection or create an alarm.
To enable anomaly detection for an Event Function
-
Open the Multi-function
page in the CloudWatch console. -
Under Function summary, choose your Event Function's name.
The single-function view opens with the Event Function runtime metrics.
-
On the Memory Usage pane, choose the three vertical dots, and then choose View in metrics to open the Metrics dashboard.
-
On the Graphed metrics tab, in the Actions column, choose the first icon to enable anomaly detection for the Event Function.
For more information, see Using CloudWatch Anomaly Detection.
Example workflow using queries to troubleshoot an Event Function
You can use the single-function view on the Lambda Insights dashboard to identify the root cause of a spike in Event Function duration. For example, if the multi-function overview indicates a large increase in Event Function duration, you can pause on or choose each Event Function in the Duration pane to determine which Event Function is causing the increase. You can then go to the single-function view and review the Application logs to determine the root cause.
To run queries on an Event Function
-
Open the Multi-function
page in the CloudWatch console. -
In the Duration pane, choose your function to filter the duration metrics.
-
Open the Single-function
page. -
Choose the Filter metrics by function name dropdown list, and then choose your function.
-
To view the Most recent 1000 application logs, choose the Application logs tab.
-
Review the Timestamp and Message to identify the invocation request that you want to troubleshoot.
-
To show the Most recent 1000 invocations, choose the Invocations tab.
-
Select the Timestamp or Message for the invocation request that you want to troubleshoot.
-
Choose the View logs dropdown list, and then choose View performance logs.
An autogenerated query for your Event Function opens in the Logs Insights dashboard.
-
Choose Run query to generate a Logs message for the invocation request.
What's next?
-
Learn how to create a CloudWatch Logs dashboard in Create a Dashboard in the Amazon CloudWatch User Guide.
-
Learn how to add queries to a CloudWatch Logs dashboard in Add Query to Dashboard or Export Query Results in the Amazon CloudWatch User Guide.