Skip to content

Commit 70c246e

Browse files
committed
Merge branch 'main' of https://github.com/sidd130/serverless-patterns into sidd130-feature-sqs-lambda-s3
2 parents 8adcb49 + 9880802 commit 70c246e

File tree

91 files changed

+4942
-28
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+4942
-28
lines changed

alb-lambda-pulumi-go/go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ require (
5656
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
5757
github.com/xanzy/ssh-agent v0.3.3 // indirect
5858
go.uber.org/atomic v1.9.0 // indirect
59-
golang.org/x/crypto v0.35.0 // indirect
59+
golang.org/x/crypto v0.36.0 // indirect
6060
golang.org/x/mod v0.17.0 // indirect
61-
golang.org/x/net v0.36.0 // indirect
62-
golang.org/x/sync v0.11.0 // indirect
63-
golang.org/x/sys v0.30.0 // indirect
64-
golang.org/x/term v0.29.0 // indirect
65-
golang.org/x/text v0.22.0 // indirect
61+
golang.org/x/net v0.38.0 // indirect
62+
golang.org/x/sync v0.12.0 // indirect
63+
golang.org/x/sys v0.31.0 // indirect
64+
golang.org/x/term v0.30.0 // indirect
65+
golang.org/x/text v0.23.0 // indirect
6666
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
6767
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
6868
google.golang.org/grpc v1.56.3 // indirect

alb-lambda-pulumi-go/go.sum

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,8 +1242,8 @@ golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v
12421242
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
12431243
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
12441244
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
1245-
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
1246-
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
1245+
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
1246+
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
12471247
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
12481248
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
12491249
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -1395,8 +1395,8 @@ golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
13951395
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
13961396
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
13971397
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
1398-
golang.org/x/net v0.36.0 h1:vWF2fRbw4qslQsQzgFqZff+BItCvGFQqKzKIzx1rmoA=
1399-
golang.org/x/net v0.36.0/go.mod h1:bFmbeoIPfrw4sMHNhb4J9f6+tPziuGjq7Jk/38fxi1I=
1398+
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
1399+
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
14001400
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
14011401
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
14021402
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -1447,8 +1447,8 @@ golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
14471447
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
14481448
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
14491449
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
1450-
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
1451-
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
1450+
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
1451+
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
14521452
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
14531453
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
14541454
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -1567,8 +1567,8 @@ golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
15671567
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
15681568
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
15691569
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1570-
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
1571-
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1570+
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
1571+
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
15721572
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
15731573
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
15741574
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
@@ -1595,8 +1595,8 @@ golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
15951595
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
15961596
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
15971597
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
1598-
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=
1599-
golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s=
1598+
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
1599+
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
16001600
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
16011601
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
16021602
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -1620,8 +1620,8 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
16201620
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
16211621
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
16221622
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
1623-
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
1624-
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
1623+
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
1624+
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
16251625
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
16261626
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
16271627
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Serverless Generative AI with Amazon Bedrock Nova Micro
2+
3+
## Overview
4+
5+
This pattern demonstrates how to create a serverless API leveraging Amazon Bedrock Nova Micro through AWS Lambda. The solution uses an Amazon API Gateway REST endpoint to process requests and return AI-generated responses using Nova Micro's text-to-text capabilities.
6+
7+
Learn more about this pattern at [Serverless Land Patterns](https://serverlessland.com/patterns/apigw-lambda-bedrock-nova-terraform).
8+
9+
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.
10+
11+
## Requirements
12+
13+
* [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.
14+
* [Enable Access To Nova Models](https://docs.aws.amazon.com/nova/latest/userguide/getting-started-console.html) and follow item 2 under the Prerequisites to request access to Amazon Nova Micro model
15+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
16+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
17+
* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed
18+
19+
## Deployment Instructions
20+
21+
1. Clone the project to your local working directory
22+
23+
```sh
24+
git clone https://github.com/aws-samples/serverless-patterns/
25+
```
26+
27+
2. Change the working directory to this pattern's directory
28+
29+
```sh
30+
cd serverless-patterns/apigw-lambda-bedrock-nova-terraform
31+
```
32+
33+
3. From the command line, run each of the command mentioned in the create_lambda_layer.sh file, this file contains the commands to create the Lambda Layer as well zip the bedrock_integration.py which invokes the Amazon Nova model.
34+
35+
4. From the command line, initialize terraform to to downloads and installs the providers defined in the configuration:
36+
```
37+
terraform init
38+
```
39+
40+
5. From the command line, apply the configuration in the main.tf file:
41+
```
42+
terraform apply
43+
```
44+
45+
6. During the prompts:
46+
- Enter yes
47+
48+
## How it works
49+
50+
The pattern creates an API Gateway endpoint that accepts POST requests containing prompts. These requests are forwarded to a Lambda function which processes the input and interacts with Amazon Bedrock's Nova Micro model.
51+
52+
Key components:
53+
54+
* API Gateway REST API endpoint
55+
* Lambda function with Nova Micro integration
56+
* IAM roles and policies for Lambda and Bedrock access
57+
* Lambda layer for boto3 dependencies
58+
* Nova Micro Request Format:
59+
```
60+
61+
{
62+
"system": [
63+
{
64+
"text": "You are a helpful AI assistant that provides accurate and concise information."
65+
}
66+
],
67+
"messages": [
68+
{
69+
"role": "user",
70+
"content": [
71+
{
72+
"text": "<your-prompt>"
73+
}
74+
]
75+
}
76+
],
77+
"inferenceConfig": {
78+
"maxTokens": 1024,
79+
"temperature": 0.7,
80+
"topP": 0.9,
81+
"topK": 50,
82+
"stopSequences": []
83+
}
84+
}
85+
86+
```
87+
88+
## Testing
89+
90+
Using Curl:
91+
92+
```
93+
curl -X POST \
94+
-H "Content-Type: application/json" \
95+
-d '{"prompt": "What are the key benefits of using AWS services?"}' \
96+
https://YOUR-API-ENDPOINT/dev/generate_content
97+
98+
```
99+
100+
## Viewing Test Results
101+
```
102+
{
103+
"generated-text": "<Model generated response>"
104+
}
105+
```
106+
107+
## Cleanup
108+
109+
1. Change directory to the pattern directory:
110+
```sh
111+
cd serverless-patterns/apigw-lambda-bedrock-nova-terraform
112+
```
113+
114+
2. Delete all created resources
115+
```sh
116+
terraform destroy
117+
```
118+
119+
3. During the prompts:
120+
* Enter yes
121+
122+
4. Confirm all created resources has been deleted
123+
```sh
124+
terraform show
125+
```
126+
127+
## Reference
128+
129+
- [Amazon Bedrock Nova Models](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-nova.html)
130+
- [AWS Lambda with API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-with-lambda-integration.html)
131+
- [Amazon API Gateway REST APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-rest-api.html)
132+
133+
----
134+
Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
135+
136+
SPDX-License-Identifier: MIT-0
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
resource "aws_api_gateway_rest_api" "generate_content_api" {
2+
name = "generate-content-api"
3+
}
4+
5+
resource "aws_api_gateway_resource" "generate_content" {
6+
rest_api_id = aws_api_gateway_rest_api.generate_content_api.id
7+
parent_id = aws_api_gateway_rest_api.generate_content_api.root_resource_id
8+
path_part = "generate_content"
9+
}
10+
11+
resource "aws_api_gateway_method" "generate_content_post" {
12+
rest_api_id = aws_api_gateway_rest_api.generate_content_api.id
13+
resource_id = aws_api_gateway_resource.generate_content.id
14+
http_method = "POST"
15+
authorization = "NONE"
16+
}
17+
18+
resource "aws_api_gateway_integration" "lambda_integration" {
19+
rest_api_id = aws_api_gateway_rest_api.generate_content_api.id
20+
resource_id = aws_api_gateway_resource.generate_content.id
21+
http_method = aws_api_gateway_method.generate_content_post.http_method
22+
23+
integration_http_method = "POST"
24+
type = "AWS_PROXY"
25+
uri = aws_lambda_function.content_generation.invoke_arn
26+
}
27+
28+
resource "aws_api_gateway_deployment" "api_deployment" {
29+
rest_api_id = aws_api_gateway_rest_api.generate_content_api.id
30+
31+
depends_on = [
32+
aws_api_gateway_integration.lambda_integration
33+
]
34+
35+
lifecycle {
36+
create_before_destroy = true
37+
}
38+
}
39+
40+
resource "aws_api_gateway_stage" "dev" {
41+
deployment_id = aws_api_gateway_deployment.api_deployment.id
42+
rest_api_id = aws_api_gateway_rest_api.generate_content_api.id
43+
stage_name = "dev"
44+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import json
2+
import boto3
3+
import botocore
4+
5+
# Create Bedrock Runtime client
6+
bedrock = boto3.client(
7+
service_name='bedrock-runtime',
8+
region_name='us-east-1',
9+
endpoint_url='https://bedrock-runtime.us-east-1.amazonaws.com'
10+
)
11+
12+
def lambda_handler(event, context):
13+
try:
14+
# Handle both direct Lambda invocation and API Gateway requests
15+
if isinstance(event, dict):
16+
if 'body' in event:
17+
body = json.loads(event["body"])
18+
else:
19+
body = event
20+
else:
21+
return {
22+
'statusCode': 400,
23+
'body': json.dumps({
24+
'error': 'Invalid input format'
25+
})
26+
}
27+
28+
# Extract the prompt
29+
prompt = body.get("prompt")
30+
if not prompt:
31+
return {
32+
'statusCode': 400,
33+
'body': json.dumps({
34+
'error': 'Prompt is required'
35+
})
36+
}
37+
38+
print("Prompt = " + prompt)
39+
40+
# Create the request body for Nova Micro
41+
request_body = json.dumps({
42+
"system": [
43+
{
44+
"text": "You are a helpful AI assistant that provides accurate and concise information."
45+
}
46+
],
47+
"messages": [
48+
{
49+
"role": "user",
50+
"content": [
51+
{
52+
"text": prompt
53+
}
54+
]
55+
}
56+
],
57+
"inferenceConfig": {
58+
"maxTokens": 1024,
59+
"temperature": 0.7,
60+
"topP": 0.9,
61+
"topK": 50,
62+
"stopSequences": []
63+
}
64+
})
65+
66+
# Invoke Bedrock API with Nova Micro model
67+
try:
68+
response = bedrock.invoke_model(
69+
body=request_body,
70+
modelId='amazon.nova-micro-v1:0',
71+
accept='application/json',
72+
contentType='application/json'
73+
)
74+
75+
# Parse the response body
76+
response_body = json.loads(response['body'].read())
77+
print("Nova Micro Response:", response_body)
78+
79+
# Extract text from the nested response structure
80+
generated_text = response_body.get('output', {}).get('message', {}).get('content', [{}])[0].get('text', '')
81+
82+
return {
83+
'statusCode': 200,
84+
'headers': {
85+
'Content-Type': 'application/json',
86+
'Access-Control-Allow-Origin': '*'
87+
},
88+
'body': json.dumps({
89+
'generated-text': generated_text
90+
})
91+
}
92+
93+
except botocore.exceptions.ClientError as e:
94+
print(f"Bedrock API Error: {str(e)}")
95+
return {
96+
'statusCode': 500,
97+
'headers': {
98+
'Content-Type': 'application/json',
99+
'Access-Control-Allow-Origin': '*'
100+
},
101+
'body': json.dumps({
102+
'error': f"Bedrock API Error: {str(e)}"
103+
})
104+
}
105+
106+
except Exception as e:
107+
print(f"Error: {str(e)}")
108+
import traceback
109+
print(f"Traceback: {traceback.format_exc()}")
110+
return {
111+
'statusCode': 500,
112+
'headers': {
113+
'Content-Type': 'application/json',
114+
'Access-Control-Allow-Origin': '*'
115+
},
116+
'body': json.dumps({
117+
'error': str(e),
118+
'traceback': traceback.format_exc()
119+
})
120+
}

0 commit comments

Comments
 (0)