Ravindra BagaleCourses & study guides Track your progress

RAG on AWS: Bedrock Knowledge Bases with S3 (Step by Step)

Generative AI course · Chapter 47 · Estimated reading time: 12 minutes

In the RAG chapter you built every step of a PDF chatbot yourself in Python: read, chunk, embed, retrieve, answer. That is the best way to learn. In a company, though, the documents often already sit in Amazon S3, and the team wants AWS to run the boring parts. Amazon Bedrock Knowledge Bases is a managed RAG service: you point it at an S3 bucket, it chunks and embeds the files, stores the vectors, and answers questions with citations to the source file. In this chapter you will set up a knowledge base for HR policy PDFs, ask questions with cited answers in the console and in Python, and clean everything up so nothing keeps billing.

Related chapter and guide

To see how RAG works inside, step by step in Python, read What Is RAG? Build a Chatbot from Your Own PDF (Step by Step). For a quick no-code and Python project, see the guide How to Build a RAG Chatbot from a PDF: No-Code and Python. This chapter does the same job on AWS, managed for you.

What you will learn in this chapter

  • What a Bedrock knowledge base does for you, and what you still decide
  • The 4 parts: S3 data source, embedding model, vector store, answer model
  • Example 1: HR policy PDFs in S3 → a knowledge base
  • Example 2: cited answers in the console and in Python
  • Example 3: clean up, so nothing keeps billing
  • How to keep answers grounded and private

1. Suppose HR answers the same leave question 30 times a month

Suppose Bluehill Softworks has 4 HR policy PDFs: leave, travel, work from home and laptop use. Employees ask HR the same questions again and again: "How many casual leave days do I get?", "Can I claim a cab after 10 pm?"

The PDFs already live in an S3 bucket. The IT team wants a bot that answers only from these PDFs, shows which file the answer came from, and runs inside the company's AWS account. A knowledge base does that with no servers to manage.

2. The 4 parts of a knowledge base

  1. Data source: an S3 bucket (or a folder in it) with your files: PDF, Word, text, HTML and other common formats.
  2. Embedding model: turns each chunk into numbers, for example Amazon Titan Text Embeddings V2. (Embeddings and vector databases were the subject of earlier chapters.)
  3. Vector store: where the vectors live. The console can quick-create one for you; one option is Amazon S3 Vectors, a low-cost vector store inside S3. Amazon OpenSearch Serverless and Aurora PostgreSQL with pgvector are other options.
  4. Answer model: a text model on Bedrock that writes the final answer from the retrieved chunks, with citations.

Bedrock handles chunking, embedding and retrieval. You still decide which files go in, who can ask, which model answers, and you check the answers.

A Bedrock knowledge base: S3 files to cited answers Steps from top to bottom: 1 Files in S3, Current, approved, private bucket; 2 Sync, Chunk and embed the files; 3 Vector store, For example S3 Vectors; 4 Retrieve and answer, A model writes from the chunks; 5 Cite and check, Show the source file every time. 1Files in S3Current, approved, private bucket 2SyncChunk and embed the files 3Vector storeFor example S3 Vectors 4Retrieve and answerA model writes from the chunks 5Cite and checkShow the source file every time

A Bedrock knowledge base: S3 files to cited answers

Ravindra Bagale's Tip

The mistake: uploading a whole shared drive, old drafts and all, into the knowledge base "so it knows everything". Then the bot quotes the 2021 travel policy. Put only the current, approved files in one dedicated bucket or folder, with the year in each file name, and re-sync whenever a policy changes. A knowledge base is only as good as the files you give it.

3. Worked example 1: HR PDFs in S3 → a knowledge base

Costs first. Building and testing uses paid services: embedding the files, the vector store, and every question you ask. For 4 small PDFs the cost is low, but check the Amazon Bedrock and S3 Vectors pricing pages, keep a budget alert on (see the Bedrock chapter), and do the clean-up in Example 3 the same day.

Steps:

  1. Create a bucket in S3, for example dummy-policies-bucket (bucket names must be unique, so add your own word). Keep Block all public access on.
  2. Upload the 4 PDFs (use dummy policies for practice, never real employee data).
  3. Open Amazon Bedrock in the same Region. In the left menu choose Knowledge Bases → Create → Knowledge Base with vector store.
  4. Give it a name, such as hr-policies-kb, and let it create a new service role.
  5. Choose Amazon S3 as the data source and select your bucket.
  6. Keep the default chunking for a first test.
  7. Choose an embedding model, for example Amazon Titan Text Embeddings V2.
  8. For the vector store, choose Quick create a new vector store and pick Amazon S3 Vectors.
  9. Review and Create. When it is ready, select the data source and choose Sync. Wait until the status says the sync is complete.

Expected output (yours will be similar, not the same): the knowledge base shows status Available, and the data source sync history shows 4 files scanned and 4 added, with 0 failed. If a file fails, the sync details show why (for example, a scanned PDF with no text or a file that is too large).

4. Worked example 2: cited answers in the console and in Python

In the console:

  1. Open your knowledge base and choose Test knowledge base.
  2. Choose an answer model (a small text model is fine for testing).
  3. Ask the questions below one by one.
How many casual leave days do I get in a year?
Can I claim a cab home after 10 pm? What is the limit?
What is the policy on installing my own software on the office laptop?
What is the company's share price today?

Expected output (yours will be similar, not the same): the first 3 answers are short and each shows a source link, such as leave-policy-2026.pdf. Open the source to check the exact lines. For the share price, a good set-up answers that it cannot find this in the documents. If it invents a number, tighten the prompt (below).

In Python, with bedrock-agent-runtime and retrieve_and_generate. We tested this code offline with botocore's Stubber, which checks every parameter against the real API but returns a fake reply. For real use, remove the Stubber lines and put in your knowledge base ID:

import boto3
from botocore.stub import Stubber

KB_ID = "KBDUMMY123"   # dummy: copy your knowledge base ID
MODEL_ARN = "arn:aws:bedrock:us-east-1::foundation-model/amazon.nova-micro-v1:0"
client = boto3.client("bedrock-agent-runtime", region_name="us-east-1",
                      aws_access_key_id="DUMMY", aws_secret_access_key="DUMMY")
request = {"input": {"text": "How many casual leave days do I get in a year?"},
           "retrieveAndGenerateConfiguration": {"type": "KNOWLEDGE_BASE",
               "knowledgeBaseConfiguration": {"knowledgeBaseId": KB_ID, "modelArn": MODEL_ARN}}}
fake = {"sessionId": "dummy-session", "output": {"text": "You get 8 casual leave days a year."},
        "citations": [{"generatedResponsePart": {"textResponsePart": {"text": "You get 8 casual leave days a year."}},
                       "retrievedReferences": [{"content": {"text": "Casual leave: 8 days per calendar year..."},
                           "location": {"type": "S3", "s3Location": {"uri": "s3://dummy-policies-bucket/leave-policy.pdf"}}}]}]}

with Stubber(client) as stub:
    stub.add_response("retrieve_and_generate", fake, request)
    r = client.retrieve_and_generate(**request)

print("ANSWER:", r["output"]["text"])
for i, c in enumerate(r["citations"], 1):
    for ref in c["retrievedReferences"]:
        print(f"SOURCE {i}: {ref['location']['s3Location']['uri']}")
        print(f"  quote: {ref['content']['text'][:60]}")
if not r["citations"]:
    print("WARNING: no sources returned. Do not trust this answer.")

Real output (19 October 2026, boto3 1.43; the reply is fake by design, so the code can be tested at no cost):

ANSWER: You get 8 casual leave days a year.
SOURCE 1: s3://dummy-policies-bucket/leave-policy.pdf
  quote: Casual leave: 8 days per calendar year...

The important habit is the last 2 lines: if no source comes back, do not trust the answer. Show the source to the user every time.

Keep answers grounded. In the knowledge base's generation settings you can edit the prompt template. Add a rule such as:

Answer only from the search results. If the answer is not in them, say:
"I could not find this in the HR policies. Please contact HR."
Mention the policy file name in your answer.

5. Worked example 3: clean up, so nothing keeps billing

A test knowledge base keeps costing money for storage, and the vector store may have its own charges. We wrote a small script that prints the clean-up commands in a safe order. All names and IDs are dummies; replace them with yours.

kb_id, ds_id = "KBDUMMY123", "DSDUMMY456"
vector_bucket, index = "dummy-kb-vectors", "dummy-kb-index"
docs_bucket, region = "dummy-policies-bucket", "us-east-1"
steps = [
    ("Delete the data source", f"aws bedrock-agent delete-data-source --knowledge-base-id {kb_id} --data-source-id {ds_id} --region {region}"),
    ("Delete the knowledge base", f"aws bedrock-agent delete-knowledge-base --knowledge-base-id {kb_id} --region {region}"),
    ("Delete the vector index", f"aws s3vectors delete-index --vector-bucket-name {vector_bucket} --index-name {index} --region {region}"),
    ("Delete the vector bucket", f"aws s3vectors delete-vector-bucket --vector-bucket-name {vector_bucket} --region {region}"),
    ("Empty and delete the documents bucket (only if you no longer need the files)", f"aws s3 rb s3://{docs_bucket} --force"),
    ("Check nothing is left", f"aws bedrock-agent list-knowledge-bases --region {region}"),
]
for n, (why, cmd) in enumerate(steps, 1):
    print(f"{n}. {why}\n   {cmd}")

Real output:

1. Delete the data source
   aws bedrock-agent delete-data-source --knowledge-base-id KBDUMMY123 --data-source-id DSDUMMY456 --region us-east-1
2. Delete the knowledge base
   aws bedrock-agent delete-knowledge-base --knowledge-base-id KBDUMMY123 --region us-east-1
3. Delete the vector index
   aws s3vectors delete-index --vector-bucket-name dummy-kb-vectors --index-name dummy-kb-index --region us-east-1
4. Delete the vector bucket
   aws s3vectors delete-vector-bucket --vector-bucket-name dummy-kb-vectors --region us-east-1
5. Empty and delete the documents bucket (only if you no longer need the files)
   aws s3 rb s3://dummy-policies-bucket --force
6. Check nothing is left
   aws bedrock-agent list-knowledge-bases --region us-east-1

We checked these operation names against the AWS SDK's own API definitions. You can also do all of this in the console: delete the knowledge base, then open S3 → Vector buckets and delete the index and bucket. The last step, aws s3 rb ... --force, deletes every file in the bucket, so read it twice.

6. Keep it private and safe

  • Least privilege: only the knowledge base's service role should read the policy bucket. Keep the bucket private.
  • Who can ask: a knowledge base answers anyone who can call it. Put it behind your company login, not on a public web page.
  • No personal data in the files unless your company has approved it, and Bedrock Guardrails can mask phone numbers and other personal details in answers.
  • Re-sync after changes, and remove old versions of policies.

7. Beginner mistakes and quick fixes

Mistake What happens Quick fix
Uploading old and draft files Wrong or outdated answers Only current, approved files, one folder
Forgetting to Sync The bot cannot find new files Sync after every upload or change
Scanned PDFs with no text Files fail or give empty answers Use text PDFs, or run text extraction first
Showing answers without sources Users cannot check anything Always show the cited file
A public bucket Policies leak Block all public access stays on
Not cleaning up a test Storage keeps billing Run the clean-up steps the same day

8. Try at home

Try at home

Task 1: plan the files

  1. List 3 to 5 documents you would put in a knowledge base.
  2. Rename them with the year, like leave-policy-2026.pdf.
  3. Remove any personal data.

Task 2: test questions

  1. Write 5 questions the documents answer and 2 they do not.
  2. Write the answer you expect for each.
  3. Use them to test the knowledge base (or the Python bot from the RAG chapter).

Task 3: the offline script

  1. Run the Stubber script for retrieve_and_generate.
  2. Remove the citations from the fake reply and run it again.
  3. Check that the WARNING line appears.

Recap

In short

  1. Bedrock Knowledge Bases is managed RAG on AWS: S3 files in, cited answers out.
  2. It has 4 parts: an S3 data source, an embedding model, a vector store (S3 Vectors can be quick-created) and an answer model.
  3. Create, Sync, then Test knowledge base in the console; in Python use retrieve_and_generate.
  4. Always show the cited source, and treat an answer with no source as untrusted.
  5. Edit the prompt template so it says "not found" instead of guessing.
  6. Clean up the knowledge base, vector store and test bucket the same day.

Use the RAG chapter to understand the steps, and a knowledge base when you want AWS to run them for you. Either way, your files and your test questions decide the quality.

Previous chapter: Amazon Bedrock for Beginners: Your First Gen AI App on AWS

Next chapter: Amazon Q and Kiro: Write and Fix AWS Cloud Code Faster with AI


Ravindra Bagale, trainer: linkedin.com/in/ravindra-bagale. Bluehill Softworks, its policies, buckets and IDs on this page are made up for learning. Bedrock Knowledge Bases and S3 Vectors steps were checked on the Amazon Bedrock and Amazon S3 user guides on 19 October 2026; console labels can change. The Python output was produced on 19 October 2026 with boto3 1.43 and a stubbed (fake) reply. AI output changes every time, so your output will be similar, not the same.