Skip to content

Latest commit

 

History

History
142 lines (109 loc) · 6.18 KB

File metadata and controls

142 lines (109 loc) · 6.18 KB

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 for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

Requirements

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
    
  2. Change directory to the pattern directory:

    cd lambda-ecs-durable-python-sam
    
  3. From the command line, use AWS SAM to build the application:

    sam build
    
  4. 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
    
  5. 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.

  6. 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

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

# 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

# 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
    sam delete
  2. 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