Enhancing AWS Lambda Monitoring with CloudWatch Lambda Insights

Raul NaupariRaul Naupari
4 min read

While AWS Lambda offers default metrics for basic monitoring, sometimes we need to look closely at function performance and resource use. Here is where CloudWatch Lambda Insights helps, providing enhanced observability designed for our Lambda functions.

What is CloudWatch Lambda Insights?

Lambda Insights collects, aggregates, and summarizes a new set of metrics for our functions. It does this by using a CloudWatch Lambda Extension (provided as a Lambda layer), which runs alongside our function in the same execution environment. This extension gathers detailed performance data for each invocation and sends it as log events using the embedded metric format to a dedicated CloudWatch Log Group (/aws/lambda-insights). CloudWatch then processes these logs to generate metrics.

What Metrics Does Lambda Insights Track?

Lambda Insights provides a new set of metrics. Key metrics include:

  • duration: The overall duration of the invocation.

  • cpu_total_time: Total CPU time used by the function.

  • memory_utilization: Maximum memory used as a percentage of allocated memory.

  • total_network: The number of bytes received and sent by the function.

  • fd_use: The maximum number of file descriptors in use.

  • init_duration: The time spent in the init phase of the Lambda execution environment lifecycle.

  • total_memory: The amount of memory allocated to our function.

  • billed_duration: Includes the duration plus a small amount of time needed to initialize the runtime.

  • billed_mb_ms: The total amount of compute resources used by our function is calculated by multiplying total_memory by the billed_duration.

Use Cases for Lambda Insights

Enabling Lambda Insights is particularly beneficial in the following scenarios:

  • Cold Start Analysis: The detailed breakdown of initialization times helps identify and reduce cold start latency.

  • Performance Optimization: Lambda Insights offers detailed memory and CPU usage metrics that help identify resource-related bottlenecks.

  • Cost Optimization: By identifying inefficient resource utilization, Lambda Insights helps optimize function configurations to reduce costs while maintaining performance.

  • Anomaly Detection: Lambda Insights enables easier identification of unexpected behavior patterns, such as memory leaks, excessive CPU utilization, running out of file descriptors, or unusual network activity.

  • Troubleshooting Issues: The detailed metrics make it easier to diagnose issues occurring in our functions.

Lambda Insights vs. Default Lambda Metrics

  • The default Lambda metrics are aggregated per minute, while Lambda Insights allows us to see the values for each invocation.

  • Lambda Insights offers detailed system-level resource usage metrics (CPU, Memory, Network).

  • The default Lambda metrics are enabled by default, while Lambda Insights needs to be activated for each function.

  • Lambda Insights is not free. It incurs costs based on the volume of log data ingested into CloudWatch Logs.

Let's deploy a simple .NET Lambda function with Lambda Insights enabled using the SAM.

Pre-requisites

  • Have an IAM User with programmatic access.

  • Install the Amazon Lambda Templates (dotnet new -i Amazon.Lambda.Templates)

  • Install the Amazon Lambda Tools (dotnet tool install -g Amazon.Lambda.Tools)

  • Install AWS SAM CLI.

The Lambda Function

Run the following commands to set up our Lambda function:

dotnet new lambda.EmptyFunction -n MyLambda -o .
dotnet add src/MyLambda package Amazon.Lambda.APIGatewayEvents
dotnet new sln -n MyApplications
dotnet sln add --in-root src/MyLambda

Open the Program.cs file and update the content as follows:

using Amazon.Lambda.Core;
using Amazon.Lambda.APIGatewayEvents;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace MyLambda;

public class Function
{
    public APIGatewayHttpApiV2ProxyResponse FunctionHandler(APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context)
    {
        return new APIGatewayHttpApiV2ProxyResponse
        {
            Body = @"{""Message"":""Hello World""}",
            StatusCode = 200,
            Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
        };
    }
}

AWS SAM template

At the solution level, create a template.yaml file with the following content:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  SAM

Resources:

  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Timeout: 60
      MemorySize: 512
      Tracing: Active
      Runtime: dotnet8
      Architectures:
        - x86_64    
      Handler: MyLambda::MyLambda.Function::FunctionHandler
      CodeUri: ./src/MyLambda/
      Policies:
        - CloudWatchLambdaInsightsExecutionRolePolicy
      Layers:
        - !Sub arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension:55
      Events:
        get:
          Type: Api
          Properties:
            Path: /
            Method: get

Outputs:
  MyApiEndpoint:
    Description: "API endpoint"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod"

To add Lambda Insights to our functions, we need to configure two things:

  • Add the CloudWatchLambdaInsightsExecutionRolePolicy policy. This policy provides the required permissions to send metrics and logs specifically for Lambda Insights.

  • Add the specific layer with the Lambda Insights extension. You can find all the layers here.

Run the following commands to deploy the resources to AWS:

sam build
sam deploy --guided

Final Words

Do not enable Lambda Insights on all functions by default. Enable it only for:

  • Critical production functions where deep visibility is essential.

  • Functions currently undergoing performance tuning or troubleshooting.

  • During the development and testing phases.

Configure an appropriate data retention period for the /aws/lambda-insights log group. Storing insights data indefinitely may incur ongoing CloudWatch Logs storage costs. You can find all the code here. Thanks, and happy coding.

0
Subscribe to my newsletter

Read articles from Raul Naupari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Raul Naupari
Raul Naupari

Somebody who likes to code