Setting up tracing for Ruby Lambda functions

Enable Instana AWS Lambda tracing to monitor performance, detect issues, and gain visibility into Ruby-based AWS Lambda executions.

For more information, see AWS Lambda tracing.

Supported runtimes

  • Ruby 3.3
  • Ruby 3.2
  • Ruby 3.1

Starting from 7 January 2024, AWS Lambda no longer supports the Ruby 2.7 runtime.

Prerequisites

Set up the AWS agent for Lambda monitoring to help ensure the collection of necessary information about versions and some runtime metrics that Instana cannot collect from inside the AWS Lambda runtime.

Enabling tracing for Ruby Lambda functions

You can enable tracing of Lambda functions through the AWS web console or any of the following AWS management tools:

Enabling tracing by using AWS web console

To enable tracing of Ruby Lambda functions through the AWS web console, complete the following steps:

  1. Add the Instana Lambda layer to your function:

    1. On the configuration page of your Lambda function, click Layers.

      Lambda Layers
      Figure 1. Lambda Layers

    2. In the Layers box, click Add a layer.

      Add a layer
      Figure 2. Add a layer

    3. On the Add layer page, select the Specify an ARN option as the layer source.

    4. In the Specify an ARN field, copy and paste the ARN for the Instana Lambda Layer that matches your region. You can find the right ARN for the Instana Lambda Layer in the Instana Lambda Layers section.

    5. Click Add.

      Add Instana Ruby Layer
      Figure 3. Add Instana Ruby Layer

  2. Instrument the handler function:

    Wrap your Ruby lambda function in a Instana.serverless.wrap_aws block, passing the event and context as arguments as shown in the following example. This wrapper automatically creates spans for your Lambda invocation and handles the tracing context.

    require 'bundler/setup'
    
    require 'json'
    require 'instana'
    
    def lambda_handler(event:, context:)
      Instana.serverless.wrap_aws(event, context) do
        # Your original Lambda function code goes here
        {
          statusCode: 200,
          body: {
            message: "Hello World!",
          }.to_json
        }
      end
    end
    

    With the wrapper approach, you can maintain your existing handler function name while adding Instana tracing capabilities. You don't need to change your Lambda handler configuration in the AWS console.

  3. Set environment variables:

    Add the following environment variables:

    • INSTANA_ENDPOINT_URL: Your serverless monitoring endpoint. Make sure to use the correct value for your region that starts with https://serverless-.
    • INSTANA_AGENT_KEY: Your agent key.
    • LAMBDA_HANDLER: Your custom handler, if you are not using the default handler lambda_function.lambda_handler.

    You can also obtain the correct values for these environment variables from your Instana UI. To obtain the correct values for these environment variables, click Data sources > Install agents > AWS Lambda.

Enabling tracing by using AWS CLI

The following example shows an AWS CLI command that might serve as a starting point if you want to automate the Instana integration of your AWS Lambda functions:

# Do not copy and paste this verbatim!
# It will overwrite any previously defined collection of layers and
# environment variables.
aws --region $YOUR_REGION lambda update-function-configuration \
  --function-name $YOUR_LAMBDA_FUNCTION_NAME \
  --layers $INSTANA_LAYER_ARN \
  --environment ""Variables={INSTANA_ENDPOINT_URL=... , INSTANA_AGENT_KEY=... , LAMBDA_HANDLER=...}""

Unlike some other language integrations, you don't need to modify the handler configuration when you use the Ruby integration. The Ruby integration uses a wrapper approach that preserves your original handler function.

Troubleshooting

If you encounter issues with the Ruby Lambda integration, troubleshoot as follows:

  • Verify that the correct layer ARN is being used for your region.
  • Ensure that all required environment variables are properly set.
  • Check CloudWatch logs for any error messages that are related to the Instana integration.
  • Verify that your Lambda function has internet access to reach the Instana backend.

For additional help, consult the AWS Lambda troubleshooting guide or contact Instana support.

Instana Lambda layers

The ARNs of the latest version of the AWS Lambda layers for Ruby are the following, per region:

Table 1. Instana Lambda layers
Region ARN
af-south-1 arn:aws:lambda:af-south-1:410797082306:layer:instana-ruby:1
ap-east-1 arn:aws:lambda:ap-east-1:410797082306:layer:instana-ruby:1
ap-northeast-1 arn:aws:lambda:ap-northeast-1:410797082306:layer:instana-ruby:1
ap-northeast-2 arn:aws:lambda:ap-northeast-2:410797082306:layer:instana-ruby:1
ap-northeast-3 arn:aws:lambda:ap-northeast-3:410797082306:layer:instana-ruby:1
ap-south-1 arn:aws:lambda:ap-south-1:410797082306:layer:instana-ruby:1
ap-south-2 arn:aws:lambda:ap-south-2:410797082306:layer:instana-ruby:1
ap-southeast-1 arn:aws:lambda:ap-southeast-1:410797082306:layer:instana-ruby:1
ap-southeast-2 arn:aws:lambda:ap-southeast-2:410797082306:layer:instana-ruby:1
ap-southeast-3 arn:aws:lambda:ap-southeast-3:410797082306:layer:instana-ruby:1
ap-southeast-4 arn:aws:lambda:ap-southeast-4:410797082306:layer:instana-ruby:1
ap-southeast-5 arn:aws:lambda:ap-southeast-5:410797082306:layer:instana-ruby:1
ap-southeast-7 arn:aws:lambda:ap-southeast-7:410797082306:layer:instana-ruby:1
ca-central-1 arn:aws:lambda:ca-central-1:410797082306:layer:instana-ruby:1
ca-west-1 arn:aws:lambda:ca-west-1:410797082306:layer:instana-ruby:1
cn-north-1 arn:aws-cn:lambda:cn-north-1:107998019096:layer:instana-ruby:1
cn-northwest-1 arn:aws-cn:lambda:cn-northwest-1:107998019096:layer:instana-ruby:1
eu-central-1 arn:aws:lambda:eu-central-1:410797082306:layer:instana-ruby:1
eu-central-2 arn:aws:lambda:eu-central-2:410797082306:layer:instana-ruby:1
eu-north-1 arn:aws:lambda:eu-north-1:410797082306:layer:instana-ruby:1
eu-south-1 arn:aws:lambda:eu-south-1:410797082306:layer:instana-ruby:1
eu-south-2 arn:aws:lambda:eu-south-2:410797082306:layer:instana-ruby:1
eu-west-1 arn:aws:lambda:eu-west-1:410797082306:layer:instana-ruby:1
eu-west-2 arn:aws:lambda:eu-west-2:410797082306:layer:instana-ruby:1
eu-west-3 arn:aws:lambda:eu-west-3:410797082306:layer:instana-ruby:1
il-central-1 arn:aws:lambda:il-central-1:410797082306:layer:instana-ruby:1
me-central-1 arn:aws:lambda:me-central-1:410797082306:layer:instana-ruby:1
me-south-1 arn:aws:lambda:me-south-1:410797082306:layer:instana-ruby:1
sa-east-1 arn:aws:lambda:sa-east-1:410797082306:layer:instana-ruby:1
us-east-1 arn:aws:lambda:us-east-1:410797082306:layer:instana-ruby:1
us-east-2 arn:aws:lambda:us-east-2:410797082306:layer:instana-ruby:1
us-west-1 arn:aws:lambda:us-west-1:410797082306:layer:instana-ruby:1
us-west-2 arn:aws:lambda:us-west-2:410797082306:layer:instana-ruby:1

The pattern for Chinese regions is arn:aws-cn:lambda:${region}:107998019096:layer:instana-ruby:${layer-version} and for the rest of the regions is arn:aws:lambda:${region}:410797082306:layer:instana-ruby:${layer-version}.

Make sure to always use the latest versions and update the layer version that you are using at regular intervals to benefit from new features and fixes provided the new version of the layer.