
How to Build Serverless AI with Lambda & Bedrock
Connect Next.js to Amazon Bedrock via AWS Lambda and API Gateway for pay-as-you-go AI integration.
Introduction
Integrating Large Language Models (LLMs) into modern web applications presents two primary challenges: security risks from exposing private API keys on the client side, and infrastructure bloat from managing dedicated backend servers.
By building a serverless AI microservice using AWS Lambda, Amazon Bedrock, and Amazon API Gateway, you can create a secure, scalable backend with pay-as-you-go pricing and zero infrastructure maintenance.
1. Architecture Overview
Below is the high-level request-response lifecycle of the serverless AI microservice:
+------------------+ +---------------------+ +--------------------+ +-----------------------+
| | HTTP POST | | Invokes | | boto3 Call| |
| Next.js Client | ---------> | Amazon API Gateway | ---------> | AWS Lambda | ---------> | Amazon Bedrock |
| (Frontend) | (Prompt) | (CORS & Routing) | (Payload) | (Python 3.12) | (Payload) | (Claude 3 Haiku) |
| | | | | | | |
+------------------+ +---------------------+ +--------------------+ +-----------------------+
^ |
| |
+-------------------------------------------------------------------------------------------------------+
Returns Generated Response
Request Flow Explained
- Next.js Frontend: Sends an HTTP
POSTrequest containing the user's prompt.
- Amazon API Gateway: Routes the request securely, handles client authentication (if configured), and manages CORS preflight headers.
- AWS Lambda: Receives the event payload, parses the JSON input, initializes the AWS SDK (
boto3), and invokes Amazon Bedrock.
- Amazon Bedrock: Calls the foundation model (e.g., Anthropic Claude 3 Haiku) and returns the generated text response back through Lambda and API Gateway to the client.
2. Step-by-Step Implementation Guide
Step 1: Request Model Access in Amazon Bedrock
Before invoking models via API, grant access in your target region (e.g.,
us-east-1 ):- Navigate to the Amazon Bedrock Console.
- Go to Model access in the left sidebar.
- Select Edit or Request access, choose Anthropic Claude 3 Haiku, and submit your request.
Step 2: Configure IAM Roles & Permissions
Your Lambda function requires permissions to invoke Amazon Bedrock models.
- Go to IAM Roles and locate the execution role assigned to your Lambda function.
- Create an inline policy with the following JSON policy structure:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "bedrock:InvokeModel",
"Resource": "*"
}
]
}
Step 3: Create the AWS Lambda Function
- Open the AWS Lambda Console and click Create function.
- Choose Runtime:
Python 3.12and selectarm64orx86_64.
- Add the Python code below to
lambda_function.py:
import json
import boto3
# Initialize Bedrock Runtime client outside the handler for connection reuse
bedrock = boto3.client(
service_name='bedrock-runtime',
region_name='us-east-1'
)
def lambda_handler(event, context):
try:
# Parse incoming JSON body
body = json.loads(event.get('body', '{}'))
prompt = body.get('prompt', 'Hello AWS!')
# Construct Anthropic Claude 3 native request payload
native_request = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 300,
"temperature": 0.7,
"messages": [
{
"role": "user",
"content": [{"type": "text", "text": prompt}]
}
]
}
# Invoke Bedrock model
response = bedrock.invoke_model(
modelId="anthropic.claude-3-haiku-20240307-v1:0",
body=json.dumps(native_request)
)
# Parse response body from stream
response_body = json.loads(response.get('body').read())
result_text = response_body['content'][0]['text']
# Return successful HTTP response with CORS headers
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps({'response': result_text})
}
except Exception as e:
return {
'statusCode': 500,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps({'error': str(e)})
}
Step 4: Set Up API Gateway
- Open Amazon API Gateway and create an HTTP API or REST API.
- Create a route for
POST /generate.
- Set the integration target to your newly created AWS Lambda function.
- Enable CORS: Set
Access-Control-Allow-Originto*or your specific Next.js domain, and allowPOSTandOPTIONSmethods.
- Deploy the API to a stage (e.g.,
prod) and copy the Invoke URL.
Step 5: Integrate with Next.js Frontend
Call your deployed endpoint directly from your Next.js application using standard
fetch :// Helper function to call the AWS Lambda Bedrock API endpoint
async function generateAIResponse(userPrompt) {
try {
const response = await fetch('YOUR_API_GATEWAY_ENDPOINT_URL/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ prompt: userPrompt }),
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
console.log("AI Response:", data.response);
return data.response;
} catch (error) {
console.error("Error fetching AI response:", error);
}
}
3. Key Benefits
- Zero Infrastructure Management: AWS Lambda automatically provisions compute power and scales seamlessly without active server management.
- Cost Efficiency: Pay-per-request pricing combined with token-based pricing on Amazon Bedrock ensures minimal cost, especially for early-stage applications.
- Enhanced Security: Private credentials and API configs remain securely stored inside AWS IAM without client-side exposure.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article