0
0
AWScloud~10 mins

Lambda execution model in AWS - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Lambda execution model
Event triggers Lambda
Lambda service receives event
Check for existing container
Reuse container
Run handler code inside container
Return response or error
Container kept warm or terminated
This flow shows how AWS Lambda receives an event, checks for a container to run the code, executes the handler, and then either keeps the container ready or ends it.
Execution Sample
AWS
def lambda_handler(event, context):
    print('Start')
    result = event['value'] * 2
    print('End')
    return result
A simple Lambda function that doubles a value from the event and prints start and end messages.
Process Table
StepActionContainer StateHandler ExecutionOutput
1Event triggers LambdaNo containerNot startedNone
2Create new containerContainer createdNot startedNone
3Run handler codeContainer activePrint 'Start'None
4Process event dataContainer activeCalculate result = value * 2None
5Run handler codeContainer activePrint 'End'None
6Return resultContainer activeReturn doubled valueResult returned
7Container kept warmContainer ready for next eventIdleNone
8No new event arrivesContainer terminated after timeoutStoppedNone
💡 Execution stops when no new events arrive and container is terminated after idle timeout.
Status Tracker
VariableStartAfter Step 4After Step 6Final
event['value']5 (example)555
resultundefined101010
container_stateNo containerContainer activeContainer activeContainer terminated
Key Moments - 3 Insights
Why does Lambda sometimes create a new container and sometimes reuse an existing one?
Lambda reuses containers to save time on startup. If a container is available (see Step 3), it runs the handler there. If not, it creates a new container (Step 2).
What happens to the container after the Lambda function finishes running?
After returning the result (Step 6), the container stays warm for a while (Step 7) to handle new events quickly, then terminates if idle (Step 8).
Does the variable 'result' keep its value between Lambda invocations?
No, each invocation runs fresh code. Variables like 'result' are reset each time the handler runs (Step 4 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the handler code first executed?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Handler Execution' column for when 'Print Start' happens.
According to the variable tracker, what is the value of 'result' after Step 4?
Aundefined
B5
C10
DNone
💡 Hint
Look at the 'result' row under 'After Step 4' column.
If no new event arrives, what happens to the container according to the execution table?
AIt stays active indefinitely
BIt is kept warm for a while then terminated
CIt immediately terminates after Step 6
DIt restarts automatically
💡 Hint
See Steps 7 and 8 in the 'Container State' column.
Concept Snapshot
AWS Lambda execution model:
- Event triggers Lambda service
- Lambda checks for existing container
- Reuses container or creates new one
- Runs handler code inside container
- Returns result
- Container kept warm for reuse or terminated after idle
- Variables reset each invocation
Full Transcript
AWS Lambda runs code in response to events. When an event arrives, Lambda checks if a container is ready. If not, it creates one. Then it runs your handler code inside that container. After running, it returns the result. The container stays ready for a short time to handle more events quickly. If no events come, the container shuts down. Variables inside your code reset each time the handler runs.