How can I get the name of the lambda function that raised error with DLQ?

0

LambdaA is a function that raises an error.

import json

def lambda_handler(event, context):
    raise Exception("ERROR!!")
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

LambdaB is a function that prints "event".

import json

def lambda_handler(event, context):
    print(f"Event: {event}")
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

The SNS topic "TestTopic" is set as Dead-letter queue service in the LambdaA .
"TestTopic" is subscribed by LambdaB. dlq

I invoked LambdaA asynchronously and checked the CloudWatch logs for LambdaB.
And then "event" dose not seem to contain the function name that caused the error.

Event: {'Records': [{'EventSource': 'aws:sns', 'EventVersion': '1.0', 'EventSubscriptionArn': 'arn:aws:sns:ap-northeast-1:xxxxxxxxxxxx:TestTopic:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Sns': {'Type': 'Notification', 'MessageId': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'TopicArn': 'arn:aws:sns:ap-northeast-1:xxxxxxxxxxxx:TestTopic', 'Subject': None, 'Message': '{}', 'Timestamp': '2024-03-15T10:26:25.862Z', 'SignatureVersion': '1', 'Signature': 'xxxxxxxxxxxxxxxxxxx', 'SigningCertUrl': 'https://sns.ap-northeast-1.amazonaws.com/SimpleNotificationService-xxxxxxxxxxxxxxxxxxxx.pem', 'UnsubscribeUrl': 'https://sns.ap-northeast-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:ap-northeast-1:xxxxxxxxxxxx:TestTopic:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'MessageAttributes': {'RequestID': {'Type': 'String', 'Value': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'}, 'ErrorCode': {'Type': 'String', 'Value': '200'}, 'ErrorMessage': {'Type': 'String', 'Value': 'ERROR!!'}}}}]}

How can I get the function name?

  • I cant even get the above to send a message to SNS

yyahi
gefragt vor 2 Monaten217 Aufrufe
2 Antworten
2
Akzeptierte Antwort

Hi,

AWS Lambda does not include the function name in the attributes that are added to the event, see https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html#invocation-dlq

Lambda sends the event to the dead-letter queue as-is, with additional information in attributes. You can use this information to identify the error that the function returned, or to correlate the event with logs or an AWS X-Ray trace.

Dead-letter queue message attributes

RequestID (String) – The ID of the invocation request. Request IDs appear in function logs. You can also use the X-Ray SDK to record the request ID on an attribute in the trace. You can then search for traces by request ID in the X-Ray console. For an example, see the error processor sample.

ErrorCode (Number) – The HTTP status code.

ErrorMessage (String) – The first 1 KB of the error message.

However, you could implement such logic in your exception handling logic in your Lambda function. Alternatively, you could use a 1:1 mapping between SNS topics and Lambda functions, that way it would always be clear from which function the message originated.

Regards, Ben

profile pictureAWS
EXPERTE
beantwortet vor 2 Monaten
profile picture
EXPERTE
überprüft vor 2 Monaten
0

Thank you for your answer. I understood your answer. I also want to catch timeout and throttling, so I will try 1:1 mapping between SNS topics and Lambda functions.

Best regards, yyahi

yyahi
beantwortet vor 2 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen