Your data. Anywhere you go.

New Relic for iOS or Android

Download on the App Store    Android App on Google play

New Relic Insights App for iOS

Download on the App Store

Learn more

Close icon

Send data to APM from lambda python 2.7 code



Python Agent Question Template

Please provide a permalink so our experts can see into the exact time and place as you:

  • What language agent version are you using?
    We are using python 2.7 in a lambda function in AWS.

  • What error message are you seeing if any?
    We just want to do some test to send data to APM in order to test in production lambdas.

import newrelic
import newrelic.agent
application = newrelic.agent.application()

def lambda_handler(event, context):
result= “hello world”
newrelic.agent.record_custom_event(‘your_event_type’, {‘param1’:‘value1’}, application)
return result

  • Describe what are you seeing? How does that differ than what were you expecting to see?
    We are waiting for the data in newrelic APM after 5 minutes it does not appear. We also generate de newrelic.ini and install the newrelic agent in the lambda

In the APM it appears “Still waiting for data form application”


Hi @ca.zambrano11 !

Normally, the Python Agent registers a persistent connection when the application starts receiving traffic. If the connection is not established prior to termination of the process, no data will be sent. Since this is a lambda job, the job will terminate prior to the connection to New Relic being established. Details about this behavior are available here.

To force an immediate connection, the newrelic.agent.application() call can be changed to newrelic.agent.register_application(timeout=10.0).

This type of workload also seems really well suited for the insights API!


Hi @afeldman
I have followed above steps but nothing is sent to insights from Lambda.
python newrelic.agent.xxxxxx() does not work.
Log shows register successfully and reporting to newrelic but no data shows on new relic


Hi @wayne.wu1 ,

Here’s a solution which should generically work for Python Lambda handlers. Keep in mind that using this solution can cause some performance degradation since the agent will attempt to connect on cold start and will send data to New Relic on every invocation (which takes some additional time).

This solution assumes the NEW_RELIC_LICENSE_KEY environment variable is set in your Lambda job.

The background_task decorator can be used as described here:

import newrelic.agent
# Important: force the first call to report immediately to New Relic by setting
# the startup_timeout. This can also be set through the
# NEW_RELIC_STARTUP_TIMEOUT environment variable.
newrelic.agent.global_settings().startup_timeout = 10.0

def background_task(*args, **kwargs):
    from newrelic.core.agent import agent_instance

    def _wrapper(wrapped, instance, _args, _kwargs):
            decorator = newrelic.agent.background_task(*args, **kwargs)
            return decorator(wrapped)(*_args, **_kwargs)

    return _wrapper

def handler(event, context):
    return {'status': 'ok'}


Hi @afeldman
Thank you for your solution. I found out why it did not work before. I need set up a bigger timeout number. Appreciate your answer. I will keep it as a backup.