
Building a Serverless RAG Chatbot on AWS
Lessons from building a RAG chatbot using Amazon Bedrock, OpenSearch Serverless, and AWS SAM.
Building a Serverless RAG Chatbot on AWS
Introduction
I recently completed the AWS SimuLearn: Serverless Chatbot Using Private Data hands-on lab on AWS Skill Builder.
This was one of the most interesting labs in my AWS learning journey because it brought together several technologies I had previously explored separately: Amazon Bedrock, Retrieval-Augmented Generation (RAG), vector search, serverless architecture, and infrastructure as code.
The challenge was to build a chatbot capable of answering questions using private organizational documents instead of relying entirely on a foundation model's existing knowledge.
In this article, I'll share the architecture, the key components, and what I learned from building a serverless RAG application on AWS.
1. Understanding the Problem
Traditional large language models (LLMs) can generate useful responses, but they don't automatically have access to an organization's private documents.
Imagine a city organization with internal documents containing policies, procedures, and operational information.
Employees might ask questions such as:
"What is the procedure for submitting a service request?"
Without access to the organization's documentation, an LLM may provide a generic or inaccurate answer.
This is where Retrieval-Augmented Generation (RAG) becomes valuable.
Instead of relying only on the model's internal knowledge, RAG retrieves relevant information from external sources and provides that information as context for generating a response.
2. Exploring the AWS Architecture
The lab combined multiple AWS services to create a serverless chatbot.
AWS Service | Responsibility |
|---|---|
Amazon Bedrock | Provides foundation models and Knowledge Bases |
Amazon OpenSearch Serverless | Stores and searches vector embeddings |
Amazon S3 | Stores private documents |
AWS Lambda | Executes backend application logic |
Amazon API Gateway | Exposes the chatbot backend through an API |
Amazon Cognito | Handles user authentication |
AWS Amplify | Supports frontend application deployment |
Amazon CloudFront | Delivers application content |
Amazon SageMaker AI | Provides a code development environment |
AWS SAM | Builds and deploys serverless application resources |
I found it helpful to separate the architecture into three parts: document ingestion, application deployment, and question answering.
3. Building a Knowledge Base with Amazon Bedrock
The first important part was preparing private documents for retrieval.
The process involved:
- Storing documents in Amazon S3.
- Configuring an Amazon Bedrock Knowledge Base.
- Using Amazon OpenSearch Serverless as the vector store.
- Synchronizing the data source to make document content searchable.
During ingestion, documents are processed into smaller chunks, converted into vector embeddings, and indexed in the vector store.
These embeddings represent the semantic meaning of the content, enabling similarity-based retrieval.
For example, a user might ask:
"How do employees request time off?"
The retrieval system can find relevant document passages even if they don't contain the exact wording of the question.
One important lesson was understanding the difference between the services:
Amazon S3 stores the original documents, Amazon Bedrock Knowledge Bases manages retrieval integration, and OpenSearch Serverless provides the vector search capability.
4. Understanding the RAG Workflow
After the documents were indexed, the application could retrieve relevant context to support AI-generated answers.
The basic RAG workflow looks like this:
User Question → Knowledge Base Retrieval → Relevant Document Chunks → LLM → Generated Answer
In this lab, I explored how to retrieve relevant content through the Amazon Bedrock Knowledge Bases Retrieve API.
The retrieved information can then be included in a prompt sent to a foundation model through Amazon Bedrock.
This approach introduces an important distinction:
- Retrieval: Finding the information relevant to the user's question.
- Generation: Using that retrieved information to produce a natural-language response.
I also explored how prompt engineering and LangChain can help structure the interaction between retrieved content and the language model.
The key takeaway is that RAG does not retrain the foundation model. Instead, it provides additional context at inference time.
5. Building and Deploying the Serverless Application
Another valuable part of the lab was connecting the RAG functionality to a web application.
The application architecture included:
Frontend:
- AWS Amplify for application hosting and deployment.
- Amazon CloudFront for content delivery.
- Amazon Cognito for user authentication.
Backend:
- Amazon API Gateway for receiving requests.
- AWS Lambda for processing application logic.
- Amazon Bedrock for retrieval and model invocation.
The simplified request flow was:
User → Web Application → API Gateway → Lambda → Amazon Bedrock → Response
The backend coordinates retrieval and generation before returning the final answer to the user.
This helped me understand how an AI prototype can be integrated into a complete web application.
6. Deploying with AWS SAM
One of my favorite parts of this lab was working with AWS Serverless Application Model (AWS SAM).
Previously, I had learned about AWS CloudFormation and Terraform for infrastructure as code.
This lab demonstrated how AWS SAM simplifies the development and deployment of serverless applications.
I worked with the serverless deployment workflow involving commands such as:
sam build sam deploy AWS SAM builds the application artifacts and deploys the infrastructure through AWS CloudFormation.
This allows developers to manage resources such as Lambda functions and API Gateway APIs without manually creating every component in the AWS Console.
I learned that AWS SAM is particularly useful when developing applications centered on Lambda and other serverless services.
7. Security and Authentication
Because this application uses private organizational information, security is an important consideration.
Amazon Cognito provides user authentication, while IAM permissions control access between AWS services.
However, an important lesson is that authentication alone does not guarantee document-level access control.
For a production RAG application, developers should also consider:
- Restricting access to private documents.
- Applying least-privilege IAM permissions.
- Ensuring users can only retrieve information they are authorized to access.
- Protecting API endpoints and sensitive configuration.
- Evaluating model responses for accuracy and potential information disclosure.
RAG can improve the relevance of answers, but security and answer correctness still require careful design.
8. Key Lessons Learned
This lab reinforced several important concepts.
1. RAG makes foundation models more useful for domain-specific applications.
Instead of retraining a model, we can provide relevant information from private documents at inference time.
2. Vector search enables semantic retrieval.
OpenSearch Serverless can retrieve relevant document chunks based on semantic similarity rather than relying only on exact keywords.
3. Bedrock Knowledge Bases reduces integration complexity.
It helps manage document ingestion, embeddings, indexing, and retrieval workflows.
4. Serverless services simplify application development.
Lambda and API Gateway provide a backend without requiring developers to manage traditional application servers.
5. AWS SAM improves deployment workflows.
Infrastructure as code makes serverless applications easier to define, deploy, and update.
6. A working RAG application still needs evaluation.
Retrieval relevance, response accuracy, latency, cost, and access controls are important considerations beyond the initial lab.
9. Final Thoughts
Completing this lab helped me understand how several AWS services work together to build a practical generative AI application.
My previous AWS labs focused on prompt engineering, integrating foundation models, deploying AI agents, and configuring private access to Amazon Bedrock.
This time, I explored how to combine private data, vector search, LLMs, authentication, and serverless deployment into one application.
My biggest takeaway is:
Building an effective RAG application is not just about connecting an LLM to documents. It is about designing a reliable process for retrieving relevant information, generating useful answers, and securely delivering those answers to users.
For future exploration, I would like to experiment with retrieval evaluation, metadata filtering, source citations, and automated testing of RAG responses.
This was a valuable hands-on experience, and I'm excited to keep learning about production-ready generative AI architectures on AWS.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article