Search code examples
python-3.xchalice

Strange issue with Python recursive function in Chalice framework


I have defined this SNS-triggered Lambda in Chalice:

@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

When an exception is raised, the function should retry up to 3 times.

Instead, what I get is the exception below. Look closely at the trace: Line 56 shows the error occurs when attempting the recursive call:

[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]

Mysteriously, the function can't work with the event object that it received the first time.

What could cause this?

I suspect this might have something to do with the magic behind @app.on_sns_message, but I'm not sure where to look next.


Solution

  • The problem is the fact that the function is decorated, the failure is in the code the decorator is running. Pull the functionality you want to run recursively into a separate function and the problem should go away.