# aws-samples/codenator-automatic-code-generation-and-execution-using-llm architecture

> Architecture diagram of the GitHub repository aws-samples/codenator-automatic-code-generation-and-execution-using-llm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm
- Repository: https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm

## Overview

##### Project Type and Purpose
- Full-stack, microservices-style AWS cloud solution for automatic code generation, scanning, execution, and storage using Large Language Models (LLMs).
- Purpose: Orchestrate LLM code generation (via Bedrock/SageMaker), perform static security scans (CodeGuru/Semgrep), execute code in a sandboxed container, store tasks/results, and present a WebUI for user interaction.

##### Main Components
a. Frontend (WebUI)
- Gradio-based UI in src/codenator/web_ui
- User submits prompts, views code, approves executions, and sees results.
b. Controller (Orchestration Layer)
- In src/codenator/controller
- Central logic: retrieves prompts, invokes API layer, processes LLM responses, coordinates scanning, execution, and persistence.
- Prompt Store (DynamoDB) for prompt versioning.
c. API Layer (LLM Service)
- In src/codenator/api_layer
- Uniform interface to LLM providers (Bedrock, SageMaker).
- Runs on ECS Fargate, stores models in DynamoDB.
d. Code Scanner (Guardrail)
- In src/codenator/code_scanner
- Static analysis via Amazon CodeGuru or Semgrep.
e. Code Executor (Task Executor)
- In src/codenator/code_executor
- Runs code in encrypted, sandboxed Docker containers.
- Uses Amazon KMS for encryption.
f. Task Store
- In src/codenator/task_store
- Persists tasks and results in Amazon OpenSearch Serverless.
g. Shared Storage & Logging
- S3 buckets for shared files, execution logs, user feedback.
h. Optional Authentication Layer
- CloudFormation templates for Cognito integration (auth-layer.yaml, web-ui-auth.yaml).

##### Relationships & Data Flow
1. User → WebUI: prompt submission and approval.
2. WebUI → Controller: POST requests with user input.
3. Controller → Prompt Store (DynamoDB): fetch/store prompt templates.
4. Controller → API Layer: invoke LLM, retrieve generated code.
5. Controller → Code Scanner: submit generated code for static analysis.
6. Controller → (on pass) Code Executor: send encrypted code to run in a container.
7. Code Executor → S3: write execution output and logs.
8. Controller → Task Store (OpenSearch): index task metadata, code, results.
9. Controller → WebUI: return final results to user.
10. Logging & Feedback → S3: user-provided feedback stored.

##### Architectural Patterns & Principles
- Microservices: each component is a separate container/Docker image, independently deployable (Dockerfile per service).
- API-Oriented: well-defined HTTP handlers in each microservice.
- Event/Request-Driven orchestration by Controller.
- Infrastructure as Code: all AWS resources deployed via CloudFormation nested templates.
- Security by Design: encryption (KMS), manual approval gates, static analysis.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Codenator Services
    subgraph "Codenator Services"
        direction TB
        WebUI["WebUI\n(src/codenator/web_ui)"]:::compute
        Controller["Controller\n(src/codenator/controller)"]:::compute
        APILayer["API Layer\n(src/codenator/api_layer)"]:::compute
        CodeScanner["Code Scanner\n(src/codenator/code_scanner)"]:::compute
        CodeExecutor["Code Executor\n(src/codenator/code_executor)"]:::compute
        TaskStoreService["Task Store Service\n(src/codenator/task_store)"]:::compute
    end

    %% AWS Managed Services
    DynamoDB["DynamoDB\nPrompt Store"]:::datastore
    ModelDB["DynamoDB\nModel Database"]:::datastore
    S3["S3\nShared Storage & Logs"]:::datastore
    OpenSearch["OpenSearch Serverless"]:::datastore
    KMS["KMS\nEncryption"]:::security
    ECS["ECS Fargate"]:::compute
    Cognito["Cognito (Optional)"]:::optional

    %% CloudFormation
    subgraph "CloudFormation Stacks"
        direction TB
        RootCF["Root Template\n(root-template.yaml)"]:::optional
        BuildImagesCF["Build Images\n(build-images.yaml)"]:::optional
        WebUICF["WebUI Deployment\n(web-ui.yaml)"]:::optional
        ControllerCF["Controller Deployment\n(controller.yaml)"]:::optional
        APICF["API Layer Deployment\n(api-layer.yaml)"]:::optional
        ScannerCF["Code Scanner Deployment\n(code-scanner.yaml)"]:::optional
        ExecutorCF["Code Executor Deployment\n(code-executor.yaml)"]:::optional
        TaskStoreCF["Task Store Deployment\n(task-store.yaml)"]:::optional
        AOSSStoreCF["OpenSearch Store\n(aoss-store.yaml)"]:::optional
        AuthCF["Auth Layer\n(auth-layer.yaml)"]:::optional
        WebUIAuthCF["WebUI Auth\n(web-ui-auth.yaml)"]:::optional
    end

    %% User Interaction
    User["User"]:::external

    %% Flows
    User -->|submits prompt| WebUI
    WebUI -->|POST HTTP| Controller
    Controller -->|get/store| DynamoDB
    Controller -->|invoke LLM| APILayer
    APILayer -->|runs on| ECS
    Controller -->|scan code| CodeScanner
    CodeScanner -->|scan result| Controller
    Controller -->|encrypt & send| CodeExecutor
    CodeExecutor -->|uses| KMS
    CodeExecutor -->|run on| ECS
    CodeExecutor -->|write logs| S3
    Controller -->|index task| TaskStoreService
    TaskStoreService -->|backend| OpenSearch
    Controller -->|return result| WebUI
    User -->|feedback| S3

    %% Model DB flow
    APILayer -->|metadata| ModelDB

    %% CloudFormation relations
    RootCF --> BuildImagesCF
    RootCF --> WebUICF
    RootCF --> ControllerCF
    RootCF --> APICF
    RootCF --> ScannerCF
    RootCF --> ExecutorCF
    RootCF --> TaskStoreCF
    RootCF --> AOSSStoreCF
    RootCF --> AuthCF
    AuthCF --> WebUIAuthCF

    %% Click Events
    click WebUI "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/tree/main/src/codenator/web_ui"
    click Controller "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/tree/main/src/codenator/controller"
    click APILayer "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/tree/main/src/codenator/api_layer"
    click CodeScanner "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/tree/main/src/codenator/code_scanner"
    click CodeExecutor "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/tree/main/src/codenator/code_executor"
    click TaskStoreService "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/tree/main/src/codenator/task_store"
    click RootCF "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/root-template.yaml"
    click BuildImagesCF "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/build-images.yaml"
    click WebUICF "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/web-ui.yaml"
    click ControllerCF "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/controller.yaml"
    click APICF "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/api-layer.yaml"
    click ScannerCF "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/code-scanner.yaml"
    click ExecutorCF "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/code-executor.yaml"
    click TaskStoreCF "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/task-store.yaml"
    click AOSSStoreCF "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/aoss-store.yaml"
    click AuthCF "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/auth-layer.yaml"
    click WebUIAuthCF "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/web-ui-auth.yaml"
    click DynamoDB "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/prompt-store.yaml"
    click ModelDB "https://github.com/aws-samples/codenator-automatic-code-generation-and-execution-using-llm/blob/main/deployment/CloudFormation/model-db.yaml"

    %% Styles
    classDef compute fill:#C3D9FF,stroke:#0366d6,color:#000
    classDef datastore fill:#FFD8A8,stroke:#d97706,color:#000
    classDef security fill:#D1FAE5,stroke:#059669,color:#000
    classDef optional fill:#E5E7EB,stroke:#6B7280,color:#000
    classDef external fill:#FFFFFF,stroke:#000000,color:#000
```
