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?