Write AWS Lambda durable functions in Python: workflows with checkpointed steps, waits, callbacks, and parallel work.
Install
pip install aws-durable-execution-sdk-python
Quick start
from aws_durable_execution_sdk_python import (
DurableContext,
StepContext,
durable_execution,
durable_step,
)
from aws_durable_execution_sdk_python.config import Duration
@durable_step
def validate_order(step_ctx: StepContext, order_id: str) -> dict:
step_ctx.logger.info("Validating order", extra={"order_id": order_id})
return {"order_id": order_id, "valid": True}
@durable_execution
def handler(event: dict, context: DurableContext) -> dict:
order_id = event["order_id"]
validation = context.step(validate_order(order_id))
if not validation["valid"]:
return {"status": "rejected", "order_id": order_id}
# The function stops while it waits, so no Lambda compute is billed.
context.wait(Duration.from_seconds(5), name="cool-down")
return {"status": "approved", "order_id": order_id}
@durable_execution makes handler the Lambda handler. Each context.step result is checkpointed. After a wait or a failure, the function runs again from the start, and completed steps return their checkpointed result.
The function stops while it waits for the result. The target can be a durable function or a plain Lambda function.
Advanced
Retries and errors
A step retries on failure according to its retry strategy. After the last attempt, the step raises StepError.
from aws_durable_execution_sdk_python import (
DurableContext,
StepContext,
StepError,
durable_execution,
durable_step,
)
from aws_durable_execution_sdk_python.config import Duration, StepConfig
from aws_durable_execution_sdk_python.retries import (
RetryStrategyConfig,
create_retry_strategy,
)
RETRY = StepConfig(
retry_strategy=create_retry_strategy(
RetryStrategyConfig(max_attempts=3, initial_delay=Duration.from_seconds(2))
)
)
@durable_step
def charge(step_ctx: StepContext, amount: int) -> dict:
# Call the payment service here. An exception makes the step retry.
return {"status": "charged", "amount": amount}
@durable_execution
def handler(event: dict, context: DurableContext) -> dict:
try:
return context.step(charge(event["amount"]), config=RETRY)
except StepError as error:
context.logger.warning("Charge failed", extra={"error": str(error)})
return {"status": "failed"}
RetryPresets in aws_durable_execution_sdk_python.retries holds ready-made strategies.
Pause after repeated failures (experimental)
A retry decision can pause the durable execution. When the retry strategy returns RetryDecision.retry(delay, pause=True) for a failed attempt, the step saves its normal retry, then the SDK asks Lambda to pause, and the invocation ends. An operator resumes the execution with the Lambda ResumeDurableExecution API, and the next attempt runs.
The built-in strategy pauses after every Nth failed attempt with pause_every. For example, pause_every=5 pauses after attempts 5, 10, 15, and so on:
pause does not change how many attempts run. When the decision has should_retry=False, the step fails as usual and does not pause.
context.step uses pause, including the wait_for_callback submitter. with_retry ignores pause.
Only an attempt that raised in the current invocation can pause. An AT_MOST_ONCE_PER_RETRY attempt that was interrupted retries without a pause.
The pause applies to the whole execution. A step in a map or parallel branch can still pause the execution after the map or parallel operation has finished. No checkpointed data is lost. Resume the execution to continue.
Give the function's execution role lambda:PauseDurableExecution on its own durable executions. If the function uses a customer managed AWS KMS key, the role also needs kms:Decrypt on that key, as the Lambda pause and resume prerequisites require. If the pause call fails, the SDK logs a warning with the error and the step retries as usual. A missing permission is one possible cause.
This feature is experimental. It may change or be removed in a future release.
Replay rules
The function runs again from the start after each wait, retry, or resume. So:
Code outside steps must be deterministic. Put timestamps, random values, and API calls in a step.
Do not call durable operations inside a step. Use a child context to group operations.
Return values from steps. A step's changes to variables outside it are lost on replay.
Use context.logger for logs. It does not repeat log lines on replay.
Instrumentation plugins
Set DURABLE_EXECUTION_PLUGINS to load installed plugins at cold start, in order:
DURABLE_EXECUTION_PLUGINS=otel-invocation
You can also pass plugin factories to @durable_execution(plugins=[...]). See docs/plugins.md for the factory contract, the OpenTelemetry views, and trace propagation.
Use together with aws-durable-execution-sdk-python: extensions, drivers, servers and companion tools.
aws-lambda-powertoolsPythie’s Index: 89Powertools for AWS Lambda (Python) is a developer toolkit to implement Serverless best practices and increase developer velocity.15.1M downloads/mo