You can instrument your Lambda functions by adding the New Relic Lambda layer. This integrates the New Relic agent, enabling automatic monitoring whenever your functions are invoked.
Tip
New Relic recommends integrating your AWS account with New Relic using the AWS integration to automatically discover and monitor your Lambda functions. This allows you to leverage the full power of New Relic APM for your serverless functions.
Instrumentation methods
New Relic offers several methods to instrument your AWS Lambda functions for comprehensive monitoring:
Command Line Interface (CLI): Use the AWS CLI to quickly add the New Relic layer to your Lambda functions.
Serverless Framework: Seamlessly integrate New Relic instrumentation into your serverless deployments.
CloudFormation/SAM: Include the New Relic layer in your infrastructure-as-code templates.
AWS CDK: Add New Relic Lambda layer in your CDK code alongside other infrastructure resources.
Terraform: Easily manage New Relic instrumentation alongside your other infrastructure resources.
Manual Instrumentation: Directly add the New Relic layer through the AWS Lambda console for more granular control.
Regardless of the method you choose, the New Relic layer adds the New Relic agent to your functions. This agent automatically instruments your functions upon invocation, generating a payload, NR_LAMBDA_MONITORING, that is sent to New Relic via the New Relic Lambda extension.
Depending on your needs, you can choose to either bypass the extension and only see telemetry in CloudWatch, bypass CloudWatch, or use CloudWatch as a fallback. The CloudWatch section at end of this doc will guide you through each option.
You can instrument your Lambda function with the newrelic-lambda CLI quickstart.
To install or upgrade the New Relic instrumentation layer, run this command:
This command automatically finds the newest available layer for your Lambda's region and runtime.
This command provides a rapid way to initiate New Relic instrumentation and can be easily integrated into your CI/CD pipeline. However, since it modifies existing Lambda function resources, redeployment of updated code may inadvertently remove the instrumentation. To ensure continuous monitoring, either re-run the command after each deployment or, preferably, incorporate the New Relic layer and configuration directly into your deployment process.
Note that the CLI can operate on many functions in a batch: use --function all, --function installed, or
--function not-installed to operate on all functions in a region, or only those with or without existing
New Relic instrumentation.
AWS's Serverless Application Model, or SAM is a variant of CloudFormation templates that simplifies relating functions to the resources they depend on, and managing the lifecycle of an entire application. We use SAM and CloudFormation for most of our Lambda example functions, and many other tools are built on top of CloudFormation templates, providing an addition layer of abstraction.
CloudFormation is an AWS service that simplifies the provisioning and management of AWS resources. By defining the desired state of resources in YAML or JSON templates, CloudFormation automatically handles the underlying API calls to create, update, or delete resources as needed. This declarative approach automates infrastructure management, ensuring consistency and reproducibility.
To monitor your Lambda function with New Relic:
Install the New Relic Lambda layer. Here's an example of a simple CloudFormation template for a Node.js Lambda function:
AWSTemplateFormatVersion:'2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: And example of a simple instrumented Node.js Lambda
Resources:
NewRelicExample:
Type: AWS::Serverless::Function
Properties:
# In this example, we're using the SAM CLI to package and deploy our lambda. SAM will transform this value during the publish step.
CodeUri: newrelic-example-node/
# The handler for your function needs to be the one provided by the instrumentation layer, below.
Handler: newrelic-lambda-wrapper.handler
Runtime: nodejs22.x
Environment:
Variables:
# For the instrumentation handler to invoke your real handler, we need this value
Deploy your Lambda function with the updated configuration.
Serverless Framework is a popular development and deployment tool for serverless applications. It's written for AWS in Node.js, and acts mostly as a high level abstraction on top of CloudFormation templates. It works well for Node, Python, Ruby, Java, and .NET functions.
New Relic provides a Serverless Framework Plugin to simplify instrumentation of your Serverless Framework application.
Prerequisites:
An existing application that uses the Serverless Framework.
Deploy your Lambda function with the updated configuration.
Terraform is a popular general-purpose infrastructure as code tool. It can be used to manage AWS resources. We offer some examples of New Relic instrumented Lambda functions deployed using Terraform scripts.
You can use the New Relic Terraform provider to set up your AWS integrations.
Upon setting up the provider, configure the required environment variables, and add the NR.Apm.Lambda.Mode: true tag to your Lambda function.
While it is more error prone and labor intensive than the approaches above, you can manually alter the configuration of a Lambda function to use New Relic from the AWS Lambda Console, for Node.js, Python, Ruby and Java.
Here's an example for how to instrument New Relic Lambda monitoring for a Ruby runtime:
Navigate to the Lambda service section in the AWS web console. From there, find the Lambda function you would like to connect to New Relic.
In the default Code tab, scroll down to the Layers section, and click on the Add a layer button.
Click the Create layer button.
Go to the Choose a layer and select the Specify an ARN option.
Go to New Relic's list of layers and use the drop-down list to select the AWS region where your Lambda function is hosted. From there, locate the ARN that matches your Lambda function's Ruby version and architecture. There should be two options: X86 and ARM64. Use the Copy to clipboard button or manually copy the ARN string.
In the Specify an ARN section of the AWS console form, paste in the New Relic Lambda layer ARN.
On the AWS console form, click the Add button to add the layer to your Lambda function.
On your Lambda function's page, with the default Code tab selected, scroll down to the Runtime settings section and click the Edit button.
Make a safe copy of the existing Handler value. You'll need it for a later step.
Change the Handler value to: newrelic_lambda_wrapper.handler and click Save.
Switch to the Configuration tab on your Lambda function's page.
Select the Environment variables sub-tab and based on your requirement, define the Environmental Variables.
Modify the Execution Role to allow access to the New Relic license key secret.
Find the ARN of the secret named NEW_RELIC_LICENSE_KEY.
Add a new inline policy in the function's execution role that looks like this the code below. Replace the SECRET_ARN with the value you found above.
"Statement":[
{
"Action":[
"secretsmanager:GetSecretValue"
],
"Resource":"SECRET_ARN",
"Effect":"Allow"
}
]
Tip
The New Relic layer will automatically deliver the New Relic Ruby agent and be loaded via a Ruby require prior to your Lambda function's invocation. To avoid conflicts, don't include a copy of the Ruby agent anywhere else. Feel free to perform any desired New Relic Ruby agent API calls within your function to take advantage of the agent's presence.
In your AWS Management Console, on the Configuration tab, add the NR.Apm.Lambda.Mode: true tag to your Lambda function.
Deploy your Lambda function with the updated configuration.
Java, Python, Node.js, and .Net runtimes will follow the same instrumentation flow as Ruby, but with some different handlers. Here's how to update your function's handler to point to the newly attached layer in the console for your function:
Note that for Go, you must make source code changes to your Lambda function to instrument it. Configuration changes are not enough.
Find and use data
After you instrument your AWS Lambda functions, you can find and use the data in the New Relic APM interface. The data is organized into several key areas, each providing different insights into the performance and health of your Lambda functions.
In the APM interface, you can explore various aspects of your Lambda functions including distributed tracing, service maps, transactions, and error analysis, and many more. Each of these areas provides detailed insights into the performance, latency, and error rates of your Lambda functions, allowing you to quickly identify and resolve issues.
Tip
The Invocation experience is now integrated with APM transaction traces. This allows you to use a specific AWS RequestId to drill-down into an APM transaction trace for detailed information about that particular Lambda execution.
To view your Lambda functions in the New Relic APM interface: