AWS Lambda PowerTools for Python and Serverless Framework

Hello,
Has anyone successfully implemented AWS Powertools for Python in serverless? I have added the permissions for xray:

- Effect: Allow
      Action:
        - xray:PutTraceSegments
        - xray:PutTelemetryRecords
      Resource: "*"

set Tracing to Active:
Tracing: Active

and have set the environment variables:

environment:
    POWERTOOLS_SERVICE_NAME: test
    POWERTOOLS_LOGGER_LOG_EVENT: True
    POWERTOOLS_METRICS_NAMESPACE: test

added imports for the tracer and logger components:

from aws_lambda_powertools import Tracer, Logger

# Setting up aws-powertools tracing and logging
tracer = Tracer()
logger = Logger()
logger.setLevel(LOG_LEVEL)

In the lambda function I added the following to my main function:

@tracer.capture_lambda_handler
def notify(event, context):
 logger.debug("event: %s", event)
 ...
 service = event['detail']['service']
 logger.debug("service: %s", service)
...

The logging functionality works when I execute the lambda, but I don’t see any traces being generated. Any help with this would be appreciated.

Figured out the issue. Turns out I was setting the Tracing setting incorrectly. So instead of:
Tracing: Active

It should be:

tracing:
    lambda: true

How did you figure out what this setting should be? Is this documented anywhere?

Yes, it’s in the serverless docs under AWS X-Ray Tracing here

1 Like