AWS Builder Center

Build a Serverless RAG Chatbot on AWS with Amazon Bedrock

and Keep the Bill Under $5

a student of computer science and engineering
Most "chat with your documents" tutorials either stop at a demo or quietly cost you $50+ a month in idle infrastructure. In this guide, you will build a fully serverless Retrieval-Augmented Generation (RAG) chatbot that answers questions from your own PDFs, using:
  • Amazon S3 for your documents
  • Amazon Bedrock Knowledge Bases for chunking, embedding and retrieval
  • AWS Lambda + Amazon API Gateway for the backend
  • A tiny static frontend (optional) for the chat UI
You pay only when someone asks a question. At low traffic, that is pennies.
By the end you will have a working API endpoint, a clear mental model of how RAG works on AWS, and a checklist of the mistakes that trip up most beginners.
Why this matters
Foundation models are smart, but they do not know your company handbook, your product docs, or your study notes. Two ways to fix that:
  1. Fine-tune a model. Expensive, slow, and stale the moment your documents change.
  1. Use RAG. Retrieve the relevant passages at question time and hand them to the model as context.
RAG wins for most real-world use cases: it is cheaper, easy to update (just upload a new file), and answers can point back to their source, which reduces hallucinations.
User → API Gateway → Lambda → Bedrock Knowledge Base → (vector store + S3 docs)
↓
Foundation model → Answer + citations

Flow:
  1. You upload documents to S3.
  1. The Knowledge Base splits them into chunks, creates embeddings, and stores them in a vector store.
  1. A user asks a question through your API.
  1. Lambda calls Bedrock, which retrieves the most relevant chunks and generates a grounded answer.
  1. The response comes back with source citations.



Prerequisites

  • An AWS account (Free Tier is fine to start)
  • Access enabled for the foundation and embedding models you plan to use in the Amazon Bedrock console (Model access page)
  • Basic Python knowledge
  • 3 to 5 sample PDFs or text files
Tip: Pick a region where your chosen models are available, and keep every resource in that same region. Region mismatch is the number one cause of "AccessDenied" and "model not found" errors.



Step 1: Upload your documents to S3

  1. Create a bucket, for example  my-rag-docs-<your-initials> .
  1. Keep Block all public access turned on.
  1. Upload your documents.
Documents that are well-structured (clear headings, short sections) produce noticeably better answers than messy scans.



Step 2: Create a Bedrock Knowledge Base

  1. Open Amazon Bedrock → Knowledge Bases → Create.
  1. Choose S3 as the data source and point it at your bucket.
  1. Select an embedding model.
  1. For the vector store, let the console quick-create one for you.
  1. Click Sync to ingest your documents.
Chunking is where quality is won or lost:
Setting
When to use it
Default chunking
Fine for a first pass
Smaller chunks
Factual lookups, FAQs
Larger chunks
Narrative docs, policies
Hierarchical chunking
Long documents with nested structure
Start with defaults, test with 10 real questions, then adjust.
Cost warning: Some vector store options keep a minimum compute capacity running even when idle. Check the pricing page for the store you pick, and delete it when you finish experimenting.



Step 3: Test inside the console

Use the built-in Test knowledge base panel before writing any code. Ask a question you know the answer to. Check:
  • Is the answer correct?
  • Do the citations point to the right file and passage?
  • What happens when you ask something that is not in your documents?
That last test matters. A good RAG system says "I don't know" instead of inventing something.



Step 4: Write the Lambda function

Create a Python Lambda function and use the Bedrock Agent Runtime API:
python
import json
import os
import boto3

client = boto3.client("bedrock-agent-runtime")

KB_ID = os.environ["KNOWLEDGE_BASE_ID"]
MODEL_ARN = os.environ["MODEL_ARN"]

def lambda_handler(event, context):
body = json.loads(event.get("body", "{}"))
question = body.get("question", "").strip()

if not question:
return _response(400, {"error": "Please provide a question."})

result = client.retrieve_and_generate(
input={"text": question},
retrieveAndGenerateConfiguration={
"type": "KNOWLEDGE_BASE",
"knowledgeBaseConfiguration": {
"knowledgeBaseId": KB_ID,
"modelArn": MODEL_ARN,
},
},
)

answer = result["output"]["text"]
sources = []
for citation in result.get("citations", []):
for ref in citation.get("retrievedReferences", []): location = ref.get("location", {}).get("s3Location", {})
if location.get("uri"):
sources.append(location["uri"])

return _response(200, {"answer": answer, "sources": sorted(set(sources))})

def _response(status, payload):
return {
"statusCode": status,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
"body": json.dumps(payload),
}
Set two environment variables on the function:
  •  KNOWLEDGE_BASE_ID : from the Knowledge Base overview page
  •  MODEL_ARN : the ARN of the generation model you enabled

IAM permissions (least privilege)

Give the Lambda execution role only what it needs:
json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:RetrieveAndGenerate",
"bedrock:Retrieve",
"bedrock:InvokeModel"
],
"Resource": "*"
}
]
}
Once it works, tighten  Resource  to your specific Knowledge Base and model ARNs.



Step 5: Expose it with API Gateway

  1. Create an HTTP API (cheaper and simpler than REST API for this use case).
  1. Add a  POST /ask  route integrated with your Lambda.
  1. Enable CORS if a browser frontend will call it.
  1. Deploy and copy the invoke URL. Test it:
bash
curl -X POST https://YOUR_API_ID.execute-api.YOUR_REGION.amazonaws.com/ask \
-H "Content-Type: application/json" \
-d '{"question": "What is our refund policy?"}'
You should get back a grounded answer plus the S3 files it came from.



Step 6 (optional): A simple chat UI

Host a single  index.html  on S3 static hosting behind CloudFront, with a text box that calls your  /ask  endpoint using  fetch . Keep it minimal: input, send button, answer, sources list.



Making answers better: 5 lessons I would give my past self

  1. Clean your documents first. Garbage in, garbage out. Remove headers, footers and duplicate pages.
  1. Write real test questions. Collect 15 to 20 questions users actually ask and re-run them after every change.
  1. Add a system instruction. Tell the model to answer only from retrieved context and to say when it cannot find the answer.
  1. Always show citations. They build trust and make debugging far easier.
  1. Add Guardrails. Amazon Bedrock Guardrails can filter sensitive topics and personal data before anything reaches your users.



Common errors and fixes

Symptom
Likely cause
Fix
 AccessDeniedException 
Model access not enabled
Enable the model in Bedrock console, same region
Empty or vague answers
Documents not synced
Re-run the data source sync
Wrong answers with confident tone
Chunks too large or too small
Adjust chunking and retest
Lambda timeout
Default 3-second limit
Raise timeout to 30 seconds or more
CORS error in browser
Missing headers
Enable CORS on API Gateway and return the headers



What will this cost?

Your bill has four parts:
  • Model usage: per input and output token, so it scales with traffic
  • Embeddings: a small one-time cost when you sync documents
  • Vector store: check the minimum idle cost of the option you choose
  • Lambda and API Gateway: negligible at low volume
Cost-saving habits: set an AWS Budget alert on day one, delete the Knowledge Base and vector store when you finish experimenting, and keep prompts short.
Always confirm current rates on the official AWS pricing pages, since they change.



Where to go next

  • Add conversation memory so follow-up questions work
  • Connect additional data sources such as web pages or Confluence
  • Turn the chatbot into an agent that can take actions (create tickets, look up orders)
  • Add authentication with Amazon Cognito
  • Describe the whole stack in AWS CDK or SAM so you can deploy it with one command
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