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

2回答
2
承認された回答

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
エキスパート
回答済み 2ヶ月前
profile picture
エキスパート
レビュー済み 2ヶ月前
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
回答済み 2ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ