Skip to content

Latest commit

 

History

History
115 lines (86 loc) · 4.52 KB

File metadata and controls

115 lines (86 loc) · 4.52 KB

Lambda Durable Function - REST API Call with Python

This pattern demonstrates a Lambda durable function that calls an external REST API using Python.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-durable-rest-api-sam-py

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-durable-rest-api-sam-py
    
  3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:

    sam build
    sam deploy --guided
    
  4. During the prompts:

    • Enter a stack name
    • Enter the desired AWS Region
    • 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.

  5. 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 demonstrates AWS Lambda durable Execution for calling external REST APIs. The function uses two key components:

  1. @durable_step - Wraps the REST API call, making it automatically retryable
  2. @durable_execution - Marks the Lambda handler as a durable execution workflow

AWS Lambda durable Execution automatically handles failures, retries, and state persistence without requiring external services like DynamoDB or Step Functions.

Testing

  1. Get the function name from the stack outputs:
aws cloudformation describe-stacks --stack-name <your-stack-name> \
  --query 'Stacks[0].Outputs[?OutputKey==`FunctionName`].OutputValue' --output text
  1. Invoke the function with default URL:
aws lambda invoke \
  --function-name <function-name> \
  --payload '{}' \
  response.json && cat response.json
  1. Invoke with a custom URL:
aws lambda invoke \
  --function-name <function-name> \
  --payload '{"url": "https://jsonplaceholder.typicode.com/users/1"}' \
  response.json && cat response.json

Example JSON Lambda test event:

{
  "url": "https://jsonplaceholder.typicode.com/posts/1"
}

Expected response (success):

{
  "statusCode": 200,
  "headers": {"Content-Type": "application/json"},
  "body": "{\"message\": \"API call successful\", \"url\": \"https://jsonplaceholder.typicode.com/posts/1\", \"data\": {...}}"
}

The execution is durable - if the API call fails, AWS Lambda will automatically retry the call_rest_api step without re-executing the entire function.

Cleanup

  1. Delete the stack:
    aws cloudformation delete-stack --stack-name <your-stack-name>
  2. Confirm the stack has been deleted:
    aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'<your-stack-name>')].StackStatus"

Learn More


Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0