|
1 | | -from typing import Any |
| 1 | +"""Simple durable Lambda handler example. |
2 | 2 |
|
3 | | -from aws_durable_execution_sdk_python.context import DurableContext |
| 3 | +This example demonstrates: |
| 4 | +- Step execution with logging |
| 5 | +- Wait operations (pausing without consuming resources) |
| 6 | +- Replay-aware logging |
| 7 | +- Returning a response |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from typing import TYPE_CHECKING, Any |
| 13 | + |
| 14 | +from aws_durable_execution_sdk_python.config import Duration |
| 15 | +from aws_durable_execution_sdk_python.context import DurableContext, durable_step |
4 | 16 | from aws_durable_execution_sdk_python.execution import durable_execution |
5 | 17 |
|
| 18 | +if TYPE_CHECKING: |
| 19 | + from aws_durable_execution_sdk_python.types import StepContext |
| 20 | + |
| 21 | + |
| 22 | +@durable_step |
| 23 | +def step_1(step_context: StepContext) -> None: |
| 24 | + """First step that logs a message.""" |
| 25 | + step_context.logger.info("Hello from step1") |
| 26 | + |
| 27 | + |
| 28 | +@durable_step |
| 29 | +def step_2(step_context: StepContext, status_code: int) -> str: |
| 30 | + """Second step that returns a message.""" |
| 31 | + step_context.logger.info("Returning message with status code: %d", status_code) |
| 32 | + return f"Hello from Durable Lambda! (status: {status_code})" |
| 33 | + |
6 | 34 |
|
7 | 35 | @durable_execution |
8 | | -def handler(_event: Any, _context: DurableContext) -> str: |
9 | | - """Simple hello world durable function.""" |
10 | | - return "Hello World!" |
| 36 | +def handler(event: Any, context: DurableContext) -> dict[str, Any]: |
| 37 | + """Durable Lambda handler with steps, waits, and logging. |
| 38 | +
|
| 39 | + Args: |
| 40 | + event: Lambda event input |
| 41 | + context: Durable execution context |
| 42 | +
|
| 43 | + Returns: |
| 44 | + Response dictionary with statusCode and body |
| 45 | + """ |
| 46 | + # Execute Step #1 - logs a message |
| 47 | + context.step(step_1()) |
| 48 | + |
| 49 | + # Pause for 10 seconds without consuming CPU cycles or incurring usage charges |
| 50 | + # The execution will suspend here and resume after 10 seconds |
| 51 | + context.wait(Duration.from_seconds(10)) |
| 52 | + |
| 53 | + context.logger.info("Waited for 10 seconds") |
| 54 | + |
| 55 | + # Execute Step #2 - returns a message with status code |
| 56 | + message = context.step(step_2(status_code=200)) |
| 57 | + |
| 58 | + # Return response |
| 59 | + return { |
| 60 | + "statusCode": 200, |
| 61 | + "body": message, |
| 62 | + } |
0 commit comments