AWS Builder Center
Why HTTP 101 Isn't Enough: Debugging WebSockets Behind Amazon API Gateway and Private ECS

Why HTTP 101 Isn't Enough: Debugging WebSockets Behind Amazon API Gateway and Private ECS

A WebSocket connection can return HTTP 101 while the application session never starts. This hands-on case study explains why, demonstrates the correct API Gateway-to-private ECS event-driven architecture, and shares a Terraform-based AWS deployment with real verification results.

A WebSocket client connects successfully. The server responds with HTTP  101 Switching Protocols . Amazon API Gateway accepts the connection, and the backend infrastructure appears healthy.
But the application session never starts.
I encountered this architectural challenge while working with WebSocket communication between Amazon API Gateway and a private FastAPI application running on Amazon ECS Fargate.
The interesting part was that the WebSocket connection itself was successful. The problem was understanding what actually happens after API Gateway accepts that connection.
To investigate, I built an independent, open-source AWS lab that reproduces the problem, demonstrates the correct architecture, and validates the solution in AWS.
This article explains the root cause, implementation, troubleshooting approach, and lessons learned.
Note: This is a sanitized, independent educational reproduction of a real-world architectural problem. It is not a customer case study or an AWS product.

1. The Problem: HTTP 101 Does Not Guarantee Application Readiness

Consider a FastAPI application exposing a native WebSocket endpoint:
 @app.websocket("/native/{report_id}") 
When a client connects directly to a native FastAPI WebSocket server, the application receives the WebSocket upgrade request and executes the corresponding handler.
However, I was using Amazon API Gateway WebSocket API as the public entry point, with the application running privately inside Amazon ECS.
The initial assumption was that API Gateway would forward the WebSocket connection through the VPC Link and Network Load Balancer into the application's native WebSocket handler.
The expected flow was:
Client → API Gateway → VPC Link → Internal NLB → ECS → FastAPI WebSocket Handler
The client successfully connected to API Gateway, but the native FastAPI WebSocket handler was never invoked.
This raised an important question:
If the connection succeeded, why didn't the WebSocket application session start?

2. Understanding the Root Cause

Amazon API Gateway WebSocket APIs do not work as transparent, end-to-end WebSocket tunnels to HTTP backend integrations.
Instead, API Gateway manages the client WebSocket connection and routes events to backend integrations.
The architecture separates two communication paths:
Inbound communication
The client sends a WebSocket message to API Gateway. API Gateway evaluates its route selection expression and invokes the configured backend integration.
Outbound communication
The backend sends messages back to the connected client using the API Gateway Management API and the client's connection ID.
This difference is critical when integrating API Gateway WebSocket APIs with private ECS services.
A successful HTTP 101 response confirms the client established a WebSocket connection with API Gateway. It does not prove that the application-specific session was initialized inside ECS.
In my lab, the route selection expression is:
 $request.body.action 
For example, the client can send:
 {"action":"start","sessionId":"demo-001"} 
API Gateway selects the  start  route and invokes its configured HTTP integration.
The backend must handle that HTTP event, rather than expecting the native FastAPI WebSocket handler to receive the original connection.

3. The Corrected AWS Architecture

I built the following architecture to demonstrate this event-driven model:
Client ↔ Amazon API Gateway WebSocket API
For incoming backend events:
API Gateway → VPC Link V1 → Internal Network Load Balancer → ECS Fargate → FastAPI HTTP Event Adapter
For session state:
FastAPI → Amazon DynamoDB
For outbound messages:
FastAPI → API Gateway Management API → Connected WebSocket Client
The main AWS components are:
  • Amazon API Gateway WebSocket API: Maintains client connections and routes incoming messages.
  • VPC Link V1: Provides private integration connectivity to the Network Load Balancer.
  • Internal Network Load Balancer: Forwards HTTP integration requests to private ECS tasks.
  • Amazon ECS Fargate: Hosts the FastAPI event adapter without assigning a public IP address.
  • Amazon DynamoDB: Stores connection and session information, including TTL-based expiration.
  • API Gateway Management API: Delivers asynchronous messages to active clients using  PostToConnection .
  • Amazon CloudWatch: Provides logs for troubleshooting integration requests and backend events.
  • Terraform: Provisions and manages the AWS infrastructure.
One important detail is that private inbound connectivity does not automatically provide outbound connectivity.
The ECS application also needs a valid egress path to reach the API Gateway Management API.
For this demonstration, I used a NAT gateway. Production environments should evaluate VPC endpoints for supported AWS services and verify the remaining callback connectivity requirements.

4. Implementing the FastAPI Event Adapter

Instead of forwarding the WebSocket upgrade into FastAPI, I created HTTP event endpoints to handle requests from API Gateway.
The application includes:
  •  POST /proxy/connect  — processes connection events.
  •  POST /proxy/disconnect  — cleans up disconnected clients.
  •  POST /proxy/message  — processes routed application messages.
  •  GET /health  — provides application health status.
  •  /native/{report_id}  — demonstrates a direct, native FastAPI WebSocket connection for comparison.
The API Gateway integration forwards the relevant connection context, including the connection ID, domain, stage, event type, and route key.
The FastAPI adapter converts that information into a consistent internal event model.
Application logic then handles session creation, message processing, and publishing responses.
This separation is useful because the application logic does not need to depend on whether the event originated from the AWS deployment or the local gateway simulator.

5. Sending Messages Back to the WebSocket Client

Receiving an HTTP integration event does not automatically send a WebSocket application message to the client.
The backend must explicitly publish the response.
In this implementation, I used the boto3  apigatewaymanagementapi  client and its  post_to_connection  operation.
The publisher requires the following information:
  • Connection ID
  • API Gateway domain name
  • Stage
  • Response payload
The ECS task role must have appropriately scoped  execute-api:ManageConnections  permissions.
The application also handles  GoneException  when the client connection is no longer available.
Stale connection records are removed from DynamoDB rather than repeatedly attempting delivery.
This is particularly important for applications where clients may disconnect unexpectedly.

6. Building a Reproducible Local Simulation

I wanted other developers to understand this behavior without immediately provisioning chargeable AWS infrastructure.
Therefore, the GitHub project includes a local Docker Compose environment.
It contains:
  • FastAPI backend
  • Local WebSocket gateway simulator
  • React demonstration interface
  • Connection and session handling
  • Automated tests
The simulator owns the client WebSocket and invokes backend HTTP endpoints, reproducing the relevant callback pattern for educational testing.
It is explicitly a simulator and should not be treated as proof of AWS service behavior.
The project also includes a separate native FastAPI WebSocket endpoint so developers can compare both approaches.
To run the demonstration locally:
  1. Clone the GitHub repository.
  1. Run  docker compose up --build .
  1. Open  http://localhost:5173 .
  1. Connect to the local WebSocket gateway.
  1. Start a session and send a message.
  1. Execute the mock streaming demonstration.
This allows learners to inspect the event flow before deploying the infrastructure to AWS.

7. AWS Deployment and Verification Results

On November 3, 2026, I deployed and tested the complete AWS environment in the  ap-south-1  Region.
The verification included API Gateway, VPC Link, the internal Network Load Balancer, private ECS Fargate, DynamoDB, and the API Gateway Management API.
The captured test results showed:
  • VPC Link status:  AVAILABLE 
  • ECS service:  1/1  running
  • Network Load Balancer target: Healthy
  • API Gateway  $connect : Successful
  • Application  start  and  message  routes: Successful
  • Mock streaming route: Successful
  • Management API callbacks: HTTP  200 
  • Client received three ordered streaming chunks followed by  assistant.done 
  •  $disconnect  cleanup removed the corresponding DynamoDB record
The smoke test verified the complete application message flow rather than stopping at the successful WebSocket handshake.
After completing the verification, I destroyed the Terraform-managed environment to avoid unnecessary ongoing charges. The cleanup evidence records 47 resources destroyed.
The repository includes AWS Console screenshots and sanitized verification evidence.

8. Troubleshooting Lessons

This experiment reinforced several important lessons.

A successful handshake is not proof of backend readiness

HTTP 101 only tells us that the WebSocket upgrade succeeded at the point handling the client connection.
Application initialization must be verified separately.

Investigate the entire event path

For an API Gateway WebSocket integration, verify:
  1. API Gateway connection and access logs.
  1. Route selection and integration configuration.
  1. VPC Link availability.
  1. NLB target health.
  1. ECS application event logs.
  1. DynamoDB connection records.
  1. Management API callback responses.
  1. Actual message receipt by the client.
A healthy ECS task alone does not validate the entire WebSocket communication flow.

Connection state must be managed carefully

When multiple ECS tasks are involved, storing connection records in local application memory is insufficient.
A shared store such as DynamoDB can help manage connection metadata, but persistent upstream sessions require additional ownership and coordination mechanisms.

Streaming applications need additional design considerations

API Gateway WebSocket APIs have a 32 KiB frame limit and a 128 KiB message limit. Incoming binary frames are not supported.
Applications sending audio or larger payloads must consider text encoding, chunk sizes, message ordering, backpressure, and session lifecycle management.
The streaming demonstration in this project uses mock text chunks. It is not a complete production voice-streaming solution.

Security and cost must be part of the architecture

This lab uses a simplified authentication token for educational testing.
Production deployments should use suitable authentication and authorization, such as a  $connect  authorizer or IAM, with identity-derived access control and least-privilege permissions.
Avoid sending sensitive tokens in URLs.
Private subnets, NAT gateways, load balancers, and running Fargate services also introduce costs. Provision resources for the lab and destroy them after verification.

9. Open-Source GitHub Project

I have made the project publicly available so AWS engineers and DevOps learners can reproduce the architecture, inspect the implementation, and extend the lab.
The repository includes Terraform infrastructure, FastAPI application code, a React teaching interface, Docker Compose, automated tests, troubleshooting documentation, and AWS deployment evidence.
For official documentation, refer to:

Conclusion

The most important lesson from this project is:
A successful transport connection does not guarantee that the application session is ready.
Understanding where WebSocket connections terminate, how backend integrations receive events, and how asynchronous responses reach clients is essential when designing real-time applications on AWS.
More importantly, troubleshooting distributed systems requires validating every layer independently rather than assuming that a successful connection proves the complete application flow.
I hope this lab helps others avoid the same architectural misunderstanding.
If you have implemented real-time messaging, AI streaming, or WebSocket applications with private AWS infrastructure, I'd be interested to hear how you handled session ownership, callbacks, and backend connectivity.


Built and documented by Bhari Sagar — DevOps Engineer and creator of DevOpsGrade ( https://devopsgrade.com ).
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article