> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trace0hq.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js — AWS Lambda

> Instrument a Node.js Lambda function with OpenTelemetry and send traces, logs and metrics to Trace0.

<Note>
  You only need to add the **Node.js Lambda layer** — no separate collector layer is required. Trace0 handles ingestion directly.
</Note>

## 1. Add the OpenTelemetry Lambda Layer

Find the latest `layer-nodejs` ARN for your AWS region from the [opentelemetry-lambda releases page](https://github.com/open-telemetry/opentelemetry-lambda/releases). Add the layer ARN to your function.

## 2. Set Environment Variables

| Variable                      | Value                               |
| ----------------------------- | ----------------------------------- |
| `AWS_LAMBDA_EXEC_WRAPPER`     | `/opt/otel-handler`                 |
| `OTEL_EXPORTER_OTLP_ENDPOINT` | `https://app.trace0hq.com/api`      |
| `OTEL_EXPORTER_OTLP_HEADERS`  | `X-API-KEY=YOUR_TRACE0_ENV_API_KEY` |

## 3. Set HTTP request spans

For lambdas triggered via API Gateway, the HTTP request and response attributes are not set automatically on the lambda invocation span by the OTel Lambda auto-instrumentation. Therefore, they need to be set manually in your lambda code handler.

```typescript theme={null}
import { trace, SpanStatusCode } from '@opentelemetry/api';

export const handler = async (
  event: APIGatewayProxyEvent,
  context: Context
): Promise<APIGatewayProxyResult> => {
  const { httpMethod, path, requestContext } = event;

  //Set the http request method and route on the parent span.
  const span = trace.getActiveSpan();
  span?.setAttributes({
    'http.request.method': event.httpMethod,
    'http.route': event.path
  });

  const result = await processEvent(event)

  // Set the HTTP response code and mark the span as failed for 4xx/5xx responses.
  span?.setAttributes({ 'http.response.status_code': result.statusCode });
  span?.setStatus({ code: result.statusCode < 400 ? SpanStatusCode.OK : SpanStatusCode.ERROR });

  return result;
}
```

## 4. Add Trace0 logger

To correlate logs with traces in Trace0, the [@trace0/lambda-otel-logger](https://www.npmjs.com/package/@trace0/lambda-otel-logger) library must be installed in your Lambda function. This is necessary because the OpenTelemetry JavaScript library does not yet automatically bridge console output into the OTel Logs pipeline.

First, install the library:

```json theme={null}
"dependencies": {
    "@opentelemetry/api": "^1.9.1",
    "@trace0/lambda-otel-logger": "^1.0.3"
}
```

Then add as the first import in your Lambda handler entry point, and call `flush()` at the end of every invocation:

```typescript theme={null}
import '@trace0/lambda-otel-logger';
import { flush } from '@trace0/lambda-otel-logger';

export const handler = async (event: APIGatewayProxyEvent, context: Context) => {
  try {
    console.log(`Processing request with requestId: ${context.awsRequestId}`);
    return await processEvent(event, context);
  } finally {
    await flush(); // always flush before Lambda freezes
  }
};
```

The library will now automatically capture all logging output, inject OTel trace context (traceId, spanId), and export the logs to the Trace0 ingest endpoint.

## 5. Deploy service

Deploy your service and watch traces, metrics, and logs appear in the [Trace0 dashboard](https://app.trace0hq.com) within seconds.

## Example service

See our [Node.js Lambda example app](https://github.com/Trace0-HQ/trace0-examples/tree/main/node-js-lambda) for a working service you can deploy to your AWS account.
