5

I have a problem aws lambda python. I have a variable defined in a lambda layer and when we have multiple calls to that code , the variable value from one execution is preserved and used by subsequent run.

Is there any way to make each lambda execution to reset all variable and not to use any variable value from previous run.

0

1 Answer 1

6

I suspect this is nothing to do with Lambda Layers and everything to do with container reuse (a performance feature of Lambda). Anything declared outside your handler function will persist across warm restarts.

For more, see AWS Lambda Execution Context.

One possible way to test for cold/warm startup, and reset an environment, is as follows:

import json import logging cache = {} cold_start = True def handler(event, context): global cold_start if cold_start: print("Cold start") cold_start = False else: print("Warm start, reset cache") cache = {} # do work here cache['name'] = 'Jason' cache['age'] = 27 
Sign up to request clarification or add additional context in comments.

1 Comment

Or, go the extra mile and avoid stateful Lambda functions

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.