0

I found that python instructions which are not in the main function but in the global scope are run only once, after the function is created/updated (and perhaps after the container replacement but did not test it) which causes not the same behavior of the function first and all subsequent launches. The test code:

assets = []
print "======0. should be [] ========"
print(assets)   

def lambda_handler(event, context):
    global assets

    print "======l. should be [] ========"
    print(assets)   

    assets.append({
            "key": "val"
        })
    print "======2. should be [{'key': 'val'}] ========"
    print(assets)

    assets = {"data":assets}
    print "======3. should be {'data': [{'key': 'val'}]} ========"
    print(assets)

When I run the function the very first time (or after I updated it) I have:

======0. should be [] ========
[]
START RequestId: ecdf063e-51f0-11e8-9783-7f18ff8ee142 Version: $LATEST
======l. should be [] ========
[]
======2. should be [{'key': 'val'}] ========
[{'key': 'val'}]
======3. should be {'data': [{'key': 'val'}]} ========
{'data': [{'key': 'val'}]}
END RequestId: ecdf063e-51f0-11e8-9783-7f18ff8ee142 

This is what I expect despite I confused why the output started before 'START'. The issue is: when I run it next time assets variable already have old value:

START RequestId: 66999707-51f1-11e8-b21a-9963eeb4aa56 Version: $LATEST
======l. should be [] ========
{'data': [{'key': 'val'}]}
'dict' object has no attribute 'append': AttributeError
Traceback (most recent call last):
  File "/var/task/test.py", line 11, in lambda_handler
    assets.append({
AttributeError: 'dict' object has no attribute 'append'

END RequestId: 66999707-51f1-11e8-b21a-9963eeb4aa56

As you can see there's no ======0. should be [] ======== output, and the variable already have value.

I'm not a Python guru, so am I doing something wrong or is it a bug?

1 Answer 1

2

Lambda may or may not re-use the same function when you re-run it, so in other words, it is possible that some state will be left over between runs - you need to make sure you don't assume anything - so if you need something to be initialized, do it in the function itself - not in the global scope.

This article http://rodos.haywood.org/2015/06/lambda-functions-idempotent.html, although about nodejs not python, pretty much explains it.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Brenanan, What if I need to have a variable in global scope. My requirement is I am using aws lambda handler which calls athena table. But it calls athena for every 20 sec. I wanted to make it global, so subsequent calls to athena can be avoided I can reuse the resultset dataframe . What I have to do now to make it global . Please help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.