可以通过查看SNS通知中的消息属性来确定哪个AWS服务触发了该通知。具体来说,消息属性中的“Subject”字段可用于标识消息的主题或标题。如果您为每个AWS服务配置了特定的主题前缀,则可以使用此字段识别出具体的AWS服务。
以下是一个示例代码片段,它演示了如何从SNS通知中提取主题并使用主题前缀来确定哪个AWS服务触发了通知:
import json
def lambda_handler(event, context):
message = json.loads(event['Records'][0]['Sns']['Message'])
subject = message['Subject']
if subject.startswith('AWS S3'):
# S3 triggered the notification
print('S3 triggered the notification')
elif subject.startswith('AWS Lambda'):
# Lambda triggered the notification
print('Lambda triggered the notification')
else:
# unknown service triggered the notification
print('Unknown service triggered the notification')
在上面的代码中,我们首先使用“json.loads”函数从SNS通知中提取消息正文。然后,我们获取主题并使用字符串的“startswith”方法检查主题前缀。如果主题以“AWS S3”开头,则我们可以确定S3触发了通知。同样地,如果主题以“AWS Lambda”开头,则我们可以确定Lambda触发了通知。最后,如果主题没有匹配的前缀,则我们无法确定哪个服务触发了通知。