Docs » Instrument serverless functions for Splunk Observability Cloud » Instrument AWS Lambda functions for Splunk Observability Cloud » Instrument your AWS Lambda function for Splunk Observability Cloud

Instrument your AWS Lambda function for Splunk Observability Cloud 🔗

Use the Splunk OpenTelemetry Lambda Layer to automatically instrument your AWS Lambda functions for many programming languages.

Tip: To generate a template that instruments your Lambda function using the Splunk OpenTelemetry Lambda Layer, use the AWS Lambda guided setup. To access the AWS Lambda guided setup, follow these steps:

  1. Log in to Splunk Observability Cloud

  2. Open the AWS Lambda guided setup. Optionally, you can navigate to the guided setup on your own:

    1. In the left navigation menu, select Data Management to open the Integrate Your Data page.

    2. In the integration filter menu, select All.

    3. In the Search field, search for AWS Lambda.

    4. Select the AWS Lambda tile to open the AWS Lambda guided setup.

Check compatibility and requirements 🔗

The Splunk OpenTelemetry Lambda Layer supports the following runtimes in AWS Lambda:

  • Java 8 and 11

  • Python 3.8 and 3.9

  • Node.js 10, 12, and 14

  • Ruby 2.7

  • Go 1.18

For more information, search for “Lambda runtimes” on the AWS documentation website.

Install the AWS Lambda layer 🔗

Follow these steps to instrument your function using the Splunk OpenTelemetry Lambda Layer:

  1. In the AWS Lambda console, select the function that you want to instrument.

  2. In the Layers section, select Add a layer, then select Specify an ARN.

  3. Copy the Amazon Resource Name (ARN) for the region of your Lambda function from the list matching your architecture:

  4. Paste the selected ARN in the Specify an ARN field and select Add.

  5. Check that the Splunk layer appears in the Layers table.

Note

You can automate the update of the Lambda layer using the AWS CLI. The following command, for example, retrieves the latest version of the Splunk layer for x86_64 and the us-east-1 region:

aws lambda list-layer-versions --layer-name splunk-apm --region us-east-1 --query 'LayerVersions[0].LayerVersionArn'

Configure the Splunk OpenTelemetry Lambda Layer 🔗

Follow these steps to add the required configuration for the Splunk OpenTelemetry Lambda Layer:

  1. In the AWS Lambda console, open the function that you are instrumenting.

  2. Navigate to Configuration > Environment variables, then click Edit.

  3. Add each of the following environment variables by clicking Add environment variable:

    Environment variable

    Description

    SPLUNK_REALM

    To find the realm of your Splunk Observability Cloud account, open the navigation menu in Observability Cloud, select Settings, and select your username. The realm name appears in the Organizations section.

    SPLUNK_ACCESS_TOKEN

    A Splunk authentication token that lets exporters send data directly to Splunk Observability Cloud. See Authentication token. To send data to a Splunk OTel Collector in EC2, see Send serverless spans through the Splunk OpenTelemetry Collector.

    AWS_LAMBDA_EXEC_WRAPPER

    Set the value for the AWS_LAMBDA_EXEC_WRAPPER environment variable:

    # Select the most appropriate value
    
    # Wraps regular handlers that implement RequestHandler
    /opt/otel-handler
    
    # Same as otel-handler, but proxied through API Gateway,
    # with HTTP context propagation enabled
    /opt/otel-proxy-handler
    
    # Wraps streaming handlers that implement RequestStreamHandler
    /opt/otel-stream-handler
    

    Note

    Only AWS SDK v2 instrumentation is enabled by default. To instrument other libraries, modify your code to include the corresponding library instrumentation from the OpenTelemetry Java SDK.

    OTEL_SERVICE_NAME

    The name of your service.

    (Optional) OTEL_RESOURCE_ATTRIBUTES

    Define the name of the deployment environment of your function by setting this environment variable to deployment.environment=<name-of-your-environment>.

  4. Click Save and check that the environment variables appear in the table.

Note

By default, the layer sends telemetry directly to Observability Cloud ingest endpoints. To send data to a Splunk OTel Collector in EC2, see Send serverless spans through the Splunk OpenTelemetry Collector.

Instrument Go functions in AWS Lambda 🔗

To instrument a Go function in AWS Lambda for Splunk APM, follow these additional steps:

  1. Run the following commands to install the otellambda and the Splunk OTel Go distribution:

    go get -u go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda
    go get -u github.com/signalfx/splunk-otel-go/distro
    
  2. Create a wrapper for the OpenTelemetry instrumentation in your function’s code. For example:

    package main
    
    import (
       "context"
       "fmt"
    
       "github.com/aws/aws-lambda-go/lambda"
       "github.com/signalfx/splunk-otel-go/distro"
       "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda"
       "go.opentelemetry.io/otel"
    )
    
    func main() {
       distro.Run()
       flusher := otel.GetTracerProvider().(otellambda.Flusher)
       lambda.Start(otellambda.InstrumentHandler(HandleRequest, otellambda.WithFlusher(flusher)))
    }
    
    type MyEvent struct {
       Name string `json:"name"`
    }
    
    func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
       return fmt.Sprintf("Hello %s!", name.Name), nil
    }
    

Serverless Framework support 🔗

Some features of the Serverless Framework might impact OpenTelemetry tracing of Python Lambda functions.

Python libraries compression 🔗

The zip feature of pythonRequirements allows packing and deploying Lambda dependencies as compressed files. To instrument packages compressed using the Serverless Framework, set the SPLUNK_LAMBDA_SLS_ZIP environment variable to true. For more information, see https://github.com/serverless/serverless-python-requirements#dealing-with-lambdas-size-limitations on GitHub.

Slim feature 🔗

The Slim feature reduces the size of Lambda packages by removing some files, including dist-info folders. Some of the files removed by the Slim feature are required by the OpenTelemetry Python autoinstrumentation. Disable the slim option in your serverless.yml file or define custom slimPatterns. For more information, see https://github.com/serverless/serverless-python-requirements#slim-package on GitHub.

Check that data appears in Splunk Observability Cloud 🔗

Each time the AWS Lambda function runs, trace and metric data appears in Splunk Observability Cloud. If no data appears, see Troubleshoot the Splunk OpenTelemetry Lambda Layer.