- Newest
- Most votes
- Most comments
Unfortunately, REST API Gateway does not natively support streaming responses or chunked transfer encoding. The behavior you're experiencing, where the response is returned all at once, is the default for REST API Gateway.
However, you have a few options to achieve streaming-like behavior:
1. HTTP API Gateway: Switching to HTTP API Gateway could be a solution. HTTP API Gateway supports response streaming for certain integration types, particularly when integrated with AWS Lambda functions that are configured for response streaming. If your ECS task can be adapted to work with Lambda, this could be a viable option. This is not correct. HTTP API does not support response streaming.
-
Proxy integration: If you're not already using it, you could set up a proxy integration in your REST API Gateway. This would allow the response to pass through from your ECS application without modification by API Gateway. However, this still might not solve the streaming issue entirely, as API Gateway may buffer the response.
-
WebSocket API: While you mentioned preferring not to use WebSocket API Gateway, it is worth noting that this would be the most straightforward way to implement true streaming behavior.
-
Custom solution: You could implement a custom streaming solution at the application level. This might involve breaking your response into smaller chunks and having the client reassemble them, or implementing long-polling techniques.
-
Direct access: If possible, you could consider exposing your ECS service directly (with appropriate security measures) and bypass API Gateway altogether for this specific streaming endpoint.
It's important to note that even if you manage to get chunked transfer encoding working, API Gateway has certain timeout limits that might interfere with long-running streams.
Given your requirements and current setup, switching to HTTP API Gateway and adapting your backend to work with Lambda's response streaming capabilities might be the most suitable option if you want to maintain an API Gateway architecture while enabling streaming responses.
Remember to thoroughly test any changes, as switching from REST to HTTP API Gateway may have other implications for your overall system architecture.
Sources
Gateway responses for REST APIs in API Gateway - Amazon API Gateway
Methods for REST APIs in API Gateway - Amazon API Gateway
Receive an API response with a compressed payload in API Gateway - Amazon API Gateway
Writing response streaming-enabled Lambda functions - AWS Lambda
Hi,
You will get all the details about streaming LLM answers through API gateway with WebSocket in this very complete blog post: https://amlanscloud.com/llmstreampost/
Please, follow its guidance to get your use case to work.
Associated repo with code is here: https://github.com/amlana21/llm-stream-publish
If you want to implement your resources via CloudFormation (I always do), this one will help: https://serverlessland.com/patterns/apigw-websocket-api-bedrock-streaming
Best,
Didier
Relevant content
- Accepted Answerasked 2 years ago
- AWS OFFICIALUpdated 8 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- How do I use an interface VPC endpoint to access an API Gateway private REST API in another account?AWS OFFICIALUpdated a year ago
What integration types does HTTP API Gateway support for streaming. I do not want to make my app a lambda. This would be very inconvenient