# aws-samples/bedrock-access-gateway architecture

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

- Interactive diagram: https://gitdiagram.com/aws-samples/bedrock-access-gateway
- Repository: https://github.com/aws-samples/bedrock-access-gateway

## Overview

##### Project Type and Purpose
- This is a backend proxy service providing OpenAI-compatible RESTful APIs for Amazon Bedrock models.
- It’s a serverless/containerized microservice that can be deployed either as Lambda functions behind API Gateway (for SSE streaming) or as a Fargate service behind an ALB.
- Its goal is to let OpenAI SDKs and tools work against Bedrock without code changes.

##### File Structure Analysis
- Top-level directories:
– deployment: CloudFormation templates for API Gateway+Lambda and ALB+Fargate
– src: application code and Dockerfiles
– scripts: helper scripts (push images to ECR)
– docs: supplemental guides (Usage, Troubleshooting, Security)
- src/api: Python FastAPI application
– app.py: main entrypoint
– auth.py: API key validation
– routers/: chat, embeddings, model endpoints
– models/: SQLAlchemy or Pydantic models for Bedrock interactions
– schema.py, setting.py: request/response schemas and config
- src/Dockerfile, Dockerfile_ecs: build images for Lambda and ECS
- deployment templates reference the container image URIs, Secrets Manager ARN, and default model settings

##### README Insights
- Describes two deployment options:
– API Gateway + Lambda (streaming SSE via Lambda Web Adapter)
– ALB + Fargate (low-latency, no cold starts)
- Features: streaming, chat, embeddings, multimodal, prompt caching, inference profiles

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    Client["Client\n(curl, OpenAI SDKs,\nLangChain)"]:::client

    %% AWS Services
    APIGW["API Gateway\n(REST API)"]:::aws
    ALB["Application Load Balancer"]:::aws
    SM["Secrets Manager"]:::store
    ECR["Amazon ECR"]:::store
    Bedrock["Amazon Bedrock\n(Foundation Models)"]:::aws
    CW["CloudWatch\n(Logging & Metrics)"]:::aws

    %% Compute Layer
    subgraph "FastAPI App (Lambda/ECS)" 
        direction TB
        A_app["app.py\n(Main Entrypoint)"]:::compute
        A_auth["auth.py\n(API Key Validation)"]:::compute
        A_chat["chat.py\n(Chat Router)"]:::compute
        A_embed["embeddings.py\n(Embeddings Router)"]:::compute
        A_model["model.py\n(Model Router)"]:::compute
        A_bedrockmod["bedrock.py\n(Bedrock Models)"]:::compute
        A_base["base.py\n(Base Models)"]:::compute
        A_schema["schema.py\n(Request/Response Schemas)"]:::compute
        A_setting["setting.py\n(Config Settings)"]:::compute
    end

    Lambda["AWS Lambda\n(Container via Web Adapter)"]:::compute
    Fargate["AWS Fargate Task\n(Behind ALB)"]:::compute

    %% CI/CD & Deployment
    ECRPush["push-to-ecr.sh\n(Image Push Script)"]:::compute
    CF_Lambda["BedrockProxy.template\n(API GW + Lambda)"]:::aws
    CF_Fargate["BedrockProxyFargate.template\n(ALB + Fargate)"]:::aws
    DockerfileLambda["Dockerfile\n(Lambda Image)"]:::compute
    DockerfileECS["Dockerfile_ecs\n(Fargate Image)"]:::compute

    %% Data Flow
    Client -->|HTTP/SSE| APIGW
    Client -->|HTTP/SSE| ALB

    APIGW -->|Invoke| Lambda
    ALB -->|Route| Fargate

    Lambda -->|Pulls Code| A_app
    Fargate -->|Pulls Code| A_app

    A_app -->|Validates Key| A_auth
    A_app -->|Routes| A_chat
    A_app -->|Routes| A_embed
    A_app -->|Routes| A_model

    Lambda -->|Fetch Secret| SM
    Fargate -->|Fetch Secret| SM

    A_app -->|Uses SDK| Bedrock
    Bedrock -->|Stream Response| A_app

    A_app -->|Logs/Met.| CW
    Lambda -->|Logs/Met.| CW
    Fargate -->|Logs/Met.| CW

    ECRPush -->|Push Image| ECR
    DockerfileLambda -->|Build Image| ECR
    DockerfileECS -->|Build Image| ECR

    CF_Lambda -->|Deploys| Lambda
    CF_Lambda -->|Configures| APIGW
    CF_Fargate -->|Deploys| Fargate
    CF_Fargate -->|Configures| ALB

    %% Click Events
    click A_app "https://github.com/aws-samples/bedrock-access-gateway/blob/main/src/api/app.py"
    click A_auth "https://github.com/aws-samples/bedrock-access-gateway/blob/main/src/api/auth.py"
    click A_chat "https://github.com/aws-samples/bedrock-access-gateway/blob/main/src/api/routers/chat.py"
    click A_embed "https://github.com/aws-samples/bedrock-access-gateway/blob/main/src/api/routers/embeddings.py"
    click A_model "https://github.com/aws-samples/bedrock-access-gateway/blob/main/src/api/routers/model.py"
    click A_bedrockmod "https://github.com/aws-samples/bedrock-access-gateway/blob/main/src/api/models/bedrock.py"
    click A_base "https://github.com/aws-samples/bedrock-access-gateway/blob/main/src/api/models/base.py"
    click A_schema "https://github.com/aws-samples/bedrock-access-gateway/blob/main/src/api/schema.py"
    click A_setting "https://github.com/aws-samples/bedrock-access-gateway/blob/main/src/api/setting.py"
    click DockerfileLambda "https://github.com/aws-samples/bedrock-access-gateway/tree/main/src/Dockerfile"
    click DockerfileECS "https://github.com/aws-samples/bedrock-access-gateway/tree/main/src/Dockerfile_ecs"
    click CF_Lambda "https://github.com/aws-samples/bedrock-access-gateway/blob/main/deployment/BedrockProxy.template"
    click CF_Fargate "https://github.com/aws-samples/bedrock-access-gateway/blob/main/deployment/BedrockProxyFargate.template"
    click ECRPush "https://github.com/aws-samples/bedrock-access-gateway/blob/main/scripts/push-to-ecr.sh"
    click Usage "https://github.com/aws-samples/bedrock-access-gateway/blob/main/docs/Usage.md"
    click Troubleshooting "https://github.com/aws-samples/bedrock-access-gateway/blob/main/docs/Troubleshooting.md"
    click Security "https://github.com/aws-samples/bedrock-access-gateway/blob/main/docs/Security.md"
    click Readme "https://github.com/aws-samples/bedrock-access-gateway/blob/main/README.md"

    %% Styles
    classDef client fill:#AEDFF7,stroke:#333,stroke-width:1px
    classDef aws fill:#F9DABF,stroke:#333,stroke-width:1px
    classDef compute fill:#D5F5E3,stroke:#333,stroke-width:1px
    classDef store fill:#E8DAEF,stroke:#333,stroke-width:1px
```
