OpenTelemetry AWS lambda Instrumentation

The opentelemetry-instrumentation-aws-lambda package provides an Instrumentor to traces calls within a Python AWS Lambda function.


# Copy this snippet into an AWS Lambda function

import boto3
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor

# Enable instrumentation

# Lambda function
def lambda_handler(event, context):
    s3 = boto3.resource('s3')
    for bucket in s3.buckets.all():

    return "200 OK"


The instrument method accepts the following keyword args:

tracer_provider (TracerProvider) - an optional tracer provider meter_provider (MeterProvider) - an optional meter provider event_context_extractor (Callable) - a function that returns an OTel Trace Context given the Lambda Event the AWS Lambda was invoked with this function signature is: def event_context_extractor(lambda_event: Any) -> Context for example:

from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor

def custom_event_context_extractor(lambda_event):
    # If the `TraceContextTextMapPropagator` is the global propagator, we
    # can use it to parse out the context from the HTTP Headers.
    return get_global_textmap().extract(lambda_event["foo"]["headers"])


class opentelemetry.instrumentation.aws_lambda.AwsLambdaInstrumentor(*args, **kwargs)[source]

Bases: BaseInstrumentor


Return a list of python packages with versions that the will be instrumented.

The format should be the same as used in requirements.txt or pyproject.toml.

For example, if an instrumentation instruments requests 1.x, this method should look like: :rtype: Collection[str]

def instrumentation_dependencies(self) -> Collection[str]:

return [‘requests ~= 1.0’]

This will ensure that the instrumentation will only be used when the specified library is present in the environment.