New Relic Lambda Layer Setup and Instrumentation Guide

Author

Reads 785

Computer server in data center room
Credit: pexels.com, Computer server in data center room

To set up a New Relic Lambda layer, you'll need to create a zip file containing the required libraries and configuration files. This zip file will be uploaded to AWS Lambda as a layer.

New Relic Lambda layers can be created with or without a SAM template, depending on your project's requirements. If you're using a SAM template, you'll need to add the New Relic Lambda layer to the template.

To add New Relic to your AWS Lambda function, you'll need to include the New Relic Lambda layer in the function's configuration. This can be done by adding the layer to the function's code or by using a SAM template to define the layer.

New Relic Lambda layers can be used to instrument AWS Lambda functions, allowing you to monitor and troubleshoot your serverless applications.

New Relic Setup

To set up New Relic, you'll need to link your AWS and New Relic accounts. This can be done using the newrelic-lambda command.

Credit: youtube.com, New Relic Lambda Layer and CLI installer for no-code instrumentation of serverless monitoring

The newrelic-lambda command is recommended by New Relic itself, and it's a one-time setup process per AWS account, so you won't need to do it every time you make changes to your AWS resources.

If you're using a non-default AWS profile, you'll need to set the environment variable AWS_PROFILE to your profile name, and then run the command with your New Relic account ID and API key.

The command to run is: AWS_PROFILE=YOUR_AWS_PROFILE newrelic-lambda integrations install --nr-account-id YOUR_NR_ACCOUNT_ID --nr-api-key YOUR_NEW_RELIC_USER_KEY.

Discover more: York Times Account

Instrumentation

Instrumentation is a crucial step in getting the most out of your New Relic Lambda Layer. You can instrument your Lambda function using the newrelic-lambda-cli tool, but manual configuration is also an option.

To manually configure your function, you'll need to find the New Relic AWS Lambda Layer ARN that matches your runtime and region. This ARN will then need to be attached to your function.

To attach the layer, you'll need to update your function's handler to point to the newly attached layer. For Python, Node, and Ruby, this means setting the handler to newrelic_lambda_wrapper.handler, while for Java, the handler should be updated accordingly. For .NET, this step is not required.

Here are the environment variables you'll need to add to your Lambda console:

Instrumentation Methods

Man in Black Hoodie Sitting on Chair Looking at Computer Monitor
Credit: pexels.com, Man in Black Hoodie Sitting on Chair Looking at Computer Monitor

Manual instrumentation using layers is a recommended approach for configuring functions. You can use the newrelic-lambda-cli tool, but some users prefer to do it manually.

To manually configure layers correctly, find the New Relic AWS Lambda Layer ARN that matches your runtime and region. Copy the ARN of the most recent AWS Lambda Layer version and attach it to your function.

You can add your layer ARN to the Layers property of an AWS::Lambda::Function resource using Cloudformation. Update your function's handler to point to the newly attached layer in the console.

For Python, Node, and Ruby handlers, update the handler to newrelic_lambda_wrapper.handler. For Java, the handler path is not specified, but for .NET, it's not required to update the handler.

To add environment variables to your Lambda console, you'll need to set the following:

  • NEW_RELIC_ACCOUNT_ID: Your New Relic account ID
  • NEW_RELIC_LAMBDA_HANDLER: Path to your initial handler
  • NEW_RELIC_USE_ESM: For Node.js handlers using ES Modules, set to true
  • CORECLR_ENABLE_PROFILING (.NET only): 1
  • CORECLR_PROFILER (.NET only): {36032161-FFC0-4B61-B559-F6C5D41BAE5A}
  • CORECLR_NEWRELIC_HOME (.NET only): /opt/lib/newrelic-dotnet-agent
  • CORECLR_PROFILER_PATH (.NET only): /opt/lib/newrelic-dotnet-agent/libNewRelicProfiler.so

You can also instrument Lambda using the Layer-based Instrumentation method, which involves adding a layer to your function before configuring CloudWatch to send logs to New Relic.

Equivalent Container Function

You can use New Relic's Lambda Layer equivalent functionality with a Container Image, but only if your Lambda function is deployed as a .zip file archive. This isn't an option for me, as my Lambda is deployed in a Container format.

Computer server in data center room
Credit: pexels.com, Computer server in data center room

New Relic's Lambda Layer is built from the GitHub repository newrelic/newrelic-lambda-layers. The README file mentions that running the extension/publish-layer.sh script makes the layer publicly available. This script points to a zip file, newrelic-lambda-extension.x86_64.zip, which is downloaded from the newrelic/newrelic-lambda-extension repository.

To replicate this in our Container Image, we need to copy the contents of the zip file to the /opt directory. The Dockerfile for a Go-language-enabled Container Image has a build stage called newrelic-lambda-layer, which unpacks the zip file and places it in the /opt directory.

ES Module Support

ES Module support is available in Node.js, thanks to AWS's announcement in late 2022.

To leverage import and top-level await in your functions, you'll need to use Lambda Layer releases v9.8.1.1 and above, which vary by region and runtime.

Adding the environment variable NEW_RELIC_USE_ESM: true will configure the layer to work with import.

However, if you use layer-installed instrumentation with this variable, your function must use promises or async/await, as callback-based functions are not supported.

Credit: youtube.com, EcmaScript Modules in NodeJS // Upgrade your Projects to ES Modules

You can still support callback-based functions by removing the NEW_RELIC_USE_ESM environment variable and using the CommonJS-based wrapper.

If you're using Node 16 or Node 14 runtimes, layer-installed instrumentation will not work with import and top-level await, so you'll need to instrument your function manually using the Node Agent.

Here are the steps to follow:

  1. Instrument your function manually using our Node Agent.
  2. Deploy your function without setting the function handler to our Node wrapper.
  3. Use your regular handler function, which you've wrapped with newrelic.setLambdaHandler().
  4. For Node 18 or above, apply the latest Lambda Layer for your runtime.
  5. For Node 14 or Node 16, deploy our agent with your function code, or use our Extension-only Lambda Layer for delivering telemetry.
  6. Add your NEW_RELIC_LICENSE_KEY as an environment variable.

Custom Layers

Custom layers are a powerful feature of New Relic Lambda Layers. You can deploy your own copies of the layers into your account or modify and publish your own customized wrapper layers.

To use custom layers, you'll need to import the New Relic Node agent into your handler file, just like it's done in the New Relic Lambda Layers repository. This is a straightforward step that gets you started with customizing your layers.

New Relic Serverless APM customers can use the newrelic-lambda-cli tool with custom layers by adding the --layer-arn flag to the layers install command. This is a handy way to attach custom layers to your account.

If you want to specify the use of layers by ARN, you can do so directly within SAM, Cloudformation Templates, Serverless.yml, or other configuration methods. This gives you a lot of flexibility in how you manage your layers.

Logging and Monitoring

Credit: youtube.com, Lambda Monitoring and Logging #29 #HowTo

Logging and Monitoring is a crucial part of setting up a New Relic Lambda Layer. To start, you need to choose your environment and runtime for the Lambda you want to monitor, then select CloudWatch Logs as the logging service.

Choose your New Relic account and AWS account, and make sure you have an Nerd Graph API key filled up or generate a new one. This will allow you to integrate New Relic with your AWS account.

You'll also need to install the New Relic CLI tool and run a command to set up the integration between New Relic and your AWS account. If you've never done this before, you'll need to run the command; otherwise, you can skip it.

To complete the setup, invoke the Lambda function at least once to create a CloudWatch log group. This is necessary for the integration to work.

If you want to configure additional logging settings, you can do so by setting environment variables for the NewRelicLogIngestion function. Specifically, you can enable DebugLogging and NRLogging by setting their values to True.

On a similar theme: New Relic Slack Integration

Credit: youtube.com, New Relic Logs: Faster, Easier, and Scalable

You can also configure the number of retries for log ingestion by setting the MAX_RETRIES, INITIAL_BACKOFF, and BACKOFF_MULTIPLIER environment variables. This will determine how many times the function tries to send data in case of communication issues.

Here's a quick rundown of the retry settings:

By configuring these settings, you can fine-tune your log ingestion process and ensure that your New Relic Lambda Layer is working as expected.

Instrumentation Methods

Manual Instrumentation is a viable option for some users, especially those who prefer a hands-on approach. It involves configuring layers correctly to ensure seamless integration with New Relic.

To configure layers manually, find the New Relic AWS Lambda Layer ARN that matches your runtime and region. You can then copy the ARN of the most recent AWS Lambda Layer version and attach it to your function.

You can use Cloudformation to add your layer ARN to the Layers property of an AWS::Lambda::Function resource. This is a crucial step in the manual instrumentation process.

For your interest: Data Lake Layers

Credit: youtube.com, Guided install: Instrumentation made simple

Update your function's handler to point to the newly attached layer in the console. For Python, Node, and Ruby, the handler should be updated to newrelic_lambda_wrapper.handler. Java and .NET require different handler updates.

Here's a quick rundown of the required handler updates for each language:

Add these environment variables to your Lambda console to complete the manual instrumentation process. These variables include NEW_RELIC_ACCOUNT_ID, NEW_RELIC_LAMBDA_HANDLER, and others specific to .NET.

CDK and Secret Manager

To use the SecretManager created by the newrelic-lambda command in cdk, you'll need to add a description to cdk. The newrelic-lambda integrations install setup stores the account ID and license key in AWS Secrets Manager as a resource named NEW_RELIC_LICENSE_KEY.

You'll need to add cdk code to retrieve this resource and grant Lambda permission to read its value. This involves adding a cdk description to get the resource and a description to grant the necessary permissions.

Thomas Goodwin

Lead Writer

Thomas Goodwin is a seasoned writer with a passion for exploring the intersection of technology and business. With a keen eye for detail and a knack for simplifying complex concepts, he has established himself as a trusted voice in the tech industry. Thomas's writing portfolio spans a range of topics, including Azure Virtual Desktop and Cloud Computing Costs.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.