generated from amazon-archives/__template_MIT-0
-
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
Open
jojo786
wants to merge
6
commits into
aws-samples:main
Choose a base branch
from
jojo786:jojo786-feature-apigw-restapi-lambda-python-response-streaming
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.
+302
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
55ba8e7
API Gateway REST API to AWS Lambda Python function with response stre…
jojo786 3d43729
multiple review changes
jojo786 38ce845
removed APIGW logging
jojo786 e7cc326
removed markdown links and added notice of lack of API auth
jojo786 d1b1667
Add architecture diagram
bfreiberg 58f787e
Fix repo name
bfreiberg 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,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 | ||
99 changes: 99 additions & 0 deletions
99
...da-python-response-streaming/apigw-rest-api-lambda-python-response-streaming-pattern.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,99 @@ | ||
| { | ||
| "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-rest-api-lambda-python-response-streaming", | ||
| "templateURL": "serverless-patterns/apigw-rest-api-lambda-python-response-streaming", | ||
| "projectFolder": "apigw-rest-api-lambda-python-response-streaming", | ||
| "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" | ||
| } | ||
| ], | ||
| "patternArch": { | ||
| "icon1": { | ||
| "x": 20, | ||
| "y": 50, | ||
| "service": "apigw", | ||
| "label": "API Gateway REST API" | ||
| }, | ||
| "icon2": { | ||
| "x": 50, | ||
| "y": 50, | ||
| "service": "lambda", | ||
| "label": "AWS Lambda" | ||
| }, | ||
| "icon3": { | ||
| "x": 80, | ||
| "y": 50, | ||
| "service": "bedrock", | ||
| "label": "Amazon Bedrock" | ||
| }, | ||
| "line1": { | ||
| "from": "icon1", | ||
| "to": "icon2", | ||
| "label": "" | ||
| }, | ||
| "line2": { | ||
| "from": "icon2", | ||
| "to": "icon3", | ||
| "label": "" | ||
| } | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
apigw-rest-api-lambda-python-response-streaming/src/app.py
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,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 |
4 changes: 4 additions & 0 deletions
4
apigw-rest-api-lambda-python-response-streaming/src/requirements.txt
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,4 @@ | ||
| boto3 | ||
| fastapi | ||
| pydantic | ||
| uvicorn |
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,3 @@ | ||
| #!/bin/bash | ||
|
|
||
| PATH=$PATH:$LAMBDA_TASK_ROOT/bin PYTHONPATH=$PYTHONPATH:/opt/python:$LAMBDA_RUNTIME_DIR exec python -m uvicorn --port=$PORT app:app |
74 changes: 74 additions & 0 deletions
74
apigw-rest-api-lambda-python-response-streaming/template.yaml
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,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 | ||
bfreiberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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' | ||
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.