AWS Builder Center

Building Serverless GenAI Apps: Getting Started with Amazon Bedrock & AWS Lambda

Generative AI is transforming how we build modern software, and AWS makes it remarkably easy to integrate foundational models into scalable applications without managing heavy infrastructure. In this post, I want to share a quick overview of how combining AWS Lambda with Amazon Bedrock provides a seamless, serverless path to building GenAI-powered applications.

Software Developer

Why Amazon Bedrock + AWS Lambda?

  • Zero Infrastructure Management: AWS Lambda automatically handles compute scaling, execution, and resource allocation.
  • Access to Top AI Models: Amazon Bedrock gives you single-API access to leading foundation models (Claude, Llama, Titan) without provisioning GPUs.
  • Cost-Efficiency: You only pay for the execution time of your Lambda functions and the tokens processed by Bedrock.

A Simple 3-Step Architecture

  1. API Gateway: Receives the user request (e.g., a prompt sent from a frontend or mobile app).
  1. AWS Lambda: Processes the incoming request, formats the prompt payload, and calls Bedrock via the AWS SDK (using  boto3  in Python or  @aws-sdk/client-bedrock-runtime  in Node.js).
  1. Amazon Bedrock: Evaluates the prompt with your model of choice and returns the generated text response back to Lambda and the user.
import json
import boto3

bedrock = boto3.client(service_name='bedrock-runtime')

def lambda_handler(event, context):
body = json.loads(event.get('body', '{}'))
user_prompt = body.get('prompt', 'Hello AWS!')
# Payload structure for Anthropic Claude models on Bedrock
payload = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 300,
"messages": [{"role": "user", "content": user_prompt}]
}
response = bedrock.invoke_model(
modelId='anthropic.claude-3-haiku-20240307-v1:0',
body=json.dumps(payload)
)
response_body = json.loads(response['body'].read())
output_text = response_body['content'][0]['text']
return {
'statusCode': 200,
'body': json.dumps({'response': output_text})
}
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article