Stream structured LLM responses from Bedrock Claude through AWS API Gateway REST, Lambda, and Middy with deferred observability using TransformStream flush(). Built with AI SDK v6.
Companion code for a four-part series on dev.to:
- Streaming LLM Responses Through API Gateway REST with Lambda
- The Middy After Hook Problem
- Deferred Observability with TransformStream flush()
- Complete CDK Stack with Tests
Each step builds on the previous one. Check the PRs to see the delta:
| Branch | PR | Description |
|---|---|---|
step-1/api-gateway-streaming |
#1 | CDK stack, Lambda handler, AI SDK streaming |
step-2/stream-error-handling |
#2 | AbortError classification for streaming |
step-3/transformstream-observability |
#3 | TransformStream pipeline for deferred observability |
step-4/complete-cdk-stack |
#4 | Test suite and production readiness |
npm install
npx cdk deploy- Node.js 22+
- AWS CLI configured
- AWS CDK CLI
- Bedrock model access enabled for Claude 3.5 Sonnet v2
- TransformStream Pipeline: WHATWG
flush()as deferred execution point after stream completion - Dual Middleware Bridge: In-memory store connecting AI SDK and Middy middleware systems
- Deferred Observability: Attach logging to the data pipeline, not the HTTP lifecycle
MIT