|
| 1 | +# Streaming Amazon Bedrock response with Amazon API Gateway WebSocket API and AWS Lambda |
| 2 | + |
| 3 | +This CDK application demonstrates a simple, serverless approach to integrating Amazon Bedrock with AWS Lambda and Amazon API Gateway. Written in Rust, it showcases how to efficiently stream responses from Amazon Bedrock to a client via WebSocket connections. The example serves as a practical illustration of implementing real-time, serverless communication between Bedrock's GenAI capabilities and a client application. |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +* [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. |
| 10 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 11 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 12 | +* [Node and NPM](https://nodejs.org/en/download/) installed |
| 13 | +* [AWS Cloud Development Kit](https://docs.aws.amazon.com/cdk/latest/guide/cli.html) (AWS CDK) installed |
| 14 | +* [Docker](https://docs.docker.com/engine/install/) installed and running locally (needed for Rust cross-platform Lambda build) |
| 15 | +* [Rust](https://www.rust-lang.org/) 🦀 installed with v1.79.0 or higher |
| 16 | +* [Cargo Lambda](https://www.cargo-lambda.info/) installed |
| 17 | +* [cross](https://github.com/cross-rs/cross) compilation installed for Cargo Lambda: `cargo install cross --git https://github.com/cross-rs/cross` |
| 18 | +* [wscat](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-wscat.html) installed for CLI WebSocket capabilities |
| 19 | + |
| 20 | + |
| 21 | +## Amazon Bedrock Setup Instructions |
| 22 | + |
| 23 | +You must request access to the Bedrock LLM model before you can use it. This example uses `Claude 3 Sonnet`, so make sure you have `Access granted` to this model. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html). |
| 24 | + |
| 25 | +## Deployment Instructions |
| 26 | + |
| 27 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 28 | + ```bash |
| 29 | + git clone https://github.com/aws-samples/serverless-patterns |
| 30 | + ``` |
| 31 | +2. Change directory to the pattern's CDK directory: |
| 32 | + ```bash |
| 33 | + cd apigw-websocket-api-bedrock-streaming-rust-cdk/cdk |
| 34 | + ``` |
| 35 | +3. From the command line, use npm to install the development dependencies: |
| 36 | + ```bash |
| 37 | + npm install |
| 38 | + ``` |
| 39 | +4. If you haven't done so previously for this account, run this command to bootstrap CDK: |
| 40 | + ```bash |
| 41 | + cdk bootstrap |
| 42 | + ``` |
| 43 | +5. Review the CloudFormation template that CDK generates for your stack using the following AWS CDK CLI command: |
| 44 | + ```bash |
| 45 | + cdk synth |
| 46 | + ``` |
| 47 | +6. Use AWS CDK to deploy your AWS resources |
| 48 | + ```bash |
| 49 | + cdk deploy |
| 50 | + ``` |
| 51 | + |
| 52 | + After the deployment completes, note the URL in the `Outputs` section at the end. The `BedrockStreamerStack.WebSocketURL` followed by the WebSocket URL will be used to connect to API Gateway. It should look something like `wss://{YOUR_API_ID_HERE}.execute-api.{YOUR_REGION_HERE}.amazonaws.com/prod` |
| 53 | + |
| 54 | +## How it works |
| 55 | + |
| 56 | +This pattern establishes a WebSocket connection to Amazon API Gateway. When requests are made to this API, API Gateway routes them to an AWS Lambda function. The Lambda function then initiates a streaming request to Amazon Bedrock using the [ConverseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html) API. This allows the response from the LLM in Bedrock to start streaming back to the Lambda function as soon as generation begins, without waiting for the entire response to be ready. |
| 57 | + |
| 58 | +Generating Bedrock responses often takes a long time. By using a streaming approach, the Lambda function can immediately start processing the incoming response and writing the data chunks to the API Gateway WebSocket. The WebSocket then delivers these chunks in real-time to the connected client, providing an extremely interactive user experience. |
| 59 | + |
| 60 | +## Testing |
| 61 | + |
| 62 | +1. From your terminal, use `wscat` to connect to API Gateway using the WebSocket API and generate a short story about `CATS` by entering the `{"storyType": "CATS"}` line after `wscat` startup. |
| 63 | + ```bash |
| 64 | + # Connect to the API Gateway via WebSocket |
| 65 | + wscat -c <API_GATEWAY_URL_FROM_CDK_OUTPUT> |
| 66 | +
|
| 67 | + Connected (press CTRL+C to quit) |
| 68 | + > {"storyType": "CATS"} <--- ENTER THIS...PRESS RETURN |
| 69 | + < {"type":"other","message":null} |
| 70 | + < {"type":"text","message":"Here"} |
| 71 | + < {"type":"text","message":" is"} |
| 72 | + < {"type":"text","message":" a"} |
| 73 | + < {"type":"text","message":" very"} |
| 74 | + < {"type":"text","message":" short"} |
| 75 | + < {"type":"text","message":" story"} |
| 76 | + < {"type":"text","message":" about"} |
| 77 | + < {"type":"text","message":" cats"} |
| 78 | + < {"type":"text","message":":"} |
| 79 | + < {"type":"text","message":"\n\nMitt"} |
| 80 | + < {"type":"text","message":"ens"} |
| 81 | + < {"type":"text","message":" cur"} |
| 82 | + < {"type":"text","message":"le"} |
| 83 | + < {"type":"text","message":"d up"} |
| 84 | + < {"type":"text","message":" on"} |
| 85 | + < {"type":"text","message":" the"} |
| 86 | + < {"type":"text","message":" window"} |
| 87 | + < {"type":"text","message":"s"} |
| 88 | + < {"type":"text","message":"ill"} |
| 89 | + < {"type":"text","message":","} |
| 90 | + . |
| 91 | + . |
| 92 | + < {"type":"other","message":null} |
| 93 | + < {"type":"other","message":null} |
| 94 | + < {"type":"other","message":null} |
| 95 | + > ⏎ <--- CTRL+C HERE> |
| 96 | + ``` |
| 97 | +2. As the `wscat` CLI says, press `CTRL+C` to disconnect |
| 98 | + |
| 99 | +## Cleanup |
| 100 | + |
| 101 | +You can use the following commands to destroy the AWS resources created during deployment. This assumes you're currently at the `apigw-websocket-api-bedrock-streaming-rust-cdk/cdk` directory in your terminal: |
| 102 | +
|
| 103 | +```bash |
| 104 | +cdk destroy |
| 105 | +``` |
| 106 | +---- |
| 107 | +Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 108 | +
|
| 109 | +SPDX-License-Identifier: MIT-0 |
0 commit comments