Skip to content
Maple Docs
Open app

AWS Lambda instrumentation

Instrument Node.js and Python AWS Lambda functions with the OpenTelemetry Lambda layers and send traces, logs, and metrics to Maple.

This guide adds the OpenTelemetry Lambda layer to a Node.js or Python function, so each invocation becomes a trace in Maple with spans for the AWS SDK calls and HTTP requests it makes, plus database queries once you enable their instrumentation. It needs no code change.

To have a coding agent do this setup, use the maple-onboard skill. To check an existing setup, use maple-audit.

Prerequisites

  • A Lambda function on a Node.js 18+ or Python 3.9+ managed runtime.
  • An ingest key from Settings → Ingestion in Maple. Use the private key (maple_sk_…).

Add the layer

The OpenTelemetry project publishes one layer per language in every AWS region, from AWS account 184161586896. Each release has its own layer name, with the version in it:

Runtime Layer ARN
Node.js arn:aws:lambda:<region>:184161586896:layer:opentelemetry-nodejs-0_24_0:1
Python arn:aws:lambda:<region>:184161586896:layer:opentelemetry-python-0_22_0:1

Replace <region> with your function’s region. A layer can only be used in the region it was published in. The versions above are the releases of October 2026. Check the releases page for newer ones (layer-nodejs/… and layer-python/…).

Add the layer to the function. The next step sets AWS_LAMBDA_EXEC_WRAPPER, which makes the runtime start your handler through the layer’s wrapper.

--layers replaces the function’s whole layer list. If the function already uses layers, list them first, in their current order, then the OpenTelemetry ARN. To see the current list, run aws lambda get-function-configuration --function-name my-function --query 'Layers[].Arn'. The console’s Add a layer action appends instead.

aws lambda update-function-configuration \
  --function-name my-function \
  --layers arn:aws:lambda:us-east-1:184161586896:layer:opentelemetry-nodejs-0_24_0:1

AWS also publishes ADOT layers (AWS Distro for OpenTelemetry). They are built to send traces to CloudWatch. This guide uses the community layers, which send to any OTLP endpoint with the standard variables.

Configure

Set these environment variables on the function, in the console under Configuration → Environment variables, or in your infrastructure code:

AWS_LAMBDA_EXEC_WRAPPER="/opt/otel-handler"
OTEL_SERVICE_NAME="my-function"
OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.maple.dev"
OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"
OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20YOUR_INGEST_KEY"
OTEL_RESOURCE_ATTRIBUTES="deployment.environment.name=production,vcs.repository.url.full=https://github.com/acme/my-function"

Use https://ingest.eu.maple.dev for an EU organization. In OTEL_EXPORTER_OTLP_HEADERS the space after Bearer is written %20, because the specification URL-decodes header values. Both layers export OTLP over HTTP with protobuf: the Node.js layer always does, and the Python layer only ships the HTTP exporter.

An ingest key can only write telemetry to your organization. It cannot read data or call the Maple API. A plain environment variable is fine for most functions. To keep it out of the function configuration, store it in AWS Secrets Manager or Parameter Store and inject it at deploy time.

Without OTEL_SERVICE_NAME, the Python layer uses the function name. Both layers add cloud.region, faas.name and the other Lambda resource attributes on their own.

Node.js

The layer starts the OpenTelemetry Node.js SDK with the AWS SDK and Lambda instrumentations, plus only http, dns and net by default. Database, framework and logging instrumentations such as pg, mysql2, ioredis, mongodb, express, undici and pino are bundled but off until you name them. OTEL_NODE_ENABLED_INSTRUMENTATIONS replaces the default list, so keep http in it. Both variables take package names without the @opentelemetry/instrumentation- prefix:

OTEL_NODE_ENABLED_INSTRUMENTATIONS="http,undici,pg"   # only these, for query spans from pg
OTEL_NODE_DISABLED_INSTRUMENTATIONS="dns,net"         # the defaults except these

The layer ignores OTEL_EXPORTER_OTLP_PROTOCOL, so leave it at http/protobuf.

Python

The layer runs opentelemetry-instrument with the Lambda instrumentation, plus instrumentations for requests, aiohttp, boto3sqs, psycopg2, asyncpg, sqlalchemy, pymysql, redis, pymongo, Django, Flask, FastAPI and others found in your deployment package. OTEL_EXPORTER_OTLP_PROTOCOL defaults to http/protobuf in the layer. The default propagators are tracecontext,baggage,xray, so traces continue from upstream services that send traceparent.

Shutdown and flush

Lambda freezes the execution environment as soon as the handler returns, so anything still queued in a batch processor would wait until the next invocation, or be lost when the environment is retired. The Lambda instrumentation in each layer flushes before it hands the response back:

  • Node.js flushes the tracer, meter and logger providers at the end of every invocation.
  • Python flushes the tracer and meter providers, waiting up to 30 seconds. OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT changes the limit, in milliseconds. Log records are not flushed by the instrumentation.

The flush is a network call to Maple, so it adds its time to every invocation’s duration. To take it off the response path, send through the collector layer below.

Send through the collector layer

The OpenTelemetry collector layer runs a collector as a Lambda extension next to your function. The SDK flushes to it on localhost, and the collector’s decouple processor lets the invocation finish while the export to Maple completes before the environment freezes. Add the layer for your function’s architecture:

arn:aws:lambda:<region>:184161586896:layer:opentelemetry-collector-<amd64|arm64>-0_24_0:1

Add a collector.yaml to your deployment package:

receivers:
  otlp:
    protocols:
      http:
        endpoint: "localhost:4318"

processors:
  batch: {}

exporters:
  otlphttp:
    endpoint: "https://ingest.maple.dev" # EU: https://ingest.eu.maple.dev
    headers:
      authorization: "Bearer ${env:MAPLE_INGEST_KEY}"

service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlphttp]
    metrics:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlphttp]
    logs:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlphttp]

The layer adds the decouple processor after batch on its own. Then change the function’s variables: point the SDK at the local collector, and give the collector the key:

OPENTELEMETRY_COLLECTOR_CONFIG_URI="/var/task/collector.yaml"
OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318"
MAPLE_INGEST_KEY="YOUR_INGEST_KEY"

Remove OTEL_EXPORTER_OTLP_HEADERS: the collector adds the header.

Cold starts

The layer loads the SDK and its instrumentations before your handler’s first invocation, which adds to every cold start. Warm invocations pay only for the spans and the flush. To keep cold starts short:

  • On Node.js, list only the instrumentations you use in OTEL_NODE_ENABLED_INSTRUMENTATIONS.
  • Give the function more memory. Lambda allocates CPU in proportion to memory, so startup runs faster.
  • Use provisioned concurrency for latency-sensitive functions.

Sampling

The layers record every invocation by default. To keep a share of new traces, set the standard sampler variables on the function:

OTEL_TRACES_SAMPLER="parentbased_traceidratio"
OTEL_TRACES_SAMPLER_ARG="0.1" # keep 10% of new traces

parentbased_traceidratio follows the decision of the caller for invocations that arrive with a traceparent, so a trace is kept or dropped as a whole across services. Maple can only weight sampled spans back up to the traffic you served when the sample rate reaches it, as a th value in tracestate or a SampleRate span attribute. Read Sampling and throughput before you turn sampling on.

Custom spans

The layer registers the global tracer provider, so spans you create with the OpenTelemetry API nest under the invocation span. Add @opentelemetry/api (Node.js) or opentelemetry-api (Python) to your function’s dependencies, then follow the custom spans section of the Node.js or Python guide. Do not start a second SDK in your handler code.

Verify

  1. Invoke the function a few times, from the console’s Test tab or with aws lambda invoke.
  2. In Maple, open Explore → Traces and filter by your service name.
  3. Each invocation should show up as one trace with a root span for the invocation and child spans for the AWS SDK calls, HTTP requests and queries it made. A function behind API Gateway or a function URL continues the caller’s trace when the request carries traceparent.

Your service also appears on the Services page once its first spans arrive.

Troubleshooting

  • No spans and no errors. Check that AWS_LAMBDA_EXEC_WRAPPER is /opt/otel-handler, that the layer matches the runtime (the Node.js layer does nothing for a Python function), and that the layer ARN uses the function’s region.
  • 401 responses. The key is wrong, was copied from the other region, or the header is malformed. In OTEL_EXPORTER_OTLP_HEADERS it is written Authorization=Bearer%20YOUR_INGEST_KEY. See Ingest API status codes.
  • Timeouts. A function in a VPC without a NAT gateway cannot reach ingest.maple.dev, and the flush waits until it gives up. Give the function outbound internet access, or send through the collector layer from a subnet that has it.
  • Spans arrive late or are missing. A function that times out or crashes never reaches the end-of-invocation flush, so that invocation’s spans are lost. Raise the function timeout above your handler’s worst case.
  • Debugging. On Node.js, set OTEL_LOG_LEVEL=debug to print the SDK’s export errors to CloudWatch Logs. The collector layer also writes its log lines there.

Next steps