# apgaua/terraform-modules architecture

> Architecture diagram of the GitHub repository apgaua/terraform-modules, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/apgaua/terraform-modules
- Repository: https://github.com/apgaua/terraform-modules

## Overview

##### Project type and purpose
- This is an Infrastructure-as-Code project written in Terraform, aimed at provisioning AWS resources in a modular way.
- Its purpose is to stand up a complete network (VPC, subnets, gateways), an ECS-based compute layer (EC2 cluster with Auto Scaling), and an ECS service module (task definitions, IAM roles, load balancer integration).

##### File structure
- Top-level directories:
- ecs-service-module – encapsulates IAM roles and variables for ECS services
- ecs_ec2 – defines the ECS EC2 cluster, launch templates, ASGs (on-demand & spot), load balancer, security groups
- networking-module – builds VPC, public/private/DB subnets, gateways, parameter store, outputs
- CI/CD integration via .github/workflows/documentation.yml
- Standard Terraform pattern: each module has its own variables.tf, outputs.tf, resources, and templates.

##### README
- Very brief (“Estrutura de módulos terraform”) – implies the focus is on how modules fit together.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External CI/CD
    GH["GitHub Actions\n(.github/workflows/documentation.yml)"]:::ci

    %% Networking Module
    subgraph "networking-module" 
        direction TB
        VPC["VPC"]:::network
        PubSub["Public Subnets"]:::network
        PrivSub["Private Subnets"]:::network
        DBSub["DB Subnets"]:::network
        Gateways["Internet Gateway\n& NAT Gateway"]:::network
        Params["Parameter Store\n(SSM)"]:::infra
        NetOut["Networking Outputs"]:::network
    end

    %% Compute Module
    subgraph "ecs_ec2" 
        direction TB
        EC2Cluster["ECS EC2 Cluster"]:::compute
        LT_OD["Launch Template\n(On-Demand)"]:::compute
        LT_Spot["Launch Template\n(Spot)"]:::compute
        ASG_OD["Auto Scaling Group\n(On-Demand)"]:::compute
        ASG_Spot["Auto Scaling Group\n(Spot)"]:::compute
        SG["Security Groups\n(ALB & EC2)"]:::compute
        ALB["Application Load Balancer"]:::compute
        IAM_EC2["IAM Role for\nEC2 Instances"]:::iam
        UserData["User-Data Template"]:::compute
        EC2Out["ecs_ec2 Outputs"]:::compute
    end

    %% Service Module
    subgraph "ecs-service-module"
        direction TB
        ECSVars["Variables & Definitions"]:::infra
        IAM_Task["IAM Roles for ECS Tasks"]:::iam
        ECSService["ECS Service\n(Task Definitions)"]:::compute
    end

    %% Relationships
    GH -->|triggers Terraform| VPC
    VPC --> PubSub
    VPC --> PrivSub
    VPC --> DBSub
    PubSub --> Gateways
    Gateways --> PrivSub
    Params -->|used by| UserData
    Params -->|read by| ECSService
    NetOut --> EC2Cluster

    EC2Cluster --> LT_OD
    EC2Cluster --> LT_Spot
    LT_OD --> ASG_OD
    LT_Spot --> ASG_Spot
    ASG_OD -->|hosts tasks| ECSService
    ASG_Spot -->|hosts tasks| ECSService
    SG --> ALB
    SG --> EC2Cluster

    ALB -->|routes to| ECSService
    IAM_EC2 --> EC2Cluster
    IAM_Task --> ECSService
    ECSVars --> ECSService
    EC2Out --> ECSService

    %% Click events
    click VPC "https://github.com/apgaua/terraform-modules/blob/main/networking-module/vpc.tf"
    click PubSub "https://github.com/apgaua/terraform-modules/blob/main/networking-module/public_subnets.tf"
    click PrivSub "https://github.com/apgaua/terraform-modules/blob/main/networking-module/private_subnets.tf"
    click DBSub "https://github.com/apgaua/terraform-modules/blob/main/networking-module/database_subnets.tf"
    click Gateways "https://github.com/apgaua/terraform-modules/blob/main/networking-module/gateways.tf"
    click Params "https://github.com/apgaua/terraform-modules/blob/main/networking-module/parameter_store.tf"
    click NetOut "https://github.com/apgaua/terraform-modules/blob/main/networking-module/outputs.tf"
    click EC2Cluster "https://github.com/apgaua/terraform-modules/blob/main/ecs_ec2/ecs.tf"
    click LT_OD "https://github.com/apgaua/terraform-modules/blob/main/ecs_ec2/launch_template_ondemand.tf"
    click LT_Spot "https://github.com/apgaua/terraform-modules/blob/main/ecs_ec2/launch_template_spot.tf"
    click ASG_OD "https://github.com/apgaua/terraform-modules/blob/main/ecs_ec2/asg_ondemand.tf"
    click ASG_Spot "https://github.com/apgaua/terraform-modules/blob/main/ecs_ec2/asg_spot.tf"
    click ALB "https://github.com/apgaua/terraform-modules/blob/main/ecs_ec2/load_balancer.tf"
    click SG "https://github.com/apgaua/terraform-modules/blob/main/ecs_ec2/security_group.tf"
    click IAM_EC2 "https://github.com/apgaua/terraform-modules/blob/main/ecs_ec2/iam_ecs_role.tf"
    click UserData "https://github.com/apgaua/terraform-modules/blob/main/ecs_ec2/templates/user-data.tpl"
    click ECSVars "https://github.com/apgaua/terraform-modules/blob/main/ecs-service-module/variables.tf"
    click IAM_Task "https://github.com/apgaua/terraform-modules/blob/main/ecs-service-module/iam.tf"
    click GH "https://github.com/apgaua/terraform-modules/blob/main/.github/workflows/documentation.yml"
    click EC2Out "https://github.com/apgaua/terraform-modules/blob/main/ecs_ec2/output.tf"

    %% Styles
    classDef network fill:#AED6F1,stroke:#21618C,color:#1B4F72
    classDef compute fill:#ABEBC6,stroke:#1D8348,color:#145A32
    classDef infra fill:#D5DBDB,stroke:#585858,color:#1C2833
    classDef iam fill:#CCD1D1,stroke:#566573,color:#2C3E50
    classDef ci fill:#F9E79F,stroke:#B7950B,color:#7D6608
```
