|
| 1 | +# AI Chat Application using Amazon API Gateway (WebSockets), AWS Lambda, and Amazon Bedrock. |
| 2 | + |
| 3 | +This serverless architecture enables real-time AI chat using AWS services. A WebSocket Amazon API Gateway maintains persistent connections between clients and a Node.js AWS Lambda function. The AWS Lambda function handles user connections/disconnections, stores connection IDs in Amazon DynamoDB, and processes messages through an Amazon Bedrock LLM. The system includes error handling, automatic scaling, and pay-per-use pricing. The AWS SAM template provisions all necessary resources and IAM permissions, outputting a WebSocket URL for client connections. |
| 4 | + |
| 5 | + |
| 6 | +Learn more about this pattern at [Serverless Land Patterns](https://serverlessland.com/patterns/apigw-websockets-lambda-bedrock-sam) |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +* [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. |
| 13 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 14 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 15 | +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 16 | +* [NOTE! Manage Access to Amazon Bedrock Foundation Models](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html) |
| 17 | + |
| 18 | + |
| 19 | +## Deployment Instructions |
| 20 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 21 | + ``` |
| 22 | + git clone https://github.com/aws-samples/serverless-patterns |
| 23 | + ``` |
| 24 | +2. Change directory to the pattern directory: |
| 25 | + ``` |
| 26 | + cd apigw-websockets-lambda-bedrock-sam |
| 27 | + ``` |
| 28 | +3. Install dependencies |
| 29 | + ``` |
| 30 | + cd src && npm install && cd .. |
| 31 | + ``` |
| 32 | +4. From the command line, use AWS SAM build to prepare an application for subsequent steps in the developer workflow, such as local testing or deploying to the AWS Cloud: |
| 33 | + ``` |
| 34 | + sam build |
| 35 | + ``` |
| 36 | +5. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: |
| 37 | + ``` |
| 38 | + sam deploy --guided |
| 39 | + ``` |
| 40 | +6. During the prompts: |
| 41 | + * Enter a stack name |
| 42 | + * Enter the desired AWS Region |
| 43 | + * Enter the desired BedrockRegion |
| 44 | + * Enter the desired BedrockModelId |
| 45 | + * Allow SAM to create roles with the required permissions if needed. |
| 46 | +
|
| 47 | +7. Note the outputs **WebSocket URL** from the SAM deployment process. |
| 48 | +These contain the resource names and/or ARNs which are used for testing. |
| 49 | +```bash |
| 50 | +wss://<YOUR-WebSocket-URL>.execute-api.<YOUR-AWS-Region>.amazonaws.com/prod |
| 51 | +``` |
| 52 | + |
| 53 | +## Architecture |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +## How it Works |
| 58 | +WebSocket API Gateway serves as the entry point, enabling bidirectional real-time communication between clients and the backend. It handles three routes: |
| 59 | +* $connect - when users join the chat |
| 60 | +* $disconnect - when users leave |
| 61 | +* $default - for processing chat messages |
| 62 | + |
| 63 | +Lambda Function (Node.js 22.x) acts as the central orchestrator, handling all WebSocket events and business logic. It manages connection lifecycle, processes user messages, and coordinates with other AWS services. |
| 64 | +DynamoDB Table stores active WebSocket connection IDs, enabling the system to track which users are currently connected and send responses back to the correct clients. |
| 65 | +Amazon Bedrock provides AI capabilities using Claude 3 Sonnet model, processing user messages and generating intelligent responses. |
| 66 | + |
| 67 | +Data Flow: |
| 68 | +1. Connection: When a user connects, their connection ID is stored in DynamoDB |
| 69 | +2. Message Processing: User sends a message through WebSocket → Lambda receives it → Extracts message content → Sends to Bedrock Claude model |
| 70 | +3. AI Response: Bedrock processes the message and returns an AI-generated response |
| 71 | +4. Real-time Delivery: Lambda sends the AI response back to the user via WebSocket connection |
| 72 | +5. Disconnection: When user disconnects, their connection ID is removed from DynamoDB |
| 73 | + |
| 74 | +## Testing |
| 75 | + |
| 76 | +### Interactive Web Interface |
| 77 | +``` |
| 78 | +To use the test interface: |
| 79 | +1. Deploy the application using SAM |
| 80 | +2. Copy the WebSocket URL from the deployment outputs |
| 81 | +3. Open 'test.html' and update the 'WS_URL' variable with your WebSocket URL and AWS Region |
| 82 | +wss://<YOUR-WebSocket-URL>.execute-api.<YOUR-AWS-Region>.amazonaws.com/prod |
| 83 | +4. Save 'test.html' |
| 84 | +5. Open the HTML file in a browser |
| 85 | +6. Click "Connect" to establish a WebSocket connection |
| 86 | +7. Type your message and click "Send" |
| 87 | +8. When complete Click "Disconnect" |
| 88 | +``` |
| 89 | + |
| 90 | +### Command Line (wscat) |
| 91 | +```bash |
| 92 | +npm install -g wscat |
| 93 | +wscat -c wss://your-api-id.execute-api.your-region.amazonaws.com/prod |
| 94 | +# Then send: {"data": "What is AWS Lambda?"} |
| 95 | +``` |
| 96 | + |
| 97 | +## Cleanup |
| 98 | +1. Delete the stack |
| 99 | + ```bash |
| 100 | + sam delete |
| 101 | + ``` |
| 102 | +2. Confirm the stack has been deleted |
| 103 | + ```bash |
| 104 | + aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus" |
| 105 | + ``` |
| 106 | +---- |
| 107 | +Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 108 | + |
| 109 | +SPDX-License-Identifier: MIT-0 |
0 commit comments