'(CLOSED) How to work with global variable in AWS Lambda (python)

I need an approach in AWS lambda to resolve an issue

What am I doing now:

I am looking to build a quick script to test my understanding on global variables. In my script, I place a variable outside the handler, and I assign it a value for the sake of declaring it. Then I place an addition statement within the handler hoping that for every invocaIion, variable i will be added 1. n a way, I am expecting this to work like a test counter - every time I test the lambda script, AWS prepares the environment, run the script, record down the changes (+1), shuts off while storing the latest value (1) in global variable i.

import json
import boto3

i = 0

def lambda_handler(event, context):
    
    i+=1        
    print(i)

My problem is

I was expecting the output to be 1,2,3,4,5.... However, what happened was it only output 1 all the time. I suppose this could be due to me declaring variable i as 0 , but such is required as I need to declare i.

What I need

I need some solution (most likely a global variable) to store and pass down the values generated from each lambda script invocation down to the subsequent one.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source