AWS Lambda

This guide walks you through instrumenting AWS Lambda with OpenTelemetry and Middleware. These instructions can also be found on the Installation page in your Middleware Account.

Introduction

OpenTelemetry provides a standardized way to instrument applications for collecting telemetry data. However, implementing OpenTelemetry in AWS Lambda presents unique challenges due to the serverless nature of the environment. This guide aims to create a seamless OpenTelemetry experience for Lambda users for different programming languages

Key Challenges

  1. Early Function Exit: Lambda functions may exit before traces can be exported.
  2. Container Freezing: AWS Lambda may freeze the container if no new requests arrive, suspending all processes.
  3. Performance Concerns: The function should remain as lightweight as possible for optimal Lambda performance.
node-lambda-layer

Prerequisites

Node.js
Python
.NET
  1. Node.js 18+: check version with node -v
  1. Python 3.8+: check with python -V
  1. .NET 6+: check version with dotnet --version

Features Supported

TracesMetricsApp LogsCustom LogsProfiling

.NET does not Support Application logs by default.

Installation

Node.js
Python
.NET
  1. Copy the ARN for the OpenTelemetry (OTel) Collector Lambda Layer:

    1. Navigate to the OpenTelemetry Collector Lambda Repository and locate the appropriate layer for your environment.

    2. Copy the Amazon Resource Name (ARN) and change the <region> and <amd|arm> tags to the region and cpu architecture of your Lambda.

  2. Copy the ARN for the OTel Auto Instrumentation Layer:

    1. Navigate to the OpenTelemetry Lambda Repository and locate the appropriate layer for your environment.

    2. Copy the ARN and and change the <region> tag to the region your Lambda is in.

  3. In the AWS Console navigate to LambdaFunctions → your specific Lambda function and create a Layer for each ARN above

The Layers above must be added in order - Collector then Auto Instrumentation - or they will not work.

  1. Review the added layer and ensure it matches the ARN. Finally, click Add to complete the process.

  2. Set the following environment variables in your Lambda function configuration:

    If you are using *.mjs / *.ts you may need to set NODE_OPTIONS=--import ./lambda-config.[mjs/ts]

  3. Create a file named lambda-config.js in your Lambda function's root directory with the following content:

    This configuration overrides the default Batch Span Processor with a Simple Span Processor, ensuring immediate processing and exporting of spans.

  1. Copy the ARN for the OpenTelemetry (OTel) Collector Lambda Layer:

    1. Navigate to the OpenTelemetry Collector Lambda Repository and locate the appropriate layer for your environment.

    2. Copy the Amazon Resource Name (ARN) and change the <region> and <amd|arm> tags to the region and cpu architecture of your Lambda.

  2. Copy the ARN for the OTel Auto Instrumentation Layer:

    1. Navigate to the OpenTelemetry Lambda Repository and locate the appropriate layer for your environment.

    2. Copy the ARN and and change the <region> tag to the region your Lambda is in.

  3. In the AWS Console navigate to LambdaFunctions → your specific Lambda function and create a Layer for each ARN above.

The Layers above must be added in order - Collector then Auto Instrumentation - or they will not work.

  1. Review the added layer and ensure it matches the ARN. Finally, click Add to complete the process.

  2. Set the following environment variables in your Lambda function configuration:

  3. Import and set up a LoggerProvider to send log messages at desired log levels:

  1. First you'll need to install the MW.APM package via CLI or Visual Studio.

    1. For CLI run

    2. For Visual Studio in Windows by searching for the MW.APM nuget package in Tools > Nuget Package Manager > Manage Nuget Packages for Solutions > Browse

  2. Add the following to a your program.cs file:

  3. Add the below configs to your appsettings.json and then start your project. Note that ApiKey and TargetURL will be populated once logged in to the docs:

    If you want to know more about all 2 methods and its attributes. You can refer this link

This is a one-time configuration. After these configuration changes are made, each time the lambda function deployed, the .NET instrumentation will also run.

Verify Installation

Deploy and Test your application .Check your Middleware account for traces by navigating to APM -> Services and finding the name of your function in the services list.

node-example

X-Ray Tracing

Ensure X-Ray tracing is enabled for your Lambda function to get the full benefit of the instrumentation.

node-xtrace

Sending Custom Data

To capture logic specific to your business you can use the OpenTelemetry API as follows:

Node.js
Python

Troubleshooting

Missing Spans

  1. Verify that all environment variables are correctly set.
  2. Fore Node, check that the lambda-config.js file is in the root directory of your Lambda function.
  3. Ensure that the OpenTelemetry layer is correctly added to your function.
  4. Review CloudWatch logs for any error messages related to OpenTelemetry.

Need assistance or want to learn more about Middleware? Contact our support team in Slack.