# maadbreck/lab02-infracode architecture

> Architecture diagram of the GitHub repository maadbreck/lab02-infracode, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/maadbreck/lab02-infracode
- Repository: https://github.com/maadbreck/lab02-infracode

## Overview

##### Project Type and Purpose
- This is an “Infrastructure as Code” project using Terraform together with the `kreuzwerker/docker` provider to provision a local Docker-based environment.
- It spins up:
- Three independent Node.js applications (app1, app2, app3)
- A Redis cache
- A PostgreSQL database
- An NGINX reverse-proxy/web server
- A Grafana container for monitoring

##### File Structure Analysis
Top‐level:
- README.md – high-level overview and usage
- apps/ – three subfolders (app1, app2, app3), each containing a Dockerfile, entrypoint (app.js), and package.json
- terraform/ – Terraform configuration files and state folders

terraform/ contains:
- providers.tf – Docker provider definition
- variables.tf and terraform.tfvars – configurable variables (e.g., ports, image tags)
- networks.tf – Docker network definitions
- services.tf – resource blocks creating containers for Node apps, Postgres, Redis, Grafana, NGINX
- nginx.tf – specific NGINX configuration and container setup
- outputs.tf – exposes container endpoints (e.g., app URLs, Grafana URL)

a. Infrastructure Provisioner
– Terraform CLI (init, workspace, apply)
b. Docker Host (local machine or VM)
– Single Docker daemon that houses all containers
c. Docker Network(s)
– Internal bridge network connecting all containers
d. Containers:
1. Node.js App1 (HTTP → port 5001)
2. Node.js App2 (HTTP → port 5002)
3. Node.js App3 (HTTP → port 5003)
4. PostgreSQL (TCP 5432)
5. Redis (TCP 6379)
6. Grafana (HTTP → port 5000)
7. NGINX (HTTP → port 3000)

##### Relationships and Data Flows
- Apps ↔ Redis/Postgres: each app container connects over the Docker network to Redis and to Postgres.
- External Client → NGINX: all incoming HTTP traffic on port 3000 hits NGINX.
- NGINX → Node Apps: NGINX is configured (in nginx.tf) to reverse-proxy requests to apps on ports 5001–5003.

##### Architectural Patterns and Principles
- Modularization: each service in its own container (separation of concerns)
- Immutable infrastructure: containers are built via Dockerfiles and recreated on Terraform apply
- Declarative provisioning: Terraform code defines desired state
- Single Docker network for service discovery

## Diagram (Mermaid)

```mermaid
graph TB
    %% Provisioning Layer
    subgraph "Terraform Configuration" 
        direction TB
        TFProviders["providers.tf"]:::infra
        TFVariables["variables.tf"]:::infra
        TFTFvars["terraform.tfvars"]:::infra
        TFNetworks["networks.tf"]:::infra
        TFServices["services.tf"]:::infra
        TFNginx["nginx.tf"]:::infra
        TFOutputs["outputs.tf"]:::infra
    end
    Terraform["((Terraform CLI))"]:::infra
    Terraform -->|uses| TFProviders
    Terraform -->|uses| TFVariables
    Terraform -->|uses| TFTFvars
    Terraform -->|uses| TFNetworks
    Terraform -->|uses| TFServices
    Terraform -->|uses| TFNginx
    Terraform -->|uses| TFOutputs

    %% Docker Host
    DockerHost["Docker Host\n(Docker Daemon)"]:::infra
    Terraform -->|provisions| DockerHost

    %% Network Boundary
    subgraph "Docker Bridge Network" 
        direction TB
        style Docker_Network stroke-dasharray: 5 5
        Docker_Network[/"Bridge Network"/]:::network

        %% Application Containers
        App1["App1\nNode.js (5001)"]:::app
        App2["App2\nNode.js (5002)"]:::app
        App3["App3\nNode.js (5003)"]:::app

        %% Datastores
        Postgres["PostgreSQL\n(5432)"]:::db
        Redis["Redis\n(6379)"]:::db

        %% Reverse Proxy
        NGINX["NGINX\n(3000)"]:::proxy

        %% Monitoring
        Grafana["Grafana\n(5000)"]:::monitor

        Docker_Network --> App1
        Docker_Network --> App2
        Docker_Network --> App3
        Docker_Network --> Postgres
        Docker_Network --> Redis
        Docker_Network --> NGINX
        Docker_Network --> Grafana

        %% Service Relationships
        App1 -->|connects| Postgres
        App1 -->|connects| Redis
        App2 -->|connects| Postgres
        App2 -->|connects| Redis
        App3 -->|connects| Postgres
        App3 -->|connects| Redis

        NGINX -->|reverse-proxy| App1
        NGINX -->|reverse-proxy| App2
        NGINX -->|reverse-proxy| App3

        %% Monitoring Dataflow
        App1 -->|metrics| Grafana
        App2 -->|metrics| Grafana
        App3 -->|metrics| Grafana
        Postgres -->|metrics| Grafana
        Redis -->|metrics| Grafana
    end

    %% External Client
    ExternalClient["External Client"]:::external
    ExternalClient -->|HTTP 3000| NGINX

    %% Legend
    subgraph Legend
        direction TB
        Infra["Infra / Provisioning"]:::infra
        AppColor["Applications"]:::app
        DBColor["Datastore"]:::db
        ProxyColor["Reverse Proxy"]:::proxy
        MonColor["Monitoring"]:::monitor
        NetColor["Docker Network"]:::network
        ExtColor["External"]:::external
    end

    %% Click Events
    click TFProviders "https://github.com/maadbreck/lab02-infracode/blob/main/terraform/providers.tf"
    click TFVariables "https://github.com/maadbreck/lab02-infracode/blob/main/terraform/variables.tf"
    click TFTFvars "https://github.com/maadbreck/lab02-infracode/blob/main/terraform/terraform.tfvars"
    click TFNetworks "https://github.com/maadbreck/lab02-infracode/blob/main/terraform/networks.tf"
    click TFServices "https://github.com/maadbreck/lab02-infracode/blob/main/terraform/services.tf"
    click TFNginx "https://github.com/maadbreck/lab02-infracode/blob/main/terraform/nginx.tf"
    click TFOutputs "https://github.com/maadbreck/lab02-infracode/blob/main/terraform/outputs.tf"
    click App1 "https://github.com/maadbreck/lab02-infracode/tree/main/apps/app1/Dockerfile"
    click App1 "https://github.com/maadbreck/lab02-infracode/blob/main/apps/app1/app.js"
    click App1 "https://github.com/maadbreck/lab02-infracode/blob/main/apps/app1/package.json"
    click App2 "https://github.com/maadbreck/lab02-infracode/tree/main/apps/app2/Dockerfile"
    click App2 "https://github.com/maadbreck/lab02-infracode/blob/main/apps/app2/app.js"
    click App2 "https://github.com/maadbreck/lab02-infracode/blob/main/apps/app2/package.json"
    click App3 "https://github.com/maadbreck/lab02-infracode/tree/main/apps/app3/Dockerfile"
    click App3 "https://github.com/maadbreck/lab02-infracode/blob/main/apps/app3/app.js"
    click App3 "https://github.com/maadbreck/lab02-infracode/blob/main/apps/app3/package.json"
    click Postgres "https://github.com/maadbreck/lab02-infracode/blob/main/terraform/services.tf"
    click Redis "https://github.com/maadbreck/lab02-infracode/blob/main/terraform/services.tf"
    click Grafana "https://github.com/maadbreck/lab02-infracode/blob/main/terraform/services.tf"
    click NGINX "https://github.com/maadbreck/lab02-infracode/blob/main/terraform/nginx.tf"

    %% Styles
    classDef infra fill:#f2f2f2,stroke:#999,stroke-width:1px
    classDef app fill:#cce5ff,stroke:#004085,stroke-width:1px
    classDef db fill:#d4edda,stroke:#155724,stroke-width:1px
    classDef proxy fill:#ffe5b4,stroke:#ff8000,stroke-width:1px
    classDef monitor fill:#e2d6f3,stroke:#5e3788,stroke-width:1px
    classDef network fill:none,stroke:#6c757d,stroke-dasharray:5 5
    classDef external fill:#fff3cd,stroke:#856404,stroke-width:1px
```
