-
Notifications
You must be signed in to change notification settings - Fork 986
New serverless pattern - apigw-lambda-bedrock-nova-terraform #2700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
julianwood
merged 5 commits into
aws-samples:main
from
nareshrajaram2017:nareshrajaram2017-feature-apigw-lambda-bedrock-nova-terraform
Apr 23, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f49ecd
added code for apigw-lambda-bedrock-nova-terraform
nareshrajaram2017 fddbcba
Update apigw-lambda-bedrock-nova-terraform/README.md
nareshrajaram2017 032d85a
Update apigw-lambda-bedrock-nova-terraform/README.md
nareshrajaram2017 7602765
updated python version and updated README.md
nareshrajaram2017 8e7718b
publishing file
ellisms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# Serverless Generative AI with Amazon Bedrock Nova Micro | ||
|
||
## Overview | ||
|
||
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. | ||
|
||
Learn more about this pattern at [Serverless Land Patterns](https://serverlessland.com/patterns/apigw-lambda-bedrock-nova-terraform). | ||
|
||
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. | ||
|
||
## Requirements | ||
|
||
* [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. | ||
* [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 | ||
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured | ||
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) | ||
* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed | ||
|
||
## Deployment Instructions | ||
|
||
1. Clone the project to your local working directory | ||
|
||
```sh | ||
git clone https://github.com/aws-samples/serverless-patterns/ | ||
``` | ||
|
||
2. Change the working directory to this pattern's directory | ||
|
||
```sh | ||
cd serverless-patterns/apigw-lambda-bedrock-nova-terraform | ||
``` | ||
|
||
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. | ||
ellisms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
4. From the command line, initialize terraform to to downloads and installs the providers defined in the configuration: | ||
``` | ||
terraform init | ||
``` | ||
|
||
5. From the command line, apply the configuration in the main.tf file: | ||
``` | ||
terraform apply | ||
``` | ||
|
||
6. During the prompts: | ||
- Enter yes | ||
|
||
## How it works | ||
|
||
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. | ||
|
||
Key components: | ||
|
||
* API Gateway REST API endpoint | ||
* Lambda function with Nova Micro integration | ||
* IAM roles and policies for Lambda and Bedrock access | ||
* Lambda layer for boto3 dependencies | ||
* Nova Micro Request Format: | ||
``` | ||
|
||
{ | ||
"system": [ | ||
{ | ||
"text": "You are a helpful AI assistant that provides accurate and concise information." | ||
} | ||
], | ||
"messages": [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"text": "<your-prompt>" | ||
} | ||
] | ||
} | ||
], | ||
"inferenceConfig": { | ||
"maxTokens": 1024, | ||
"temperature": 0.7, | ||
"topP": 0.9, | ||
"topK": 50, | ||
"stopSequences": [] | ||
} | ||
} | ||
|
||
``` | ||
|
||
## Testing | ||
|
||
Using Curl: | ||
|
||
``` | ||
curl -X POST \ | ||
-H "Content-Type: application/json" \ | ||
-d '{"prompt": "What are the key benefits of using AWS services?"}' \ | ||
https://YOUR-API-ENDPOINT/dev/generate_content | ||
|
||
``` | ||
|
||
## Viewing Test Results | ||
``` | ||
{ | ||
"generated-text": "<Model generated response>" | ||
} | ||
``` | ||
|
||
## Cleanup | ||
|
||
1. Change directory to the pattern directory: | ||
```sh | ||
cd serverless-patterns/apigw-lambda-bedrock-nova-terraform | ||
``` | ||
|
||
2. Delete all created resources | ||
```sh | ||
terraform destroy | ||
``` | ||
|
||
3. During the prompts: | ||
* Enter yes | ||
|
||
4. Confirm all created resources has been deleted | ||
```sh | ||
terraform show | ||
``` | ||
|
||
## Reference | ||
|
||
- [Amazon Bedrock Nova Models](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-nova.html) | ||
- [AWS Lambda with API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-with-lambda-integration.html) | ||
- [Amazon API Gateway REST APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-rest-api.html) | ||
|
||
---- | ||
Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
SPDX-License-Identifier: MIT-0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
resource "aws_api_gateway_rest_api" "generate_content_api" { | ||
name = "generate-content-api" | ||
} | ||
|
||
resource "aws_api_gateway_resource" "generate_content" { | ||
rest_api_id = aws_api_gateway_rest_api.generate_content_api.id | ||
parent_id = aws_api_gateway_rest_api.generate_content_api.root_resource_id | ||
path_part = "generate_content" | ||
} | ||
|
||
resource "aws_api_gateway_method" "generate_content_post" { | ||
rest_api_id = aws_api_gateway_rest_api.generate_content_api.id | ||
resource_id = aws_api_gateway_resource.generate_content.id | ||
http_method = "POST" | ||
authorization = "NONE" | ||
} | ||
|
||
resource "aws_api_gateway_integration" "lambda_integration" { | ||
rest_api_id = aws_api_gateway_rest_api.generate_content_api.id | ||
resource_id = aws_api_gateway_resource.generate_content.id | ||
http_method = aws_api_gateway_method.generate_content_post.http_method | ||
|
||
integration_http_method = "POST" | ||
type = "AWS_PROXY" | ||
uri = aws_lambda_function.content_generation.invoke_arn | ||
} | ||
|
||
resource "aws_api_gateway_deployment" "api_deployment" { | ||
rest_api_id = aws_api_gateway_rest_api.generate_content_api.id | ||
|
||
depends_on = [ | ||
aws_api_gateway_integration.lambda_integration | ||
] | ||
|
||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
} | ||
|
||
resource "aws_api_gateway_stage" "dev" { | ||
deployment_id = aws_api_gateway_deployment.api_deployment.id | ||
rest_api_id = aws_api_gateway_rest_api.generate_content_api.id | ||
stage_name = "dev" | ||
} |
94 changes: 94 additions & 0 deletions
94
apigw-lambda-bedrock-nova-terraform/apigw-lambda-bedrock-nova-terraform.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
{ | ||
"title": "Serverless Generative AI with Amazon Bedrock Nova Micro", | ||
"description": "This pattern demonstrates how to create a serverless API leveraging Amazon Bedrock Nova Micro through AWS Lambda.", | ||
"language": "Python", | ||
"level": "200", | ||
"framework": "Terraform", | ||
"introBox": { | ||
"headline": "How it works", | ||
"text": [ | ||
"An Amazon API Gateway REST endpoint accepts POST requests containing prompts. These requests are automatically routed to an AWS Lambda function, which processes the input and interacts with Amazon Bedrock Nova Micro.", | ||
"The Lambda function formats the request according to the model's requirements, invokes the model, and returns the generated response through API Gateway. This serverless architecture ensures scalability and cost-effectiveness, as you only pay for actual API calls and compute time." | ||
] | ||
}, | ||
"gitHub": { | ||
"template": { | ||
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-lambda-bedrock-nova-terraform", | ||
"templateURL": "apigw-lambda-bedrock-nova-terraform", | ||
"projectFolder": "apigw-lambda-bedrock-nova-terraform", | ||
"templateFile": "main.tf" | ||
} | ||
}, | ||
"resources": { | ||
"bullets": [ | ||
{ | ||
"text": "Amazon API Gateway REST APIs", | ||
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html" | ||
}, | ||
{ | ||
"text": "AWS Lambda Function Integration", | ||
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-with-lambda-integration.html" | ||
}, | ||
{ | ||
"text": "Amazon Bedrock Nova Models", | ||
"link": "https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-nova.html" | ||
} | ||
] | ||
}, | ||
"deploy": { | ||
"text": [ | ||
"terraform init", | ||
"terraform plan", | ||
"terraform apply" | ||
] | ||
}, | ||
"testing": { | ||
"text": [ | ||
"Test the API endpoint using curl:", | ||
"<code>curl -X POST -H \"Content-Type: application/json\" -d '{\"prompt\": \"What are the key benefits of using AWS services?\"}' https://YOUR-API-ENDPOINT/dev/generate_content<code>", | ||
"" | ||
] | ||
}, | ||
"cleanup": { | ||
"text": [ | ||
"<code>terraform destroy<code>", | ||
"<code>terraform show<code>" | ||
] | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Naresh Rajaram", | ||
"image": "", | ||
"bio": "Senior Partner Solutions Architect, AWS", | ||
"linkedin": "nareshrajaram" | ||
} | ||
], | ||
"patternArch": { | ||
"icon1": { | ||
"x": 20, | ||
"y": 50, | ||
"service": "apigw", | ||
"label": "API Gateway REST API" | ||
}, | ||
"icon2": { | ||
"x": 50, | ||
"y": 50, | ||
"service": "lambda", | ||
"label": "AWS Lambda" | ||
}, | ||
"icon3": { | ||
"x": 80, | ||
"y": 50, | ||
"service": "bedrock", | ||
"label": "Amazon Bedrock" | ||
}, | ||
"line1": { | ||
"from": "icon1", | ||
"to": "icon2" | ||
}, | ||
"line2": { | ||
"from": "icon2", | ||
"to": "icon3" | ||
} | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
apigw-lambda-bedrock-nova-terraform/bedrock_integration.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import json | ||
import boto3 | ||
import botocore | ||
|
||
# Create Bedrock Runtime client | ||
bedrock = boto3.client( | ||
service_name='bedrock-runtime', | ||
region_name='us-east-1', | ||
endpoint_url='https://bedrock-runtime.us-east-1.amazonaws.com' | ||
) | ||
|
||
def lambda_handler(event, context): | ||
try: | ||
# Handle both direct Lambda invocation and API Gateway requests | ||
if isinstance(event, dict): | ||
if 'body' in event: | ||
body = json.loads(event["body"]) | ||
else: | ||
body = event | ||
else: | ||
return { | ||
'statusCode': 400, | ||
'body': json.dumps({ | ||
'error': 'Invalid input format' | ||
}) | ||
} | ||
|
||
# Extract the prompt | ||
prompt = body.get("prompt") | ||
if not prompt: | ||
return { | ||
'statusCode': 400, | ||
'body': json.dumps({ | ||
'error': 'Prompt is required' | ||
}) | ||
} | ||
|
||
print("Prompt = " + prompt) | ||
|
||
# Create the request body for Nova Micro | ||
request_body = json.dumps({ | ||
"system": [ | ||
{ | ||
"text": "You are a helpful AI assistant that provides accurate and concise information." | ||
} | ||
], | ||
"messages": [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"text": prompt | ||
} | ||
] | ||
} | ||
], | ||
"inferenceConfig": { | ||
"maxTokens": 1024, | ||
"temperature": 0.7, | ||
"topP": 0.9, | ||
"topK": 50, | ||
"stopSequences": [] | ||
} | ||
}) | ||
|
||
# Invoke Bedrock API with Nova Micro model | ||
try: | ||
response = bedrock.invoke_model( | ||
body=request_body, | ||
modelId='amazon.nova-micro-v1:0', | ||
accept='application/json', | ||
contentType='application/json' | ||
) | ||
|
||
# Parse the response body | ||
response_body = json.loads(response['body'].read()) | ||
print("Nova Micro Response:", response_body) | ||
|
||
# Extract text from the nested response structure | ||
generated_text = response_body.get('output', {}).get('message', {}).get('content', [{}])[0].get('text', '') | ||
|
||
return { | ||
'statusCode': 200, | ||
'headers': { | ||
'Content-Type': 'application/json', | ||
'Access-Control-Allow-Origin': '*' | ||
}, | ||
'body': json.dumps({ | ||
'generated-text': generated_text | ||
}) | ||
} | ||
|
||
except botocore.exceptions.ClientError as e: | ||
print(f"Bedrock API Error: {str(e)}") | ||
return { | ||
'statusCode': 500, | ||
'headers': { | ||
'Content-Type': 'application/json', | ||
'Access-Control-Allow-Origin': '*' | ||
}, | ||
'body': json.dumps({ | ||
'error': f"Bedrock API Error: {str(e)}" | ||
}) | ||
} | ||
|
||
except Exception as e: | ||
print(f"Error: {str(e)}") | ||
import traceback | ||
print(f"Traceback: {traceback.format_exc()}") | ||
return { | ||
'statusCode': 500, | ||
'headers': { | ||
'Content-Type': 'application/json', | ||
'Access-Control-Allow-Origin': '*' | ||
}, | ||
'body': json.dumps({ | ||
'error': str(e), | ||
'traceback': traceback.format_exc() | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.