# aws-samples/amazon-bedrock-rag architecture

> Architecture diagram of the GitHub repository aws-samples/amazon-bedrock-rag, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aws-samples/amazon-bedrock-rag
- Repository: https://github.com/aws-samples/amazon-bedrock-rag

## Overview

1. The Backend: Built as a serverless solution using AWS CDK, several Lambda functions (located under backend/lambda subdirectories) handle key operations like crawling websites, ingesting documents, querying the knowledge base, and managing data sources.
2. The Frontend: A React-based interface (located in the frontend directory) that allows users to interact with the service (e.g., entering the API Gateway endpoint, selecting models, and posing queries).

##### Main Components
a. Frontend Application: Represent this as the client layer (UI), with components for chat, selecting RAG models, and managing website data sources.
b. API Gateway: Acts as the entry point for all frontend requests to the backend.
c. Backend Serverless Components:
- Lambda Functions – Several specialized functions such as:
- crawl: for web crawling and extracting content from designated websites.
- dataSource: for connecting to and refreshing data from various sources.
- getUrls: handling URL retrieval operations.
- ingest: processing content ingestion (from both S3 and web sources).
- query: executing search queries against the knowledge base.
- webUrlSources: likely for managing or updating website source configuration.
- CDK Stack (backend/lib/backend-stack.ts) – Represents the infrastructure as code which provisions resources like API Gateway, S3 buckets, AWS Lambda functions, and other security/monitoring integrations.
c. Storage and Data Sources:
- S3 Bucket: Stores documents (e.g., the Amazon annual report) and triggers Lambda ingestion processes.
- Amazon OpenSearch Serverless: Provides the vector index (vector search collection) for storing and comparing embeddings.
d. Amazon Bedrock and Associated Foundational Models:
- Integration with Amazon Bedrock (for both retrieval models such as Amazon Titan Embeddings and the chosen generative model, defaulting to Anthropic Claude Instant).

##### Relationships and Interactions
- The Frontend sends requests via the API Gateway.
- API Gateway routes these requests to the appropriate Lambda functions.
- Some Lambda functions are event-driven (e.g., S3 bucket triggers automatic ingestion workflows) while others may be directly invoked via HTTP.
- The ingestion pipeline takes data from S3 or crawled from websites, splits and embeds content using the specified foundational models, and writes these embeddings to the OpenSearch vector store.
- The query Lambda then utilizes the vector store to fetch relevant data for answer generation.
- Optionally, the solution integrates with Amazon Bedrock to select and use the desired language models during the generation phase.

##### Architectural Patterns and Design Principles
- Serverless Architecture: Maximizes managed AWS services such as Lambda, API Gateway, S3, and OpenSearch Serverless.
- Microservices/Multi-lambda Functions: Separation of concerns with each Lambda handling a specific part of the workflow.
- Infrastructure as Code: Usage of AWS CDK to provision and manage cloud resources.
- Security By Design: API Gateway restricted by allowed IP, encrypted S3 storage enforcing SSL, and Web Application Firewall integration.

##### Detail and Clarity
- Every key component is represented.
- Detail the interactions, especially the flow from ingestion (documents and web content) to data processing and finally to generating responses using Amazon Bedrock.
- The backend’s internal structure (multiple lambda functions) should be clearly separated to show responsibilities across crawling, ingestion, data source updating, and query handling.

This approach will give other stakeholders, including developers and cloud architects, a clear understanding of how the pieces fit together and the interactions that support the end-to-end process.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        FE["Frontend React App"]:::frontend
    end

    %% Infrastructure as Code
    subgraph "Infrastructure"
        CDK["CDK Stack"]:::cdk
    end

    %% API Layer
    subgraph "API Layer"
        APIGW["API Gateway"]:::apigw
    end

    %% Backend Lambda Functions
    subgraph "Backend Lambda Functions"
        LambdaCrawl["Lambda: Crawl"]:::lambda
        LambdaDS["Lambda: Data Source"]:::lambda
        LambdagetUrls["Lambda: getUrls"]:::lambda
        LambdaIngest["Lambda: Ingest"]:::lambda
        LambdaQuery["Lambda: Query"]:::lambda
        LambdaWebUrls["Lambda: Web URL Sources"]:::lambda
    end

    %% AWS Services
    subgraph "AWS Services"
        S3["Amazon S3 Bucket – Doc Storage"]:::aws
        OSS["Amazon OpenSearch (Vector Store)"]:::aws
        Bedrock["Amazon Bedrock – Generative Model"]:::aws
    end

    %% Connections: Frontend to API Gateway
    FE -->|"sendsRequest"| APIGW

    %% Connections: CDK provisions API Gateway
    CDK -->|"provisions"| APIGW

    %% Connections: API Gateway routes to Lambda functions
    APIGW -->|"routesRequest"| LambdaCrawl
    APIGW -->|"routesRequest"| LambdaDS
    APIGW -->|"routesRequest"| LambdagetUrls
    APIGW -->|"routesRequest"| LambdaIngest
    APIGW -->|"routesRequest"| LambdaQuery
    APIGW -->|"routesRequest"| LambdaWebUrls

    %% Lambda interactions and AWS Service integrations
    LambdaCrawl -->|"sendsContent"| LambdaIngest
    S3 -->|"triggersIngest"| LambdaIngest
    LambdaIngest -->|"writesEmbeddings"| OSS
    LambdaQuery -->|"fetchesResults"| OSS
    LambdaQuery -->|"callsModel"| Bedrock

    %% Click Events
    click FE "https://github.com/aws-samples/amazon-bedrock-rag/tree/main/frontend/"
    click APIGW "https://github.com/aws-samples/amazon-bedrock-rag/blob/main/backend/lib/backend-stack.ts"
    click LambdaCrawl "https://github.com/aws-samples/amazon-bedrock-rag/blob/main/backend/lambda/crawl/index.js"
    click LambdaDS "https://github.com/aws-samples/amazon-bedrock-rag/blob/main/backend/lambda/dataSource/index.js"
    click LambdagetUrls "https://github.com/aws-samples/amazon-bedrock-rag/blob/main/backend/lambda/getUrls/index.js"
    click LambdaIngest "https://github.com/aws-samples/amazon-bedrock-rag/blob/main/backend/lambda/ingest/index.js"
    click LambdaQuery "https://github.com/aws-samples/amazon-bedrock-rag/blob/main/backend/lambda/query/index.js"
    click LambdaWebUrls "https://github.com/aws-samples/amazon-bedrock-rag/blob/main/backend/lambda/webUrlSources/index.js"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef apigw fill:#FFA07A,stroke:#000,stroke-width:2px;
    classDef lambda fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef aws fill:#FFFF99,stroke:#000,stroke-width:2px;
    classDef cdk fill:#D3D3D3,stroke:#000,stroke-width:2px;
```
