Build a Serverless RAG Chatbot on AWS with Amazon Bedrock
and Keep the Bill Under $5
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:
- Fine-tune a model. Expensive, slow, and stale the moment your documents change.
- 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:
- You upload documents to S3.
- The Knowledge Base splits them into chunks, creates embeddings, and stores them in a vector store.
- A user asks a question through your API.
- Lambda calls Bedrock, which retrieves the most relevant chunks and generates a grounded answer.
- 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
- Create a bucket, for example
my-rag-docs-<your-initials>.
- Keep Block all public access turned on.
- 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
- Open Amazon Bedrock → Knowledge Bases → Create.
- Choose S3 as the data source and point it at your bucket.
- Select an embedding model.
- For the vector store, let the console quick-create one for you.
- 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 jsonimport osimport 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
- Create an HTTP API (cheaper and simpler than REST API for this use case).
- Add a
POST /askroute integrated with your Lambda.
- Enable CORS if a browser frontend will call it.
- 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
- Clean your documents first. Garbage in, garbage out. Remove headers, footers and duplicate pages.
- Write real test questions. Collect 15 to 20 questions users actually ask and re-run them after every change.
- Add a system instruction. Tell the model to answer only from retrieved context and to say when it cannot find the answer.
- Always show citations. They build trust and make debugging far easier.
- 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
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article