So, that's how a function can be invoked. What happens when the code finishes executing? When this happens, the function runtime is terminated. This includes access to the memory state and any temporary files that were created on the filesystem. In other words, everything is gone – state included. This means that if you need to maintain or keep track of a state, then this should be saved to a service that's external to the function, such as Amazon DynamoDB.
Functions also have a finite execution time. There is a soft limit that you can configure yourself, and there is a hard limit of 900 seconds (15 minutes). If your function is still executing at this time, it will be terminated regardless. Some might say this is a limitation of the service, but actually this is a design decision that encourages you to build your applications within the bounds of a serverless architecture pattern.