|
| 1 | +# Amazon Bedrock to Amazon DynamoDB |
| 2 | + |
| 3 | +This pattern demonstrates how to use AWS Lambda to process queries using Amazon Bedrock's model and store the conversation results in Amazon DynamoDB. |
| 4 | + |
| 5 | +Learn more about this pattern at Serverless Land Patterns: [Serverless Land](https://serverlessland.com/patterns/) |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +* [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. |
| 12 | +* Python 3.12 |
| 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 CLI) installed |
| 16 | +* This pattern uses foundation models provided by Amazon Bedrock. It is required to request access to the model before starting using the pattern. Please refer to the link below for instructions: [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html). |
| 17 | + |
| 18 | + |
| 19 | +## Architecture |
| 20 | + |
| 21 | +- AWS Lambda function |
| 22 | +- Amazon Bedrock |
| 23 | +- Amazon DynamoDB |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +## Deployment Instructions |
| 28 | + |
| 29 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 30 | + ``` |
| 31 | + git clone https://github.com/aws-samples/serverless-patterns/lambda-bedrock-dynamodb-sam |
| 32 | + |
| 33 | + ``` |
| 34 | +1. Change directory to the pattern directory: |
| 35 | + ``` |
| 36 | + cd lambda-bedrock-dynamodb-sam |
| 37 | + ``` |
| 38 | +1. From the command line, use AWS SAM to build the SAM application: |
| 39 | + ``` |
| 40 | + sam build |
| 41 | + ``` |
| 42 | +1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: |
| 43 | + ``` |
| 44 | + sam deploy --guided |
| 45 | + ``` |
| 46 | +1. During the prompts: |
| 47 | + * Enter a stack name |
| 48 | + * Enter the desired AWS Region (We suggest ```us-east-1``` as the code has been tested in this region) |
| 49 | + * Enter a name for your DynamoDB table |
| 50 | + * Enter the model-id of your choice [Example : ```anthropic.claude-3-haiku-20240307-v1:0```](You'll need to have the model access as a pre-requiste) |
| 51 | + * Allow SAM CLI to create IAM roles with the required permissions. |
| 52 | +
|
| 53 | + After the first run of `sam deploy --guided` and saving the arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. |
| 54 | +
|
| 55 | +1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 56 | +
|
| 57 | +
|
| 58 | +## Usage Example |
| 59 | +
|
| 60 | +Invoke the Lambda function with a query: |
| 61 | +
|
| 62 | +```bash |
| 63 | +aws lambda invoke \ |
| 64 | + --function-name lambda-processor \ |
| 65 | + --cli-binary-format raw-in-base64-out \ |
| 66 | + --payload '{"query": "What is the capital of France?"}' \ |
| 67 | + output.txt |
| 68 | +``` |
| 69 | + |
| 70 | +Verify the stored results: |
| 71 | + |
| 72 | +```bash |
| 73 | +aws dynamodb scan --table-name YOUR_TABLE_NAME |
| 74 | +``` |
| 75 | + |
| 76 | +Replace `YOUR_TABLE_NAME` with the actual name of your DynamoDB table. |
| 77 | + |
| 78 | + |
| 79 | +## Resources Created |
| 80 | + |
| 81 | +- **DynamoDB Table**: Stores the queries and responses. |
| 82 | +- **Lambda Function**: Processes queries using Bedrock and stores results in DynamoDB. |
| 83 | + |
| 84 | + |
| 85 | +## Configuration |
| 86 | + |
| 87 | +The main configuration parameters are set in the SAM template: |
| 88 | + |
| 89 | +- `tableName`: The name of the DynamoDB table (passed as a parameter during deployment) |
| 90 | +- Lambda function timeout: 180 seconds |
| 91 | +- Lambda function memory: 128 MB |
| 92 | + |
| 93 | + |
| 94 | +## IAM Permissions |
| 95 | + |
| 96 | +The Lambda function is granted the following permissions: |
| 97 | + |
| 98 | +- `dynamodb:PutItem` on the created DynamoDB table |
| 99 | +- `bedrock:InvokeModel` for the model provided in |
| 100 | + |
| 101 | + |
| 102 | +## Environment Variables |
| 103 | + |
| 104 | +The Lambda function uses the following environment variable: |
| 105 | + |
| 106 | +- `table_name`: Set to the name of the created DynamoDB table |
| 107 | +- `model_ID` : Set to the name of the choosen Bedrock's model ID |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +## Cleanup |
| 112 | + |
| 113 | +1. To delete the resources deployed to your AWS account via AWS SAM, run the following command: |
| 114 | + |
| 115 | +```bash |
| 116 | + sam delete |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 123 | + |
| 124 | +SPDX-License-Identifier: MIT-0 |
0 commit comments