# tyaba/aws-mlops-handson architecture

> Architecture diagram of the GitHub repository tyaba/aws-mlops-handson, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tyaba/aws-mlops-handson
- Repository: https://github.com/tyaba/aws-mlops-handson

## Overview

##### Main Subsystems
- Training Pipeline (Batch ML)
- Prediction Server (Online Inference)
- Infrastructure-as-Code / CI/CD

##### Enumerate Core AWS Components (from infra/…)
- VPC & Networking (infra/modules/network)
- IAM Roles & Policies (infra/modules/iam)
- S3 Bucket for Data & Models (infra/modules/s3)
- ECR Repositories for Container Images (infra/modules/ecr)
- ECS Cluster + Task Definitions
– ml_pipeline container (infra/modules/ecs/container_definitions/ml_pipeline.json)
– predict_api container (…/predict_api.json)
- ALB (infra/modules/alb) with Listener → ECS service
- Step Functions orchestration (infra/modules/stepfunctions)
- Terraform “root” (infra/.all) invoking all modules
- GitHub Actions workflow (/.github/workflows/test-mlops-handson.yml)

##### Sketching the Training Pipeline Flow
a. Trigger
– Manual CLI (“make run-ml”) or automated Step Functions StartExecution
b. Step Functions State Machine
– Calls an ECS “RunTask” for ml_pipeline container
c. ECS Task (ml/main.py)
– ml/dataset loads raw data (data/train_data.zip or S3)
– ml/preprocessor preprocesses features
– ml/model trains classifiers (sgd_classifier_ctr_model, optuna)
– ml/logger records metrics
– Upload trained model/artifacts to S3
d. (Optional) Downstream: Model registered or results pulled

##### Sketching the Prediction Server Flow
a. Client HTTP Request → ALB
b. ALB Listener & Target Group → ECS Service (predict_api)
c. Predictor Container (predictor/app.py)
– On startup, fetch model from S3 via ml/aws/controller.py
– serve REST endpoints (predictor/routers.py, predictor/request.py)
d. Returns CTR prediction JSON

##### Infrastructure Provisioning / CI
- Terraform code (infra/.all/main.tf) provisions all AWS resources
- Dockerfiles + Makefile build two images (ML pipeline & predictor)
- Images pushed to ECR (infra/modules/ecr)
- Task definitions updated in ECS (infra/modules/ecs)
- GitHub Actions can run tests, then apply Terraform, build & push images

##### Architectural Patterns & Design Principles
- Separation of concerns:
– ML training (batch) vs. inference (online)
– IaC (Terraform) separated into reusable modules (network, iam, s3, etc.)
- Event-driven orchestration with AWS Step Functions
- Containerized workloads on ECS Fargate
- REST API behind ALB for horizontal scaling
- Strong Dev/Ops practices: Makefile, Poetry, pytest, GitHub Actions

##### Final Checklist
- Every AWS resource labeled (e.g., “ECS Cluster: mlops-cluster”)
- Containers annotated with image names (e.g., “ml-pipeline:latest”, “predict-api:latest”)
– Code build → ECR push
– Terraform apply → AWS resource creation
– Step Functions → ECS Task Run
– ECS → S3 (read/write)
– Client → ALB → ECS
– Compute vs. Storage vs. Orchestration vs. IaC
- Optionally, annotate file paths next to each major component (e.g., “infra/modules/ecs/…/predict_api.json”)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Dev/CI Layer
    subgraph "Dev/CI Layer"
        direction TB
        GH["GitHub Actions Workflow"]:::ci
        Make["Makefile"]:::ci
        DC["docker-compose.yaml"]:::ci
        DF["Dockerfile"]:::ci
        TS["bin/test-scale.sh"]:::ci
    end

    %% AWS Control Plane Layer
    subgraph "AWS Control Plane" 
        direction TB
        TF["Terraform Root"]:::iac
        subgraph "Modules" 
            direction TB
            MNet["network"]:::iac
            MIAM["iam"]:::iac
            MS3MOD["s3"]:::iac
            MECRMOD["ecr"]:::iac
            MECSMOD["ecs"]:::iac
            MALBMOD["alb"]:::iac
            MSF["stepfunctions"]:::iac
        end
    end

    %% AWS Data Plane Layer
    subgraph "AWS Data Plane"
        direction TB
        VPC["VPC & Networking"]:::network
        ALB["ALB (Listener & TG)"]:::network
        ECSCluster["ECS Cluster: mlops-cluster"]:::compute
        subgraph "ECS Services" 
            direction TB
            subgraph "ml_pipeline Task" 
                direction TB
                ML["ml-pipeline:latest"]:::compute
                subgraph "ml folder modules"
                    direction TB
                    DLoader["dataset"]:::compute
                    Preproc["preprocessor"]:::compute
                    MModel["model"]:::compute
                    MLogger["logger"]:::compute
                    MPath["path"]:::compute
                end
            end
            subgraph "predict_api Service"
                direction TB
                API["predict-api:latest"]:::compute
                subgraph "predictor folder modules"
                    direction TB
                    App["app"]:::compute
                    Routers["routers"]:::compute
                    Req["request"]:::compute
                    Setup["setup"]:::compute
                    Controller["ml/aws/controller"]:::compute
                end
            end
        end
        SF["Step Functions"]:::orchestration
        S3["S3 Bucket"]:::storage
        ECR["ECR Repos"]:::storage
    end

    %% Connections
    GH -->|runs| Make
    GH -->|triggers| TF
    GH -->|builds images via| DF
    Make -->|up| DC
    Make -->|executes tests| TS
    DF -->|build & push| ECR
    TF -->|provisions| VPC
    TF -->|deploys modules| MNet & MIAM & MS3MOD & MECRMOD & MECSMOD & MALBMOD & MSF
    SF -->|StartExecution| ECSCluster
    ECSCluster -->|runs Task| ML
    ML -->|PUT model.tar.gz| S3
    API -->|GET /predict| ALB
    ALB -->|routes to| API
    API -->|s3 fetch model| S3

    %% Layer Orchestration
    SF -->|invokes| ML
    ECR -->|stores images| ML & API

    %% Styling
    classDef ci fill:#fde047,stroke:#333,stroke-width:1px
    classDef iac fill:#fb923c,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
    classDef network fill:#93c5fd,stroke:#333,stroke-width:1px
    classDef compute fill:#4ade80,stroke:#333,stroke-width:1px
    classDef storage fill:#fde68a,stroke:#333,stroke-width:1px
    classDef orchestration fill:#fb7185,stroke:#333,stroke-width:1px

    %% Click Events Dev/CI
    click GH "https://github.com/tyaba/aws-mlops-handson/blob/main/.github/workflows/test-mlops-handson.yml"
    click Make "https://github.com/tyaba/aws-mlops-handson/tree/main/Makefile"
    click DC "https://github.com/tyaba/aws-mlops-handson/blob/main/docker-compose.yaml"
    click DF "https://github.com/tyaba/aws-mlops-handson/tree/main/Dockerfile"
    click TS "https://github.com/tyaba/aws-mlops-handson/blob/main/bin/test-scale.sh"

    %% Click Events Terraform Modules
    click TF "https://github.com/tyaba/aws-mlops-handson/blob/main/infra/.all/main.tf"
    click MNet "https://github.com/tyaba/aws-mlops-handson/blob/main/infra/modules/network/main.tf"
    click MIAM "https://github.com/tyaba/aws-mlops-handson/blob/main/infra/modules/iam/main.tf"
    click MS3MOD "https://github.com/tyaba/aws-mlops-handson/blob/main/infra/modules/s3/main.tf"
    click MECRMOD "https://github.com/tyaba/aws-mlops-handson/blob/main/infra/modules/ecr/main.tf"
    click MECSMOD "https://github.com/tyaba/aws-mlops-handson/blob/main/infra/modules/ecs/main.tf"
    click MALBMOD "https://github.com/tyaba/aws-mlops-handson/blob/main/infra/modules/alb/main.tf"
    click MSF "https://github.com/tyaba/aws-mlops-handson/blob/main/infra/modules/stepfunctions/main.tf"

    %% Click Events ECS Definitions
    click ML "https://github.com/tyaba/aws-mlops-handson/blob/main/infra/modules/ecs/container_definitions/ml_pipeline.json"
    click API "https://github.com/tyaba/aws-mlops-handson/blob/main/infra/modules/ecs/container_definitions/predict_api.json"

    %% Click Events ML Code
    click DLoader "https://github.com/tyaba/aws-mlops-handson/blob/main/ml/dataset/data_loader.py"
    click Preproc "https://github.com/tyaba/aws-mlops-handson/blob/main/ml/preprocessor/base_preprocessor.py"
    click MModel "https://github.com/tyaba/aws-mlops-handson/blob/main/ml/model/base_model.py"
    click MLogger "https://github.com/tyaba/aws-mlops-handson/blob/main/ml/logger/logging_utils.py"
    click MPath "https://github.com/tyaba/aws-mlops-handson/blob/main/ml/path/path_utils.py"
    click ML "https://github.com/tyaba/aws-mlops-handson/blob/main/ml/main.py"

    %% Click Events Predictor Code
    click App "https://github.com/tyaba/aws-mlops-handson/blob/main/predictor/app.py"
    click Routers "https://github.com/tyaba/aws-mlops-handson/blob/main/predictor/routers.py"
    click Req "https://github.com/tyaba/aws-mlops-handson/blob/main/predictor/request.py"
    click Setup "https://github.com/tyaba/aws-mlops-handson/blob/main/predictor/setup.py"
    click Controller "https://github.com/tyaba/aws-mlops-handson/blob/main/ml/aws/controller.py"
```
