generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 1k
New serverless pattern - lambda-durable-invoke-lambda-sam-python #2937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kothsidh
wants to merge
4
commits into
aws-samples:main
Choose a base branch
from
kothsidh:kothsidh-feature-lambda-durable-invoke-lambda-sam-python
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a3c85a2
Initial Commit
kothsidh 77ed527
Update lambda-durable-invoke-lambda-sam-python/README.md name
kothsidh c6e39be
Update lambda-durable-invoke-lambda-sam-python/example-pattern.json
kothsidh c49fda2
Update lambda-durable-invoke-lambda-sam-python/README.md
kothsidh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # Lambda durable functions invoking AWS Lambda function (Python) | ||
|
|
||
| This pattern demonstrates how an AWS Lambda durable function can invoke a standard Lambda function using `context.invoke()` from the AWS Durable Execution SDK. The invocation is automatically checkpointed, so if the durable function is interrupted after the invoked function completes, it resumes with the stored result without re-invoking the target function. | ||
|
|
||
| Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/lambda-durable-invoke-lambda-sam-python](https://serverlessland.com/patterns/lambda-durable-invoke-lambda-sam-python) | ||
|
|
||
| Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. | ||
|
|
||
| ## Requirements | ||
|
|
||
| * [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. | ||
| * [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured | ||
| * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) | ||
| * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed | ||
| * Python 3.14 | ||
|
|
||
| ## Deployment Instructions | ||
|
|
||
| 1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: | ||
| ``` | ||
| git clone https://github.com/aws-samples/serverless-patterns | ||
| ``` | ||
| 1. Change directory to the pattern directory: | ||
| ``` | ||
| cd lambda-durable-invoke-lambda-sam-python | ||
| ``` | ||
| 1. From the command line, use AWS SAM to build and deploy the AWS resources for the pattern as specified in the template.yaml file: | ||
| ``` | ||
| sam build | ||
| sam deploy --guided | ||
| ``` | ||
| 1. During the prompts: | ||
| * Enter a stack name | ||
| * Enter the desired AWS Region (durable functions are available in supported regions) | ||
| * Allow SAM CLI to create IAM roles with the required permissions. | ||
|
|
||
| Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. | ||
|
|
||
| 1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. | ||
|
|
||
| ## How it works | ||
|
|
||
| This pattern deploys two Lambda functions: | ||
|
|
||
| 1. **DurableLambdaFunction** - A durable Lambda function that orchestrates the workflow. It uses the `@durable_execution` decorator and performs two checkpointed operations: | ||
| - A `@durable_step` that prepares and validates input values. | ||
| - A `context.invoke()` call that invokes the ProcessorFunction and waits for its result. | ||
|
|
||
| 2. **ProcessorFunction** - A standard Lambda function that receives a list of numeric values and returns computed statistics (sum, average, max, min). | ||
|
|
||
| The durable function uses automatic checkpointing. Each step and invoke operation creates a checkpoint. If the function is interrupted (e.g., due to a transient failure), it replays from the beginning but skips completed checkpoints, resuming execution from where it left off. | ||
|
|
||
|
|
||
| ## Testing | ||
|
|
||
| 1. After deployment, invoke the durable function using the alias ARN from the stack outputs: | ||
|
|
||
| ```bash | ||
| aws lambda invoke \ | ||
| --function-name <DurableLambdaFunctionAliasArn> \ | ||
| --payload '{"values": [10, 20, 30, 40, 50]}' \ | ||
| --cli-binary-format raw-in-base64-out \ | ||
| output.json | ||
| ``` | ||
|
|
||
| 2. Check the response: | ||
|
|
||
| ```bash | ||
| cat output.json | ||
| ``` | ||
|
|
||
| Expected output: | ||
| ```json | ||
| {"statusCode": 200, "body": "{\"message\": \"Durable orchestration completed successfully\", \"input_values\": [10, 20, 30, 40, 50], \"processing_result\": {\"operation\": \"sum_and_average\", \"count\": 5, \"sum\": 150, \"average\": 30.0, \"max\": 50, \"min\": 10}}"} | ||
| ``` | ||
|
|
||
| 3. Monitor the durable execution steps in the Lambda console under the **Durable executions** tab of the DurableLambdaFunction. | ||
|
|
||
| ## Cleanup | ||
|
|
||
| 1. Delete the stack | ||
| ```bash | ||
| sam delete | ||
| ``` | ||
|
|
||
| ---- | ||
| Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: MIT-0 |
63 changes: 63 additions & 0 deletions
63
lambda-durable-invoke-lambda-sam-python/example-pattern.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| { | ||
| "title": "Lambda Durable Function invoking Lambda Function", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lambda durable functions invoking AWS Lambda function (Python) |
||
| "description": "Lambda durable function invokes a standard Lambda function using context.invoke() with automatic checkpointing.", | ||
| "language": "Python", | ||
| "level": "200", | ||
| "framework": "SAM", | ||
| "introBox": { | ||
| "headline": "How it works", | ||
| "text": [ | ||
| "This pattern deploys a durable Lambda function and a standard Lambda function. The durable function uses the AWS Durable Execution SDK to orchestrate a workflow that prepares input data in a checkpointed step and then invokes the standard Lambda function using context.invoke().", | ||
| "The context.invoke() call creates a checkpoint so that if the durable function is interrupted after the invoked function completes, it resumes with the stored result without re-invoking the processor function.", | ||
| "This pattern deploys two Lambda functions with the required IAM permissions for durable execution and cross-function invocation." | ||
| ] | ||
| }, | ||
| "gitHub": { | ||
| "template": { | ||
| "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-durable-invoke-lambda-sam-python", | ||
| "templateURL": "serverless-patterns/lambda-durable-invoke-lambda-sam-python", | ||
| "projectFolder": "lambda-durable-invoke-lambda-sam-python", | ||
| "templateFile": "template.yaml" | ||
| } | ||
| }, | ||
| "resources": { | ||
| "bullets": [ | ||
| { | ||
| "text": "AWS Lambda Durable Functions", | ||
| "link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html" | ||
| }, | ||
| { | ||
| "text": "Durable Execution SDK for Python", | ||
| "link": "https://github.com/aws/aws-durable-execution-sdk-python" | ||
| }, | ||
| { | ||
| "text": "Chained Invocations Across Functions", | ||
| "link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-examples.html" | ||
| } | ||
| ] | ||
| }, | ||
| "deploy": { | ||
| "text": [ | ||
| "sam build", | ||
| "sam deploy --guided" | ||
| ] | ||
| }, | ||
| "testing": { | ||
| "text": [ | ||
| "See the GitHub repo for detailed testing instructions." | ||
| ] | ||
| }, | ||
| "cleanup": { | ||
| "text": [ | ||
| "Delete the stack: <code>sam delete</code>." | ||
| ] | ||
| }, | ||
| "authors": [ | ||
| { | ||
| "name": "Sidharth Kothari", | ||
| "image": "https://drive.google.com/file/d/1sUXFJLHYuCmadcu4Q7mhb0mBnWfTcrtT/view", | ||
| "bio": "Cloud Engineer II at AWS with deep expertise in serverless, event-driven and microservice-based solutions", | ||
| "linkedin": "sidharthkothari" | ||
| } | ||
| ] | ||
| } | ||
50 changes: 50 additions & 0 deletions
50
lambda-durable-invoke-lambda-sam-python/src/durable_lambda_function.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import json | ||
| import os | ||
| from aws_durable_execution_sdk_python import ( | ||
| DurableContext, | ||
| StepContext, | ||
| durable_execution, | ||
| durable_step, | ||
| ) | ||
|
|
||
| PROCESSOR_FUNCTION_NAME = os.environ["PROCESSOR_FUNCTION_NAME"] | ||
|
|
||
|
|
||
| @durable_step | ||
| def prepare_input(step_ctx: StepContext, raw_values: list) -> dict: | ||
| """Prepare and validate input values before invoking the processor function.""" | ||
| step_ctx.logger.info("Preparing input values for processing") | ||
| return { | ||
| "values": raw_values, | ||
| "operation": "sum_and_average", | ||
| } | ||
|
|
||
|
|
||
| @durable_execution | ||
| def lambda_handler(event: dict, context: DurableContext) -> dict: | ||
| """Durable function that orchestrates processing by invoking another Lambda.""" | ||
| raw_values = event.get("values", [10, 20, 30, 40, 50]) | ||
| context.logger.info("Starting durable orchestration", extra={"values": raw_values}) | ||
|
|
||
| # Step 1: Prepare the input (checkpointed) | ||
| prepared = context.step(prepare_input(raw_values), name="prepare_input") | ||
|
|
||
| # Step 2: Invoke the processor Lambda function (checkpointed) | ||
| # If the durable function is interrupted after this completes, | ||
| # it resumes with the stored result without re-invoking the processor. | ||
| result = context.invoke( | ||
| function_name=PROCESSOR_FUNCTION_NAME, | ||
| payload=prepared, | ||
| name="invoke_processor", | ||
| ) | ||
|
|
||
| context.logger.info("Processing complete", extra={"result": result}) | ||
|
|
||
| return { | ||
| "statusCode": 200, | ||
| "body": json.dumps({ | ||
| "message": "Durable orchestration completed successfully", | ||
| "input_values": raw_values, | ||
| "processing_result": result, | ||
| }), | ||
| } |
21 changes: 21 additions & 0 deletions
21
lambda-durable-invoke-lambda-sam-python/src/processor_function.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| def lambda_handler(event, context): | ||
| """Standard Lambda function that processes sample values.""" | ||
| values = event.get("values", []) | ||
| operation = event.get("operation", "sum_and_average") | ||
|
|
||
| if not values: | ||
| return {"error": "No values provided"} | ||
|
|
||
| total = sum(values) | ||
| average = total / len(values) | ||
| maximum = max(values) | ||
| minimum = min(values) | ||
|
|
||
| return { | ||
| "operation": operation, | ||
| "count": len(values), | ||
| "sum": total, | ||
| "average": average, | ||
| "max": maximum, | ||
| "min": minimum, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| aws-durable-execution-sdk-python==1.3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| AWSTemplateFormatVersion: '2010-09-09' | ||
| Transform: AWS::Serverless-2016-10-31 | ||
| Description: > | ||
| Serverless pattern - Lambda Durable Function invoking another Lambda Function (Python). | ||
| Demonstrates how a durable Lambda function can use context.invoke() to call a standard | ||
| Lambda function as a checkpointed step in its workflow. | ||
|
|
||
| Globals: | ||
| Function: | ||
| Timeout: 30 | ||
| MemorySize: 128 | ||
|
|
||
| Resources: | ||
| # Standard Lambda function that processes sample values | ||
| ProcessorFunction: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| Handler: processor_function.lambda_handler | ||
| Runtime: python3.14 | ||
| CodeUri: src/ | ||
|
|
||
| # Durable Lambda function that orchestrates the workflow | ||
| DurableLambdaFunction: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| Handler: durable_lambda_function.lambda_handler | ||
| Runtime: python3.14 | ||
| CodeUri: src/ | ||
| Timeout: 600 | ||
| DurableConfig: | ||
| ExecutionTimeout: 600 | ||
| RetentionPeriodInDays: 7 | ||
| AutoPublishAlias: live | ||
| Environment: | ||
| Variables: | ||
| PROCESSOR_FUNCTION_NAME: !Ref ProcessorFunction | ||
| Policies: | ||
| - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicDurableExecutionRolePolicy | ||
| - LambdaInvokePolicy: | ||
| FunctionName: !Ref ProcessorFunction | ||
|
|
||
| Outputs: | ||
| DurableLambdaFunctionArn: | ||
| Description: ARN of the durable Lambda function | ||
| Value: !GetAtt DurableLambdaFunction.Arn | ||
| DurableLambdaFunctionAliasArn: | ||
| Description: Alias ARN of the durable Lambda function (use this for invocations) | ||
| Value: !Ref DurableLambdaFunction.Alias | ||
| ProcessorFunctionArn: | ||
| Description: ARN of the processor Lambda function | ||
| Value: !GetAtt ProcessorFunction.Arn |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure to use the correct capitalization and order: It's Lambda durable functions not Lambda Durable Functions.
And e.g. "A durable Lambda function invokes".
Can you pls double check your PR? It needs correction on a couple of places.