DEV Community

Cover image for Don't log it… Just Trace it! OpenTelemetry in AWS Lambda - Part 2
Jaya Ganesh
Jaya Ganesh

Posted on

Don't log it… Just Trace it! OpenTelemetry in AWS Lambda - Part 2

In the previous article, we were introduced to OpenTelemetry and AWS Distro for OpenTelemetry. In this article, we will set up OpenTelemetry in AWS Lambda for Python and observe it in Honeycomb, an observability tool.

ADOT arch

Prerequisites:

  • AWS Lambda: A serverless Function as a service provided by AWS where we can execute our application code without thinking about Servers
  • Amazon API Gateway: A fully managed API solution that lets us create and publish REST and HTTP APIs.
  • ADOT Lambda Layer: An AWS-supported distribution of the OpenTelemetry project that includes SDKs, agents, and collectors.

Setting up AWS Lambda Function and API Gateway:

  1. Create an AWS Lambda Python Function in the AWS Console.

Image description

  1. Now, let's create an API gateway.

Image description

  1. Let's add a GET method to the API and attach the Lambda which was created in step 1.

Image description

  1. Now, deploy it and test it from a browser.

Image description

We have got the response from Lambda. We can go to the AWS Lambda console and look into the Logs. We can see the event from the API Gateway from the print statement in the logs.

Image description

Setting up ADOT in AWS Lambda

We have successfully created an API Endpoint and saw the logs in CloudWatch. Now let us set up AWS Distro for OpenTelemetry on this lambda and export the telemetry data to Honeycomb.

  1. Add AWS Lambda Layer:

    You can get the latest Lambda Layer ARN from ADOT documentation and add attached it to our lambda function.

    Lambda Layer format (arn:aws:lambda::901920570463:layer:aws-otel-python--ver-1-29-0:1)

Image description

  1. Configure Lambda Environment Variables:

    Add the below environment variables to our Lambda function

    AWS_LAMBDA_EXEC_WRAPPER /opt/otel-instrument
    OTEL_PROPAGATORS tracecontext,xray
    OTEL_SERVICE_NAME learn-adot
  2. Create a Custom OTel Collector config file with below data and replace your Honeycomb API Key.

#collector.yaml in the root directory
#Set an environemnt variable 'OPENTELEMETRY_COLLECTOR_CONFIG_FILE' to '/var/task/collector.yaml'
receivers:
  otlp:
    protocols:
      grpc: # port 4317
      http: # port 4318

exporters:
  # Data sources: traces, metrics, logs
  # NOTE: Prior to v0.86.0 use `logging` instead of `debug`
  # logging:
  #   verbosity: detailed
  otlp:
    endpoint: "api.honeycomb.io:443"
    headers:
      "x-honeycomb-team": <HONEYCOMB API KEY>

service:
  pipelines:
    logs:
      receivers: [otlp]
      exporters: [otlp]
    traces:
      receivers: [otlp]
      exporters: [otlp]
    metrics:
      receivers: [otlp]
      exporters: [otlp]
Enter fullscreen mode Exit fullscreen mode

You can get a Honeycomb API Key using steps below:

  1. Click on Manage Data
  2. Select environments
  3. And click API Keys and generate an Ingest Key.

Image description

  1. We can add this config YAML file to the AWS Lambda code directly. In production scenarios, we can upload it to an S3 Bucket and add its URI to the Lambda environment variable. For this, we need to add get S3 Permission to the Lambda.

    Add below environment variable to Lambda:

    OPENTELEMETRY_COLLECTOR_CONFIG_URI: s3://.s3..amazonaws.com/collector_config.yaml

    Now our AWS Lambda environment variables look like this.

Image description

  1. Now Let's invoke the same API again and see the Magic happen.

    We can see a few more logs in our CloudWatch Logs indicating that our collector has started.

Image description

In the Honeycomb console, we can see our incoming requests.

Image description

Click on the graph and select view trace.

Image description

In the Trace, we can see the Span Fields and other meta data.

Image description

Conclusion

In this tutorial, we've successfully implemented basic OpenTelemetry observability in an AWS Lambda function using ADOT, transforming standard CloudWatch logs into rich, context-aware telemetry visible in Honeycomb.

While this setup already provides significant advantages—including end-to-end request visibility, automatic AWS metadata collection, and cross-service tracing capabilities—we've only scratched the surface of what's possible.

In the next article, we'll enhance our observability by adding custom instrumentation with span attributes and events to capture business-relevant context, measure specific operations, and correlate traces across services, enabling you to answer critical questions about your application's performance and behaviour.

Top comments (0)