Skip to content

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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions apigw-lambda-bedrock-nova-terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# API Gateway Integration with AWS Lambda and Amazon Bedrock Nova Micro (Terraform)

## Overview

This pattern demonstrates how to create a serverless API that leverages Amazon Bedrock's Nova Micro model through AWS Lambda for generating AI responses. The solution uses API Gateway to create a REST endpoint that processes requests and returns 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.
* [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.

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 that 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
44 changes: 44 additions & 0 deletions apigw-lambda-bedrock-nova-terraform/api_gateway.tf
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"
}
120 changes: 120 additions & 0 deletions apigw-lambda-bedrock-nova-terraform/bedrock_integration.py
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()
})
}
23 changes: 23 additions & 0 deletions apigw-lambda-bedrock-nova-terraform/create_lambda_layer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Create a directory for the layer
mkdir -p lambda-layer/python

# Navigate to the python directory
cd lambda-layer/python

# Create a requirements.txt file
echo "boto3" > requirements.txt
echo "botocore" >> requirements.txt

# Install the requirements
pip install -r requirements.txt -t .

# Go back to the lambda-layer directory
cd ..

# Zip the contents
zip -r boto3-bedrock-layer.zip python/

# Move back to the main project directory
cd ..

zip lambda_function.zip bedrock_integration.py
71 changes: 71 additions & 0 deletions apigw-lambda-bedrock-nova-terraform/example-pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"title": "API Gateway Integration with AWS Lambda and Amazon Bedrock Nova Micro (Terraform)",
"description": "This pattern demonstrates how to create a serverless API that leverages Amazon Bedrock's Nova Micro model through AWS Lambda for generating text responses. The architecture enables real-time text generation through a RESTful API endpoint.",
"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's Nova Micro model.",
"The Lambda function formats the request according to Nova Micro'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": "serverless-patterns/apigw-lambda-bedrock-nova-terraform",
"projectFolder": "apigw-lambda-bedrock-nova-terraform",
"templateFile": "apigw-lambda-bedrock-nova-terraform/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:",
"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",
"",
"Or use Python with the requests library:",
"import requests",
"response = requests.post('https://YOUR-API-ENDPOINT/dev/generate_content',",
" json={'prompt': 'What are the key benefits of using AWS services?'})",
"print(response.json())"
]
},
"cleanup": {
"text": [
"terraform destroy",
"terraform show"
]
},
"authors": [
{
"name": "Naresh Rajaram",
"image": "",
"bio": "Senior Partner Solutions Architect, AWS",
"linkedin": "https://www.linkedin.com/nareshrajaram"
}
]
}
Loading