Skip to content

Bug: Powertools JMESPath functions don't work in @logger.inject_lambda_context(correlation_id_path=...) #3381

Closed
@heitorlessa

Description

@heitorlessa

Expected Behaviour

Able to deserialize JSON string and extract given field

Current Behaviour

UnknownFunctionError exception by JMESPath when using any of the Powertools custom functions: https://docs.powertools.aws.dev/lambda/python/latest/utilities/jmespath_functions/#extracting-data

Code snippet

# Extract Event IDs from EventBridge and set as correlation ID (list value as there could be N events)
@logger.inject_lambda_context(correlation_id_path="Records[*].powertools_json(body).id")
@metrics.log_metrics(capture_cold_start_metric=True)
@tracer.capture_lambda_handler
def lambda_handler(event, context: LambdaContext):
    return True

Possible Solution

Use jmespath_utils instead of jmespath.search like the rest of Powertools utilities (Logger, Idempotency, Validation).

Steps to Reproduce

Call this function with the following event:

{
    "Records": [
        {
            "messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
            "receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...",
            "body": "{'id': '6a7e8feb-b491-4cf7-a9f1-bf3703467718'}",
            "attributes": {
                "ApproximateReceiveCount": "1",
                "SentTimestamp": "1545082649183",
                "SenderId": "AIDAIENQZJOLO23YVJ4VO",
                "ApproximateFirstReceiveTimestamp": "1545082649185"
            },
            "messageAttributes": {
                "testAttr": {"stringValue": "100", "binaryValue": "base64Str", "dataType": "Number"}
            },
            "md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
            "eventSource": "aws:sqs",
            "eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
            "awsRegion": "us-east-2"
        }
    ]
},

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.11

Packaging format used

PyPi

Debugging logs

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinglogger

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions