|
| 1 | +# Lambda durable functions invoking AWS Lambda function (Python) |
| 2 | + |
| 3 | +This pattern demonstrates how an AWS Lambda durable function can invoke a standard AWS Lambda function using `context.invoke()` from the 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. |
| 4 | + |
| 5 | +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) |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +* [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. |
| 12 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 13 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 14 | +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 15 | +* Python 3.14 |
| 16 | + |
| 17 | +## Deployment Instructions |
| 18 | + |
| 19 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 20 | + ``` |
| 21 | + git clone https://github.com/aws-samples/serverless-patterns |
| 22 | + ``` |
| 23 | +1. Change directory to the pattern directory: |
| 24 | + ``` |
| 25 | + cd lambda-durable-invoke-lambda-sam-python |
| 26 | + ``` |
| 27 | +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: |
| 28 | + ``` |
| 29 | + sam build |
| 30 | + sam deploy --guided |
| 31 | + ``` |
| 32 | +1. During the prompts: |
| 33 | + * Enter a stack name |
| 34 | + * Enter the desired AWS Region (durable functions are available in supported regions) |
| 35 | + * Allow SAM CLI to create IAM roles with the required permissions. |
| 36 | +
|
| 37 | + 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. |
| 38 | +
|
| 39 | +1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 40 | +
|
| 41 | +## How it works |
| 42 | +
|
| 43 | +This pattern deploys two Lambda functions: |
| 44 | +
|
| 45 | +1. **DurableLambdaFunction** - A durable Lambda function that orchestrates the workflow. It uses the `@durable_execution` decorator and performs two checkpointed operations: |
| 46 | + - A `@durable_step` that prepares and validates input values. |
| 47 | + - A `context.invoke()` call that invokes the ProcessorFunction and waits for its result. |
| 48 | +
|
| 49 | +2. **ProcessorFunction** - A standard Lambda function that receives a list of numeric values and returns computed statistics (sum, average, max, min). |
| 50 | +
|
| 51 | +The AWS Lambda 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. |
| 52 | +
|
| 53 | +
|
| 54 | +## Testing |
| 55 | +
|
| 56 | +1. After deployment, invoke the durable function using the alias ARN from the stack outputs: |
| 57 | +
|
| 58 | + ```bash |
| 59 | + aws lambda invoke \ |
| 60 | + --function-name <DurableLambdaFunctionAliasArn> \ |
| 61 | + --payload '{"values": [10, 20, 30, 40, 50]}' \ |
| 62 | + --cli-binary-format raw-in-base64-out \ |
| 63 | + output.json |
| 64 | + ``` |
| 65 | +
|
| 66 | +2. Check the response: |
| 67 | +
|
| 68 | + ```bash |
| 69 | + cat output.json |
| 70 | + ``` |
| 71 | +
|
| 72 | + Expected output: |
| 73 | + ```json |
| 74 | + {"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}}"} |
| 75 | + ``` |
| 76 | +
|
| 77 | +3. Monitor the durable execution steps in the Lambda console under the **Durable executions** tab of the DurableLambdaFunction. |
| 78 | +
|
| 79 | +## Cleanup |
| 80 | +
|
| 81 | +1. Delete the stack |
| 82 | + ```bash |
| 83 | + sam delete |
| 84 | + ``` |
| 85 | +
|
| 86 | +---- |
| 87 | +Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 88 | +
|
| 89 | +SPDX-License-Identifier: MIT-0 |
0 commit comments