Skip to content

Commit 8867223

Browse files
authored
Merge pull request #2511 from Monalisu/Monalisu-feature-lambda-bedrock-dynamodb-sam
Pushing new pattern code to serveless-patterns
2 parents 1a14f18 + 3d5703e commit 8867223

File tree

6 files changed

+413
-0
lines changed

6 files changed

+413
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
![Alt text](./images/bedrock-dynamodb.png)
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
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"title": "Amazon Bedrock to Amazon DynamoDB",
3+
"description": "Create a Lambda function to process queries using Amazon Bedrock and store results in Amazon DynamoDB.",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern demonstrates how to use AWS Lambda to process queries using Amazon Bedrock's Claude 3 Haiku model and store the results in Amazon DynamoDB.",
11+
"The Lambda function is triggered with an event containing a query, which is then processed using Bedrock. Both the query and the response are stored in a DynamoDB table.",
12+
"This patterns forms the basis of persisting the conversation of user and model in a NoSQL database",
13+
"This pattern deploys one Lambda function, one DynamoDB table, and the necessary IAM roles and permissions."
14+
]
15+
},
16+
"gitHub": {
17+
"template": {
18+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/bedrock-dynamodb-sam-python",
19+
"templateURL": "serverless-patterns/bedrock-dynamodb-sam-python",
20+
"projectFolder": "bedrock-dynamodb-sam-python",
21+
"templateFile": "template.yaml"
22+
}
23+
},
24+
"resources": {
25+
"bullets": [
26+
{
27+
"text": "Amazon Bedrock - Foundation Models",
28+
"link": "https://aws.amazon.com/bedrock/"
29+
},
30+
{
31+
"text": "AWS Lambda - Serverless Computing",
32+
"link": "https://aws.amazon.com/lambda/"
33+
},
34+
{
35+
"text": "Amazon DynamoDB - NoSQL Database Service",
36+
"link": "https://aws.amazon.com/dynamodb/"
37+
}
38+
]
39+
},
40+
"deploy": {
41+
"text": [
42+
"sam build",
43+
"sam deploy --guided"
44+
]
45+
},
46+
"testing": {
47+
"text": [
48+
"Invoke the Lambda function with a test event containing a query:",
49+
"aws lambda invoke --function-name lambda-processor --payload '{\"query\": \"What is the capital of France?\"}' output.txt",
50+
"Check the DynamoDB table to see the stored query and response:",
51+
"aws dynamodb scan --table-name YOUR_TABLE_NAME"
52+
]
53+
},
54+
"cleanup": {
55+
"text": [
56+
"Delete the stack: <code>aws cloudformation delete-stack --stack-name STACK_NAME</code>",
57+
"Confirm the stack has been deleted: <code>aws cloudformation list-stacks --query \"StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus\"</code>"
58+
]
59+
},
60+
"authors": [
61+
{
62+
"name": "SVVS KOUNDINYA",
63+
"image": "https://drive.google.com/file/d/1kcAC_pg-r3yIJroR3lsLowF37mxxnwPV/view?usp=sharing",
64+
"bio": " I am an Solutions Architect with AWS. I enjoy working with customers all across the Industries and help them to build efficient and scalable solutions on AWS.",
65+
"linkedin": "https://www.linkedin.com/in/svvs-koundinya-0a1652133/"
66+
},
67+
{
68+
"name": "MONALISA NATH",
69+
"image": "https://drive.google.com/file/d/1s-PBpMtLtYHSNMcfnUKy42Xdx5W0qJIS/view?usp=drive_link",
70+
"bio": " I am a Solutions Architect at AWS and help our strategic customers build and operate their key workloads on AWS.",
71+
"linkedin": "https://www.linkedin.com/in/monalisa-nath-577640180/"
72+
}
73+
]
74+
}
7.34 KB
Loading
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"title": "Amazon Bedrock to Amazon DynamoDB",
3+
"description": "Invoke Amazon Bedrock with AWS Lambda and store results in Amazon DynamoDB.",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern demonstrates processing queries with AWS Lambda and Amazon Bedrock's Claude 3 Haiku model then storing the results in Amazon DynamoDB.",
11+
"The Lambda function is invoked with an event containing a query, which is processed using Bedrock. Both the query and the response are stored in a DynamoDB table.",
12+
"This patterns forms the basis of persisting the conversation of user and model in a NoSQL database",
13+
"This pattern deploys one Lambda function, one DynamoDB table, and the necessary IAM roles and permissions."
14+
]
15+
},
16+
"gitHub": {
17+
"template": {
18+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/bedrock-dynamodb-sam-python",
19+
"templateURL": "serverless-patterns/lambda-bedrock-dynamodb-sam",
20+
"projectFolder": "lambda-bedrock-dynamodb-sam",
21+
"templateFile": "template.yaml"
22+
}
23+
},
24+
"resources": {
25+
"bullets": [
26+
{
27+
"text": "Amazon Bedrock - Foundation Models",
28+
"link": "https://aws.amazon.com/bedrock/"
29+
},
30+
{
31+
"text": "AWS Lambda - Serverless Computing",
32+
"link": "https://aws.amazon.com/lambda/"
33+
},
34+
{
35+
"text": "Amazon DynamoDB - NoSQL Database Service",
36+
"link": "https://aws.amazon.com/dynamodb/"
37+
}
38+
]
39+
},
40+
"deploy": {
41+
"text": [
42+
"sam build",
43+
"sam deploy --guided"
44+
]
45+
},
46+
"testing": {
47+
"text": [
48+
"See the GitHub repo for detailed testing instructions."
49+
]
50+
},
51+
"cleanup": {
52+
"text": [
53+
"Delete the stack: <code>sam delete</code>",
54+
"Confirm the stack has been deleted: <code>aws cloudformation list-stacks --query \"StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus\"</code>"
55+
]
56+
},
57+
"authors": [
58+
{
59+
"name": "SVVS KOUNDINYA",
60+
"image": "https://drive.google.com/file/d/1kcAC_pg-r3yIJroR3lsLowF37mxxnwPV/view?usp=sharing",
61+
"bio": " I am an Solutions Architect with AWS. I enjoy working with customers all across the Industries and help them to build efficient and scalable solutions on AWS.",
62+
"linkedin": "svvs-koundinya-0a1652133"
63+
},
64+
{
65+
"name": "MONALISA NATH",
66+
"image": "https://drive.google.com/file/d/1s-PBpMtLtYHSNMcfnUKy42Xdx5W0qJIS/view?usp=drive_link",
67+
"bio": " I am a Solutions Architect at AWS and help our strategic customers build and operate their key workloads on AWS.",
68+
"linkedin": "monalisa-nath-577640180"
69+
}
70+
],
71+
"patternArch": {
72+
"icon1": {
73+
"x": 20,
74+
"y": 50,
75+
"service": "lambda",
76+
"label": "AWS Lambda"
77+
},
78+
"icon2": {
79+
"x": 50,
80+
"y": 50,
81+
"service": "bedrock",
82+
"label": "Amazon Bedrock"
83+
},
84+
"icon3": {
85+
"x": 80,
86+
"y": 50,
87+
"service": "dynamodb",
88+
"label": "Amazon DynamoDB"
89+
},
90+
"line1": {
91+
"from": "icon1",
92+
"to": "icon2"
93+
},
94+
"line2": {
95+
"from": "icon2",
96+
"to": "icon3"
97+
}
98+
}
99+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import json
2+
import boto3
3+
import os
4+
5+
bedrock = boto3.client('bedrock-runtime')
6+
dynamodb = boto3.resource('dynamodb')
7+
table_name = os.environ['table_name']
8+
9+
model_id = os.environ['model_ID']
10+
11+
def lambda_handler(event, context):
12+
13+
messages = {
14+
"role": "user",
15+
"content": [{"text": event['query']}]}
16+
17+
response = bedrock.converse(
18+
modelId=model_id,
19+
messages=[messages]
20+
)
21+
22+
# Store the response in DynamoDB
23+
table = dynamodb.Table(table_name)
24+
item = {
25+
'id': str(context.aws_request_id),
26+
'query': event['query'],
27+
'response': response
28+
}
29+
table.put_item(Item=item)
30+
31+
return {
32+
'statusCode': 200
33+
}

0 commit comments

Comments
 (0)