AWS Lambda / SNS发布忽略无效端点

时间:2016-06-07 08:46:37

标签: python amazon-web-services aws-lambda amazon-sns boto3

我通过Lambda使用Boto3和Python通过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

1 个答案:

答案 0 :(得分:2)

Aws lamdba失败时重试该功能,直到事件从流中过期。

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

如果您处理异常并确保即使发生故障也能成功结束该功能,则不会重试。

相关问题