DEV Community
Follow
Implementing RAG with Terraform using AWS S3, Bedrock KnowledgeBase, OpenSearch Serverless, IAM
This post details the implementation of a Retrieval-Augmented Generation (RAG) system on AWS using Terraform, S3, Bedrock Knowledge Bases, and OpenSearch Serverless. RAG allows Large Language Models (LLMs) to leverage external knowledge sources for answer generation, improving accuracy. The RAG process involves two main phases: ingestion and querying. During ingestion, documents from S3 are chunked, converted into numerical embeddings by Bedrock, and stored in OpenSearch Serverless. The query phase involves converting a user's question into an embedding, retrieving relevant document chunks from OpenSearch, and using them as context for the LLM to generate grounded answers with citations.The system utilizes Amazon S3 as the document source and Amazon Bedrock Knowledge Bases to manage the ingestion pipeline. OpenSearch Serverless is configured as the vector database, employing a knn_vector field for embeddings and HNSW with FAISS for efficient similarity search. The OpenSearch index also stores original text and metadata, supporting vector, lexical, and metadata-filtered searches. Cosine similarity is used for vector comparison. Terraform scripts define the AWS infrastructure, including IAM roles, S3 buckets, Bedrock Knowledge Base configurations, and OpenSearch Serverless access policies. A Streamlit application provides a user interface for uploading documents to S3, triggering knowledge base synchronization, and submitting queries to the RAG system. This comprehensive setup offers a practical starting point for building custom RAG systems.