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
Node js

Prerequisites

  1. Node.js 18 +:

Features Supported

TracesMetricsApp LogsCustom LogsProfiling

Installation

1 Add OpenTelemetry (OTel) Layer to Lambda

a: Access the OTel Layer

  1. Navigate to the OpenTelemetry Lambda Repository.
  2. Locate the appropriate layer for your environment.

b: Match AWS Region and CPU Architecture

Copy the Amazon Resource Name (ARN) and set the configuration to match your Lambda function’s region and CPU architecture. Example ARN:

For example, for the us-east-1 region:

c: Add the OTel Layer to Lambda

  1. In the AWS Console, navigate to LambdaFunctions → your specific Lambda function.
  2. Go to the Layers section and click on Add Layer.

d: Specify the ARN

  1. In the Choose Layer section, specify the ARN from Step 1b.
  2. Click Add to save the layer.

e: Verify and Add OTel Layer

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

node-add-layer

2 Set Environment Variables

Set the following environment variables in your Lambda function configuration:

node-env

3 Create lambda-config.js

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.

4 Enable X-Ray Tracing

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

node-xtrace

5 Add Custom Instrumentation (Optional)

If you need to add custom spans or attributes, you can use the OpenTelemetry API as follows:

Troubleshooting

If you encounter missing spans:

  1. Verify that all environment variables are correctly set.
  2. 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.

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

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