On this page, you will learn how to manually instrument your lambda function. It's organized by runtime language. If you haven't already, create your free New Relic account below to start monitoring your data today.
To instrument your Go-language Lambda:
Download our Go agent package, and place it in the same directory as your function.
- Install the agent by running:bash$go get -u github.com/newrelic/go-agent/v3/newrelic
- Install the
nrlambda
integration by running:bash$go get -u github.com/newrelic/go-agent/v3/integrations/nrlambda
- Install the agent by running:
In your Lambda code, import our components, create an application, and update how you start your Lambda. See our instrumentation examples:
Optionally, add custom events that will be associated with your Lambda invocation by using the
RecordCustomEvent
API. For example:func handler(ctx context.Context) {if txn := newrelic.FromContext(ctx); nil != txn {txn.Application().RecordCustomEvent("MyEvent", map[string]interface{}{"zip": "zap",})}fmt.Println("hello world!")}Build and zip your Lambda function and upload it to AWS.
Here are suggestions for zipping and uploading the Lambda:
Build the binary for execution on Linux. This produces a binary file called
main
. You can use:bash$GOOS=linux go build -o mainZip the binary into a deployment package using:
bash$zip deployment.zip mainUpload the zip file to AWS using either the AWS Lambda console or the AWS CLI. Name the handler
main
(to match the name given during the binary build).
The following environment variables are not required for Lambda monitoring to function, but they are required if you want your Lambda functions to be included in distributed traces. To enable distributed tracing, set these environment variables in the AWS console:
NEW_RELIC_ACCOUNT_ID
. Your account ID.NEW_RELIC_TRUSTED_ACCOUNT_KEY.
This is also your account ID. If your account is a child account, this is the account ID for the root/parent account.
Optionally, to configure logging, see Go agent logging.
Invoke the Lambda at least once. This creates a CloudWatch log group, which must be present for the next step to work.
Our wrapper gathers data about the Lambda execution, generates a JSON message, and logs it to CloudWatch Logs. Next, you'll configure CloudWatch to send those logs to New Relic.
Monitoring for AWS Lambda in Java doesn't use our APM Java agent. Instead, it uses these two OpenTracing dependencies:
AWS Lambda OpenTracing Java SDK: OpenTracing instrumentation for AWS Lambda RequestHandler and RequestStreamHandler.
Our AWS Lambda OpenTracing Tracer: An OpenTracing Tracer implementation designed to monitor AWS Lambda. It generates spans, error events, transaction events, error traces, and provides distributed tracing support.
Tip
Supported OpenTracing Versions
- OpenTracing 0.31.0:
- Lambda Tracer: com.newrelic.opentracing:newrelic-java-lambda:1.1.1
- Lambda SDK: com.newrelic.opentracing:java-aws-lambda:1.0.0
- OpenTracing 0.32.0, 0.33.0:
- Lambda Tracer: com.newrelic.opentracing:newrelic-java-lambda:2.2.3
- Lambda SDK: com.newrelic.opentracing:java-aws-lambda:2.1.1
To instrument your Java Lambda:
- OpenTracing 0.31.0:
In your project's
build.gradle
file, include our OpenTracing AWS Lambda Tracer and the AWS Lambda OpenTracing SDK dependencies:dependencies {compile("com.newrelic.opentracing:java-aws-lambda:2.1.1")compile("com.newrelic.opentracing:newrelic-java-lambda:2.2.3")compile("io.opentracing:opentracing-util:0.33.0")}Implement the AWS Lambda
RequestHandler
interface as shown in the Java Lambda example and override thedoHandleRequest
method.In the
doHandleRequest
method, call theLambdaTracing.instrument(...)
API to create a root span to trace the lambda function's execution. This is also where you will define your business logic for the lambda function.Register a
LambdaTracer.INSTANCE
as the OpenTracing Global tracer, as shown in the Java Lambda example.Create a ZIP deployment package and upload it to AWS Lambda. Or deploy it via other means.
In the AWS Lambda console, set the handler. For the example Java Lambda, the handler would be
com.handler.example.MyLambdaHandler::handleRequest
. BecausehandleRequest
is assumed, you could also usecom.handler.example.MyLambdaHandler
.The following AWS console environment variables are required if you want your Lambda function to be included in distributed tracing. This is recommended.
NEW_RELIC_ACCOUNT_ID
. Your account ID.NEW_RELIC_PRIMARY_APPLICATION_ID
. This is also your account ID.NEW_RELIC_TRUSTED_ACCOUNT_KEY
. This is also your account ID. If your account is a child account, this must be the account ID for the root/parent account.
Optional: In the Lambda console, enable debug logging by adding this environment variable:
NEW_RELIC_DEBUG
istrue
.Invoke the Lambda at least once. This creates a CloudWatch log group, which must be present for the next step to work.
Our wrapper gathers data about the Lambda execution, generates a JSON message, and logs it to CloudWatch Logs. Next, you'll configure CloudWatch to send those logs to New Relic.
Please see the AWS Lambda distributed tracing example for a complete project that illustrates common use cases such as:
- Distributed tracing between Lambda functions
- Manual span creation (aka custom instrumentation)
- Tracing external calls
- Adding custom attributes (aka Tags) to spans
To instrument your Node.js Lambda:
Download our Node.js agent package and place it in the same directory as your function, ensuring the agent is installed as a dependency in the
node_modules
directory. Use the Node Package Manager:bash$npm install newrelic --saveIn your Lambda code, require the agent module at the top of the file, and wrap the handler function. For example:
const newrelic = require('newrelic');// Other module loads go under the require statement abovemodule.exports.handler = newrelic.setLambdaHandler((event, context, callback) => {// This is your handler function codeconsole.log('Lambda executed');callback();});Optional: You can also add custom events to your Lambda using the
recordCustomEvent
API. For example:module.exports.handler = newrelic.setLambdaHandler((event, context, callback) => {newrelic.recordCustomEvent('MyEventType', { foo: 'bar' });console.log('Lambda executed');callback();});Zip your Lambda function and the Node.js agent folder together. Requirements and recommendations:
- The New Relic files outside the New Relic agent folder don't need to be included.
- If your Lambda function file name is, for example,
lambda_function.node
, we recommend naming your zip filelambda_function.zip
. Do not use a tarball. - Your Lambda and its associated modules must all be in the zip file's root directory. This means that if you zip a folder that contains the files, it won't work.
Upload the zipped file to your AWS Lambda account.
In the AWS console, set these environment variables:
NEW_RELIC_NO_CONFIG_FILE
. Set totrue
if not using a configuration file.NEW_RELIC_APP_NAME
: Your application name.NEW_RELIC_ACCOUNT_ID
. Your account ID.NEW_RELIC_TRUSTED_ACCOUNT_KEY
. This is also your account ID. If your account is a child account, this needs to be the account ID for the root/parent account.
Optional: To run the agent in serverless mode outside of AWS in a local environment, set the environment variable
NEW_RELIC_SERVERLESS_MODE_ENABLED
totrue
. (When executing this in an AWS Lambda environment, the agent will automatically run in serverless mode. Do not use this variable if you're running in AWS.)Optional: To enable logging in serverless mode, set these environment variables:
- Set
NEW_RELIC_LOG_ENABLED
totrue
. - Set
NEW_RELIC_LOG
tostdout
for output to CloudWatch, or set to any writeable file location. NEW_RELIC_LOG_LEVEL
is set toinfo
by default, and it's only used when sending function logs in your Lambda. See other log levels.
- Set
Optional: If your Lambda function is using ES Modules, set the environment variable
NEW_RELIC_USE_ESM
totrue
. Note that if using ES Modules, you must use async/await or promises for your function. Callback-based functions are not supported.Invoke the Lambda at least once. This creates a CloudWatch log group, which must be present for the next step to work.
Our wrapper gathers data about the Lambda execution, generates a JSON message, and logs it to CloudWatch Logs. Next you'll configure CloudWatch to send those logs to New Relic.
Read more on how to send function logs with Lambda.
To instrument your Python Lambda:
Download our Python agent package and place it in the same directory as your function. To do this, use pip:
bash$pip install -t . newrelicImportant
If you use Homebrew, you may get this error:
DistutilsOptionError: must supply either home or prefix/exec-prefix -- not both
. For details, see the Homebrew GitHub post.In your Lambda code, import the Python agent module and decorate the handler function using the New Relic decorator. The New Relic package must be imported first in your code. Here's an example:
import newrelic.agentnewrelic.agent.initialize()@newrelic.agent.lambda_handler()def handler(event, context):...Optional: You can also add custom events to your Lambda using the
record_custom_event
API. Here's an example:@newrelic.agent.lambda_handler()def handler(event, context):newrelic.agent.record_custom_event('CustomEvent', {'foo': 'bar'})...Zip your
lambda_function.py
andnewrelic/
folder together using these guidelines:- The New Relic files outside the
newrelic/
folder don't need to be included. - If your Lambda function file name is, for example,
lambda_function.py
, name your zip filelambda_function.zip
. Do not use a tarball. - Your Lambda and its associated modules must all be in the zip file's root directory. This means that if you zip a folder that contains the files, it won't work.
- The New Relic files outside the
Upload the zipped file to your AWS Lambda account.
In the AWS console, set this environment variable:
NEW_RELIC_SERVERLESS_MODE_ENABLED
. Set totrue
The following environment variables are not required for Lambda monitoring to function but they are required if you want your Lambda functions to be included in distributed traces. To enable distributed tracing, set these environment variables in the AWS console:
NEW_RELIC_DISTRIBUTED_TRACING_ENABLED
. Set to true.NEW_RELIC_ACCOUNT_ID
. Your account ID.NEW_RELIC_TRUSTED_ACCOUNT_KEY
. This is also your account ID. If your account is a child account, this needs to be the account ID for the root/parent account.
Optional: To configure logging, use the
NEW_RELIC_LOG
andNEW_RELIC_LOG_LEVEL
environment variables in the AWS Console.Invoke the Lambda at least once. This creates a CloudWatch log group, which must be present for the next step to work.
The New Relic decorator gathers data about the Lambda execution, generates a JSON message, and logs it to CloudWatch Logs. Next, configure CloudWatch to send those logs to New Relic.
There are two ways to instrument your Ruby Lambda functions: automatically or manually.
Automatic instrumentation:
- Download and install our Lambda CLI tool, newrelic-lambda-cli.
- Use the tool to issue a
newrelic-lambda integrations install
command. See the usage instructions for more information.
Manual instrumentation:
- 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.
- Now 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.
- Define the following environment variables:
NEW_RELIC_ACCOUNT_ID
Set to your New Relic account IDNEW_RELIC_LAMBDA_HANDLER
Set to your function's original Handler value copied down in step 8 earlierNEW_RELIC_LICENSE_KEY
Set to your New Relic license keyNEW_RELIC_LOG_ENDPOINT
Set to 'https://log-api.newrelic.com/log/v1'NEW_RELIC_TELEMETRY_ENDPOINT
Set to 'https://cloud-collector.newrelic.com/aws/lambda/v1'
- If you want to send your Lambda function's logs to New Relic, which will include everything your function writes to STDOUT, make sure you set the
NEW_RELIC_EXTENSION_SEND_FUNCTION_LOGS
environment variable to 'true'.
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, do not 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.