我在 Chalice 中定义了这个 SNS 触发的 Lambda:
@app.on_sns_message(topic='arn:aws:sns:us-west-1:XXXXXXXX:MyTopic')
def step1_photo_url_preload(event, retry = 3):
try:
js = json.loads(event.message)
... some logic here, event object is never modified ...
except:
if retry:
print("WARNING: failed, %d retries remaining" % retry)
return step1_photo_url_preload(event, retry-1)
else:
raise
当引发异常时,该函数应最多重试 3 次。
相反,我得到的是下面的例外。仔细查看跟踪:第 56 行显示尝试递归调用时发生错误:
[ERROR] TypeError: 'SNSEvent' object is not subscriptable
Traceback (most recent call last):
File "/var/task/chalice/app.py", line 1459, in __call__
return self.func(event_obj)
File "/var/task/app.py", line 56, in step1_photo_url_preload
return step1_photo_url_preload(event, retry-1)
File "/var/task/chalice/app.py", line 1458, in __call__
event_obj = self.event_class(event, context)
File "/var/task/chalice/app.py", line 1486, in __init__
self._extract_attributes(event_dict)
File "/var/task/chalice/app.py", line 1532, in _extract_attributes
first_record = event_dict['Records'][0]
神秘的是,该函数不能与event
它第一次收到的对象一起工作。
什么可能导致这种情况?
我怀疑这可能与背后的魔力有关@app.on_sns_message
,但我不确定下一步该往哪里看。