generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 1k
Lambda ecs durable python sam #2953
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
4D54
wants to merge
7
commits into
aws-samples:main
Choose a base branch
from
4D54:lambda-ecs-durable-python-sam
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
7 commits
Select commit
Hold shift + click to select a range
c8075d1
Add Step Functions to ECS Python integration pattern
4D54 ce2829f
Add security note to README
4D54 5c2a130
feat: Replace Step Functions pattern with Lambda Durable Functions pa…
4D54 2dff793
fix: Replace DynamoDB callback with Lambda durable execution callback…
4D54 f5ad4a6
fix: Address PR review comments
4D54 ba19a31
publishing file
ellisms f2f5153
tagging
ellisms 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,142 @@ | ||
| # AWS Lambda durable functions to Amazon ECS with Python | ||
|
|
||
| This pattern demonstrates how to invoke Amazon ECS tasks from AWS Lambda durable functions using Python. The workflow starts an ECS task, waits for a callback, and resumes based on the task result while maintaining state across the pause/resume cycle. | ||
|
|
||
| Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-ecs-python-sam | ||
|
|
||
| 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 | ||
| * [Docker](https://docs.docker.com/get-docker/) installed (for building Lambda container images) | ||
| * [Python 3.13](https://www.python.org/downloads/) or later | ||
|
|
||
| ## 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-ecs-durable-python-sam | ||
| ``` | ||
| 1. From the command line, use AWS SAM to build the application: | ||
| ``` | ||
| sam build | ||
| ``` | ||
| 1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yaml file: | ||
| ``` | ||
| sam deploy --guided | ||
| ``` | ||
| 1. During the prompts: | ||
| * Enter a stack name | ||
| * Enter the desired AWS Region | ||
| * Enter the VpcCIDR parameter (default: 10.0.0.0/16) | ||
| * Allow SAM CLI to create IAM roles with the required permissions. | ||
| * Create managed ECR repositories for all functions (required for container images) | ||
|
|
||
| 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 implements an ECS task orchestration workflow using Lambda durable functions with callback pattern: | ||
|
|
||
| 1. **Sync Lambda** starts an ECS task and polls for completion using durable waits (no compute charges during waits) | ||
| 2. **Callback Lambda** starts an ECS task, pauses execution using `callback.result()`, and waits for a callback | ||
| 3. The ECS task processes work and calls Lambda durable execution callback API when complete | ||
| 4. The Lambda function resumes automatically when the callback is invoked and returns the result | ||
|
|
||
| The pattern uses the AWS Durable Execution SDK for Python with the `@durable_execution` decorator to maintain state across the pause/resume cycle. The callback pattern ensures no compute charges while waiting for ECS task completion. | ||
|
|
||
| ### Architecture Components | ||
|
|
||
| - **Sync Lambda**: Orchestrates ECS tasks using Lambda durable functions SDK with polling pattern and durable waits | ||
| - **Callback Lambda**: Orchestrates ECS tasks using Lambda durable functions SDK with callback pattern | ||
| - **ECS Tasks**: Process work and send callbacks to Lambda using durable execution callback APIs | ||
| - **VPC and Networking**: Provides network connectivity for ECS tasks to pull Docker images and call AWS APIs | ||
| - **CloudWatch Logs**: Stores execution logs for Lambda functions and ECS tasks | ||
|
|
||
| ## Testing | ||
|
|
||
| ### Set Environment Variables | ||
|
|
||
| ```bash | ||
| export AWS_DEFAULT_REGION=us-east-1 | ||
| export STACK_NAME=<your-stack-name> | ||
|
|
||
| # Get function names from CloudFormation outputs | ||
| export SYNC_FUNCTION=$(aws cloudformation describe-stacks \ | ||
| --stack-name $STACK_NAME \ | ||
| --query 'Stacks[0].Outputs[?OutputKey==`SyncLambdaFunctionArn`].OutputValue' \ | ||
| --output text | awk -F: '{print $NF}') | ||
|
|
||
| export CALLBACK_FUNCTION=$(aws cloudformation describe-stacks \ | ||
| --stack-name $STACK_NAME \ | ||
| --query 'Stacks[0].Outputs[?OutputKey==`CallbackLambdaFunctionArn`].OutputValue' \ | ||
| --output text | awk -F: '{print $NF}') | ||
| ``` | ||
|
|
||
| ### Test Synchronous Pattern | ||
|
|
||
| ```bash | ||
| # Invoke the sync function (must use qualified ARN with :$LATEST) | ||
| aws lambda invoke \ | ||
| --function-name $SYNC_FUNCTION:\$LATEST \ | ||
| --invocation-type Event \ | ||
| --cli-binary-format raw-in-base64-out \ | ||
| --payload '{"message": "Hello from sync pattern", "processingTime": 10}' \ | ||
| response.json | ||
|
|
||
| # Monitor ECS task logs | ||
| aws logs tail /ecs/$STACK_NAME --follow | ||
| ``` | ||
|
|
||
| A successful sync test shows these log messages: | ||
| - ECS task logs: `[SYNC] Completed: {"status": "success", ...}` | ||
| - Lambda logs: `Durable execution completed` with the ECS task result | ||
|
|
||
| ### Test Callback Pattern | ||
|
|
||
| ```bash | ||
| # Invoke the callback function (must use qualified ARN with :$LATEST) | ||
| aws lambda invoke \ | ||
| --function-name $CALLBACK_FUNCTION:\$LATEST \ | ||
| --invocation-type Event \ | ||
| --cli-binary-format raw-in-base64-out \ | ||
| --payload '{"message": "Hello from callback pattern", "processingTime": 30}' \ | ||
| response.json | ||
|
|
||
| # Monitor Lambda logs | ||
| aws logs tail /aws/lambda/$CALLBACK_FUNCTION --follow | ||
|
|
||
| # Monitor ECS task logs | ||
| aws logs tail /ecs/$STACK_NAME --follow | ||
| ``` | ||
|
|
||
| A successful callback test shows these log messages: | ||
| - Lambda logs: `Callback created` followed by `Waiting for callback from ECS task` | ||
| - ECS task logs: `[CALLBACK] Success callback sent!` | ||
| - Lambda logs: `Callback received` followed by `Durable execution completed` with the ECS task result | ||
|
|
||
| ## Cleanup | ||
|
|
||
| 1. Delete the stack | ||
| ```bash | ||
| sam delete | ||
| ``` | ||
| 1. Confirm the stack has been deleted | ||
| ```bash | ||
| aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'$STACK_NAME')].StackStatus" | ||
| ``` | ||
|
|
||
| ---- | ||
| Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: MIT-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,68 @@ | ||
| { | ||
| "title": "AWS Lambda Durable Functions to Amazon ECS with Python", | ||
| "description": "Invoke ECS tasks from Lambda durable functions with automatic checkpointing, state management, and resilient execution patterns", | ||
| "language": "Python", | ||
| "level": "300", | ||
| "framework": "SAM", | ||
| "introBox": { | ||
| "headline": "How it works", | ||
| "text": [ | ||
| "This pattern demonstrates AWS Lambda durable functions invoking Amazon ECS tasks with resilient, long-running execution capabilities:", | ||
| "1. Durable Synchronous Pattern: Lambda uses checkpointed steps and durable waits to poll ECS task status. Can run for up to 1 year with automatic recovery from failures. No compute charges during wait periods.", | ||
| "2. Durable Callback Pattern: Lambda uses checkpointed steps to reliably initiate ECS tasks. Each step (create record, start task, update status) is automatically checkpointed for guaranteed execution.", | ||
| "The pattern uses the AWS Durable Execution SDK for Python, providing automatic state management, checkpoint-based recovery, and cost-effective long-running workflows. Includes inline Python code in ECS containers, VPC networking, and DynamoDB for callback tracking." | ||
| ] | ||
| }, | ||
| "gitHub": { | ||
| "template": { | ||
| "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-ecs-python-sam", | ||
| "templateURL": "serverless-patterns/lambda-ecs-python-sam", | ||
| "projectFolder": "lambda-ecs-python-sam", | ||
| "templateFile": "template.yaml" | ||
| } | ||
| }, | ||
| "resources": { | ||
| "bullets": [ | ||
| { | ||
| "text": "Lambda durable functions", | ||
| "link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html" | ||
| }, | ||
| { | ||
| "text": "Durable Execution SDK", | ||
| "link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-execution-sdk.html" | ||
| }, | ||
| { | ||
| "text": "Run Amazon ECS or Fargate tasks", | ||
| "link": "https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs_run_task.html" | ||
| }, | ||
| { | ||
| "text": "Amazon ECS Task Definitions", | ||
| "link": "https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definitions.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": "Mian Tariq", | ||
| "image": "", | ||
| "bio": "Senior Delivery Consultant", | ||
| "linkedin": "mian-tariq" | ||
| } | ||
| ] | ||
| } | ||
87 changes: 87 additions & 0 deletions
87
lambda-ecs-durable-python-sam/lambda-ecs-durable-python-sam.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,87 @@ | ||
| { | ||
| "title": "AWS Lambda durable functions to Amazon ECS with Python", | ||
| "description": "Invoke ECS tasks from Lambda durable functions with automatic checkpointing, state management, and resilient execution patterns", | ||
| "language": "Python", | ||
| "level": "300", | ||
| "framework": "AWS SAM", | ||
| "introBox": { | ||
| "headline": "How it works", | ||
| "text": [ | ||
| "This pattern demonstrates AWS Lambda durable functions invoking Amazon ECS tasks with resilient, long-running execution capabilities:", | ||
| "1. Durable Synchronous Pattern: Lambda uses checkpointed steps and durable waits to poll ECS task status. Can run for up to 1 year with automatic recovery from failures. No compute charges during wait periods.", | ||
| "2. Durable Callback Pattern: Lambda uses checkpointed steps to reliably initiate ECS tasks. Each step (create record, start task, update status) is automatically checkpointed for guaranteed execution.", | ||
| "The pattern uses the AWS Durable Execution SDK for Python, providing automatic state management, checkpoint-based recovery, and cost-effective long-running workflows. Includes inline Python code in ECS containers, and VPC networking," | ||
| ] | ||
| }, | ||
| "gitHub": { | ||
| "template": { | ||
| "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-ecs-durable-python-sam", | ||
| "templateURL": "serverless-patterns/lambda-ecs-durable-python-sam", | ||
| "projectFolder": "lambda-ecs-durable-python-sam", | ||
| "templateFile": "template.yaml" | ||
| } | ||
| }, | ||
| "resources": { | ||
| "bullets": [ | ||
| { | ||
| "text": "Lambda durable functions", | ||
| "link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html" | ||
| }, | ||
| { | ||
| "text": "Durable Execution SDK", | ||
| "link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-execution-sdk.html" | ||
| }, | ||
| { | ||
| "text": "Run Amazon ECS or Fargate tasks", | ||
| "link": "https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs_run_task.html" | ||
| }, | ||
| { | ||
| "text": "Amazon ECS Task Definitions", | ||
| "link": "https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definitions.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": "Mian Tariq", | ||
| "image": "", | ||
| "bio": "Senior Delivery Consultant", | ||
| "linkedin": "mian-tariq" | ||
| } | ||
| ], | ||
| "patternArch": { | ||
| "icon1": { | ||
| "x": 20, | ||
| "y": 50, | ||
| "service": "lambda", | ||
| "label": "AWS Lambda durable function" | ||
| }, | ||
| "icon2": { | ||
| "x": 80, | ||
| "y": 50, | ||
| "service": "ecs", | ||
| "label": "Amazon ECS" | ||
| }, | ||
| "line1": { | ||
| "from": "icon1", | ||
| "to": "icon2", | ||
| "label": "Sync or Callback" | ||
| } | ||
| } | ||
| } |
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,14 @@ | ||
| FROM public.ecr.aws/lambda/python:3.13 | ||
|
|
||
| # Copy requirements file | ||
| COPY requirements.txt ${LAMBDA_TASK_ROOT}/ | ||
|
|
||
| # Install dependencies including durable SDK | ||
| RUN pip install -r requirements.txt | ||
|
|
||
| # Copy function code | ||
| COPY sync_handler.py ${LAMBDA_TASK_ROOT}/ | ||
| COPY callback_handler.py ${LAMBDA_TASK_ROOT}/ | ||
|
|
||
| # Default handler (will be overridden by template) | ||
| CMD [ "sync_handler.lambda_handler" ] |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.