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 for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
- Create an AWS account 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 installed and configured
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
- Docker installed (for building Lambda container images)
- Python 3.13 or later
-
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 -
Change directory to the pattern directory:
cd lambda-ecs-durable-python-sam -
From the command line, use AWS SAM to build the application:
sam build -
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 -
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 --guidedmode once and saved arguments to a configuration file (samconfig.toml), you can usesam deployin future to use these defaults. -
Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
This pattern implements an ECS task orchestration workflow using Lambda durable functions with callback pattern:
- Sync Lambda starts an ECS task and polls for completion using durable waits (no compute charges during waits)
- Callback Lambda starts an ECS task, pauses execution using
callback.result(), and waits for a callback - The ECS task processes work and calls Lambda durable execution callback API when complete
- 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.
- 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
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}')# 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 --followA successful sync test shows these log messages:
- ECS task logs:
[SYNC] Completed: {"status": "success", ...} - Lambda logs:
Durable execution completedwith the ECS task result
# 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 --followA successful callback test shows these log messages:
- Lambda logs:
Callback createdfollowed byWaiting for callback from ECS task - ECS task logs:
[CALLBACK] Success callback sent! - Lambda logs:
Callback receivedfollowed byDurable execution completedwith the ECS task result
- Delete the stack
sam delete
- Confirm the stack has been deleted
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