-
Notifications
You must be signed in to change notification settings - Fork 1k
New Feature: API Gateway REST API to AWS Lambda Python function with response streaming #2865
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Amazon Gateway REST API to AWS Lambda Python function with response streaming | ||
|
|
||
| This pattern demonstrates how to use an Amazon API Gateway REST API with response streaming to an AWS Lambda Python function. | ||
|
|
||
| Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-rest-api-lambda-python-response-streaming | ||
|
|
||
| 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 | ||
| * [Python 3.14 installed](https://www.python.org/downloads/) | ||
|
|
||
| ## 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 apigw-restapi-lambda-python-response-streaming | ||
| ``` | ||
| 1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: | ||
| ``` | ||
| sam deploy --guided | ||
| ``` | ||
| 1. During the prompts: | ||
| * Enter a stack name | ||
| * Enter the desired AWS Region | ||
| * Allow SAM CLI to create IAM roles with the required permissions | ||
| * Allow API Gateway API without any authentication | ||
|
|
||
| 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 | ||
|
|
||
| Response streaming allows you to incrementally stream responses back to clients rather than waiting for the entire response to be buffered first, reducing Time to First Byte, and making your applications more responsive to users. [Amazon API Gateway REST APIs support response streaming](https://aws.amazon.com/blogs/compute/building-responsive-apis-with-amazon-api-gateway-response-streaming/). | ||
| [AWS Lambda supports response streaming natively for Nodejs](https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/) (native support for [Python in the Lambda roadmap](https://github.com/orgs/aws/projects/286/views/1?pane=issue&itemId=129507898&issue=aws%7Caws-lambda-roadmap%7C39)), so to enable response streaming with an AWS Lambda Python function, we use [Lambda Web Adaptor](https://aws.amazon.com/blogs/compute/using-response-streaming-with-aws-lambda-web-adapter-to-optimize-performance/) and [Fast API](https://github.com/awslabs/aws-lambda-web-adapter/tree/main/examples/fastapi-response-streaming-zip). The Lambda function takes the topic from the API Gateway request, and sends a request to Bedrock, using the `InvokeModelWithResponseStream` call, to generate a bedtime story for that topic. | ||
|
|
||
| ## Testing | ||
|
|
||
| To test response streaming, you can call the API Gateway REST API URL included in the SAM output. You may use `curl` with the `no-buffer` parameter to send in a topic, to which you will receive a bedtime story for. E.g. | ||
| ``` | ||
| curl --no-buffer --json '{"topic":"response streaming with AWS serverless"}' https://<abc123.execute-api.us-eas>t-1.amazonaws.com/prod/story | ||
| ``` | ||
|
|
||
| ## 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 | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,69 @@ | ||||||
| { | ||||||
| "title": "Response streaming with Amazon API Gateway Rest API and AWS Lambda", | ||||||
| "description": "Creates an API Gateway REST API and Python Lambda function that provides a streaming response from the LLMs in Amazon Bedrock.", | ||||||
| "language": "Python", | ||||||
| "level": "200", | ||||||
| "framework": "AWS SAM", | ||||||
| "introBox": { | ||||||
| "headline": "How it works", | ||||||
| "text": [ | ||||||
| "This pattern demonstrates how to use an Amazon API Gateway REST API with response streaming to a AWS Lambda Python function.", | ||||||
| "To enable response streaming with a Lambda Python function, we use Lambda Web Adapter and Fast API." | ||||||
| ] | ||||||
| }, | ||||||
| "gitHub": { | ||||||
| "template": { | ||||||
| "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-restapi-lambda-python-response-streaming", | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| "templateURL": "serverless-patterns/apigw-restapi-lambda-python-response-streaming", | ||||||
| "projectFolder": "apigw-restapi-lambda-python-response-streaming", | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| "templateFile": "template.yaml" | ||||||
| } | ||||||
| }, | ||||||
| "resources": { | ||||||
| "bullets": [ | ||||||
| { | ||||||
| "text": "Responsive APIs with Amazon API Gateway REST API", | ||||||
| "link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/response-transfer-mode.html" | ||||||
| }, | ||||||
| { | ||||||
| "text": "AWS Lambda response streaming with Python and Lambda Web Adapter", | ||||||
| "link": "https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html" | ||||||
| }, | ||||||
| { | ||||||
| "text": "Building responsive APIs with Amazon API Gateway response streaming", | ||||||
| "link": "https://aws.amazon.com/blogs/compute/building-responsive-apis-with-amazon-api-gateway-response-streaming/" | ||||||
| }, | ||||||
| { | ||||||
| "text": "Using response streaming with AWS Lambda Web Adapter to optimize performance", | ||||||
| "link": "https://aws.amazon.com/blogs/compute/using-response-streaming-with-aws-lambda-web-adapter-to-optimize-performance/" | ||||||
| }, | ||||||
| { | ||||||
| "text": "FastAPI Response Streaming", | ||||||
| "link": "https://github.com/awslabs/aws-lambda-web-adapter/tree/main/examples/fastapi-response-streaming-zip" | ||||||
| } | ||||||
| ] | ||||||
| }, | ||||||
| "deploy": { | ||||||
| "text": [ | ||||||
| "sam deploy" | ||||||
| ] | ||||||
| }, | ||||||
| "testing": { | ||||||
| "text": [ | ||||||
| "See the GitHub repo for detailed testing instructions." | ||||||
| ] | ||||||
| }, | ||||||
| "cleanup": { | ||||||
| "text": [ | ||||||
| "Delete the stack: <code>sam delete</code>." | ||||||
| ] | ||||||
| }, | ||||||
| "authors": [ | ||||||
| { | ||||||
| "name": "Yusuf Mayet", | ||||||
| "image": "https://d2908q01vomqb2.cloudfront.net/9e6a55b6b4563e652a23be9d623ca5055c356940/2021/11/24/Yusuf-mayet-aws.jpg", | ||||||
| "bio": "I am a Solutions Architect at AWS, where I help customers realise that true transformation lies at the intersection of Cloud, DevOps cultural practices, Agile principles, modular and scalable architectures, and efficient team structures.", | ||||||
| "linkedin": "yusufmayet" | ||||||
| } | ||||||
| ] | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: MIT-0 | ||
|
|
||
| import boto3 | ||
| import json | ||
| import os | ||
| import uvicorn | ||
| from fastapi import FastAPI, Request | ||
| from fastapi.responses import StreamingResponse | ||
|
|
||
| app = FastAPI() #specified in run.sh | ||
| bedrock = boto3.client('bedrock-runtime') | ||
|
|
||
|
|
||
| @app.post("/story") | ||
| async def api_story(request: Request): | ||
| body = await request.json() | ||
| topic = body.get("topic") | ||
| print(f"Topic received: {topic}") | ||
| return StreamingResponse(bedrock_stream(topic), media_type="text/html") | ||
|
|
||
|
|
||
| def bedrock_stream(topic: str): | ||
| instruction = f""" | ||
| You are a world class writer. Please write a sweet bedtime story about {topic}. | ||
| """ | ||
| body = json.dumps({ | ||
| "anthropic_version": "bedrock-2023-05-31", | ||
| "max_tokens": 1024, | ||
| "messages": [ | ||
| { | ||
| "role": "user", | ||
| "content": instruction, | ||
| } | ||
| ], | ||
| }) | ||
|
|
||
| response = bedrock.invoke_model_with_response_stream( | ||
| modelId='global.anthropic.claude-sonnet-4-5-20250929-v1:0', #using Global CRIS Anthropic Claude Sonnet 4.5 on Bedrock | ||
| body=body | ||
| ) | ||
|
|
||
| stream = response.get('body') | ||
| if stream: | ||
| for event in stream: | ||
| chunk = event.get('chunk') | ||
| if chunk: | ||
| message = json.loads(chunk.get("bytes").decode()) | ||
| if message['type'] == "content_block_delta": | ||
| yield message['delta']['text'] or "" | ||
| elif message['type'] == "message_stop": | ||
| yield "\n" | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", "8080"))) #port specified in run.sh |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| boto3 | ||
| fastapi | ||
| pydantic | ||
| uvicorn |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #!/bin/bash | ||
|
|
||
| PATH=$PATH:$LAMBDA_TASK_ROOT/bin PYTHONPATH=$PYTHONPATH:/opt/python:$LAMBDA_RUNTIME_DIR exec python -m uvicorn --port=$PORT app:app |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| AWSTemplateFormatVersion: '2010-09-09' | ||
| Transform: AWS::Serverless-2016-10-31 | ||
| Description: | | ||
| A sample SAM template for streaming Bedrock responses with Lambda Response Streaming and Lambda Web Adapter. | ||
| This template includes an response streaming enabled Amazon API Gateway REST API that invokes an AWS Lambda Python function | ||
| that uses Lambda Web Adapter with Fast API to enable response streaming. | ||
|
|
||
| Resources: | ||
| StreamingFunction: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| Environment: | ||
| Variables: | ||
| AWS_LAMBDA_EXEC_WRAPPER: /opt/bootstrap #required for Lambda Web Adapter | ||
| PORT: 8080 #sets the port to be used in run.sh, for Lambda Web Adapter | ||
| AWS_LWA_INVOKE_MODE: RESPONSE_STREAM #set Lambda Web Adapter to enable response streaming for Lambda functions | ||
| CodeUri: src/ | ||
| Handler: run.sh #required for the Lambda Web Adapter | ||
| Runtime: python3.14 | ||
| Architectures: | ||
| - arm64 #AWS Graviton for better price performance | ||
| Timeout: 60 | ||
| Tracing: Active | ||
| Layers: | ||
| - !Sub arn:aws:lambda:${AWS::Region}:753240598075:layer:LambdaAdapterLayerArm64:25 #Lambda Web Adapter Layer for arm64/graviton | ||
| Policies: | ||
| - Statement: | ||
| - Effect: Allow | ||
| Action: | ||
| - bedrock:InvokeModelWithResponseStream #Lambda function permission to call Bedrock with response streaming | ||
| Resource: | ||
| - !Sub 'arn:aws:bedrock:${AWS::Region}::foundation-model/anthropic.claude-sonnet-4-5-20250929-v1:0' | ||
| - !Sub 'arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:inference-profile/global.anthropic.claude-sonnet-4-5-20250929-v1:0' | ||
| - !Sub 'arn:aws:bedrock:::foundation-model/anthropic.claude-sonnet-4-5-20250929-v1:0' | ||
| Events: #connects the Lambda function to the API Gateway StreamingAPI | ||
| StreamingApi: | ||
| Type: Api | ||
| Properties: | ||
| RestApiId: !Ref StreamingApi | ||
| Path: /story | ||
| Method: post | ||
|
|
||
| StreamingApi: | ||
| Type: AWS::Serverless::Api | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The API has no authentication mechanism, allowing unrestricted public access to Bedrock.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I believe most of the other apigw-lambda patterns also dont use any auth, as auth is a seperate concern and pattern that can be applied
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then please include a blurb about this being a pattern for demo purposes and not for production use |
||
| Description: | | ||
| Please note: This API does not include authentication and should not be used in production. | ||
| For securing API Gateway APIs, refer to other serverless patterns for authentication methods. | ||
| Properties: | ||
| StageName: prod | ||
| DefinitionBody: | ||
| openapi: 3.0.1 | ||
| info: | ||
| title: Streaming API | ||
| version: 1.0.0 | ||
| paths: | ||
| /story: | ||
| post: | ||
| responses: | ||
| '200': | ||
| description: Success | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: object | ||
| x-amazon-apigateway-integration: | ||
| type: aws_proxy | ||
| httpMethod: POST | ||
| responseTransferMode: "STREAM" #enable response streaming on API Gateway | ||
| uri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2021-11-15/functions/${StreamingFunction.Arn}/response-streaming-invocations' #the ARN for Lamba is different for response streaming | ||
|
|
||
| Outputs: | ||
| StreamingApiUrl: | ||
| Description: API Gateway endpoint URL | ||
| Value: !Sub 'https://${StreamingApi}.execute-api.${AWS::Region}.amazonaws.com/prod/story' | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't you also need Python installed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added python3.14 as a requirement