AWS Lambda/SNS Publish 忽略无效端点

AWS Lambda/SNS Publish ignore invalid endpoints

我正在使用 Boto3 和 Python 通过 Lambda 通过 AWS SNS 发送苹果推送通知。

from __future__ import print_function
import boto3

def lambda_handler(event, context):

    client = boto3.client('sns')
    for record in event['Records']:
        if record['eventName'] == 'INSERT':
            rec = record['dynamodb']['NewImage']
            competitors = rec['competitors']['L']
            for competitor in competitors:
                if competitor['M']['confirmed']['BOOL'] == False:
                    endpoints = competitor['M']['endpoints']['L']
                    for endpoint in endpoints: 
                        print(endpoint['S'])
                        response = client.publish(
                            #TopicArn='string',
                            TargetArn = endpoint['S'],
                            Message = 'test message'
                            #Subject='string',
                            #MessageStructure='string',
                        )

一切正常!但是当端点由于某种原因无效时(目前每次我 运行 在我的设备上构建开发时都会发生这种情况,因为那时我得到了一个不同的端点。这将找不到或停用。)Lambda 函数失败并再次被调用。在这种特殊情况下,例如,如果第二个端点失败,它将一遍又一遍地将推送发送到端点 1 到无穷大。

是否可以忽略无效端点并继续使用该函数?

谢谢

编辑:

多亏了你的帮助,我解决了这个问题:

try:
    response = client.publish(
        #TopicArn='string',
        TargetArn = endpoint['S'],
        Message = 'test message'
        #Subject='string',
        #MessageStructure='string',
    )
except Exception as e:
    print(e)
    continue

Aws lamdba 失败时会重试该函数,直到事件从流中过期。

在您的情况下,由于未处理第二个端点上的异常,重试机制确保 post 重新执行到第一个端点。

如果您处理异常并确保函数成功结束,即使出现故障,也不会发生重试。