Course Overview All Courses Blog Reserve Bootcamp Seat
AWS for AI · Day 4 of 5 ~75 minutes

Knowledge Bases for Bedrock — Managed RAG

Upload your documents to S3, create a Knowledge Base, and query them with natural language. No vector DB to manage.

Day 1
Day 2
Day 3
4
Day 4
5
Day 5
What You'll Build Today

A document Q&A system using Bedrock Knowledge Bases. Upload 3-5 PDF or text documents, sync them into a Knowledge Base backed by OpenSearch, and query it via the AWS console and Python SDK — with citations pointing back to source documents.

1
What is RAG?

Retrieval-Augmented Generation in plain terms

RAG solves a fundamental problem: language models know a lot of general knowledge, but they don't know your documents — your policies, your product manuals, your internal research.

RAG works in two stages:

Retrieval: When a user asks a question, the system searches your document store for the most relevant chunks of text.
Augmentation: Those chunks are included in the prompt to the LLM as context. The LLM answers the question using your documents as the source of truth.

Bedrock Knowledge Bases handles both stages for you — the embedding, the vector search, the retrieval pipeline. You just point it at an S3 bucket and ask questions.

2
Create Knowledge Base

Set up the Knowledge Base in the console

Upload 3-5 PDF or text documents to a new S3 bucket. Then:

In Bedrock → Knowledge bases → Create knowledge base.
Name it my-docs-kb.
Data source: S3. Point to your bucket.
Embeddings model: Amazon Titan Embeddings G1 - Text (free tier).
Vector store: Amazon OpenSearch Serverless (auto-created).

Click Create. Wait 2-3 minutes for provisioning. Then click Sync to ingest your documents. Syncing takes 1-5 minutes depending on document size.

Cost note: OpenSearch Serverless has a minimum charge of ~$0.36/hour when active. Delete the Knowledge Base after this lesson to avoid ongoing charges. We show you how at the end.

3
Query via Python

Query the Knowledge Base programmatically

pythonquery_kb.py
import boto3

agent_runtime = boto3.client("bedrock-agent-runtime", region_name="us-east-1")

KNOWLEDGE_BASE_ID = "YOUR_KB_ID"  # from the console
MODEL_ARN = "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-opus-4-5-20251101-v1:0"

def query_docs(question):
    response = agent_runtime.retrieve_and_generate(
        input={"text": question},
        retrieveAndGenerateConfiguration={
            "type": "KNOWLEDGE_BASE",
            "knowledgeBaseConfiguration": {
                "knowledgeBaseId": KNOWLEDGE_BASE_ID,
                "modelArn": MODEL_ARN
            }
        }
    )
    answer = response["output"]["text"]
    citations = response["citations"]

    print(f"Answer: {answer}\n")
    print("Sources:")
    for c in citations:
        for ref in c.get("retrievedReferences", []):
            loc = ref["location"]["s3Location"]["uri"]
            print(f"  - {loc}")

query_docs("What are the main topics covered in these documents?")

Replace YOUR_KB_ID with the Knowledge Base ID from the Bedrock console (it looks like ABCDE12345).

Day 4 Complete

  • Understood the RAG pattern and why it exists
  • Created a Bedrock Knowledge Base backed by OpenSearch Serverless
  • Synced documents from S3 into the Knowledge Base
  • Queried documents with natural language via Python and got cited answers
Day 4 Done

Final Day: Deploy an AI App on AWS

Day 5 containerizes a FastAPI app that calls Bedrock, pushes it to ECR, and deploys it on App Runner. A real URL, a real app.

Go to Day 5
Finished this lesson?