# mvasilenko/terraform-aws-infrastructure architecture

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

- Interactive diagram: https://gitdiagram.com/mvasilenko/terraform-aws-infrastructure
- Repository: https://github.com/mvasilenko/terraform-aws-infrastructure

## Overview

##### Main Components Identification
- Infrastructure Management: Represented by Terraform. You have multiple Terraform directories which are organized by region (e.g., eu-west-1) and further by service (e.g., network, ECS, LB, DNS, IAM).
- State & Lock Management: Include S3 (for Terraform state storage) and DynamoDB (for locking).
- Networking: VPC, public subnets, and security groups that are configured in the “network” directory.
- IAM Roles: Global IAM provisioning (in terraform/global/iam) and Terraform modules for IAM roles.
- Container Orchestration: ECS Cluster and Service (in terraform/eu-west-1/ecs/app), along with a task definition that runs the Flask application.
- Application: A simple Flask app located in the “app” directory, with related Dockerfile, build, and deployment scripts.
- Container Registry: ECR repository (configured in terraform/eu-west-1/ecr) where Docker images are built and pushed.
- Load Balancing: Application load balancer and listener configurations, including target group setup in the LB directory.
- DNS: Though not configured in code, the README mentions manual DNS configuration (e.g., mapping a hostname to the load balancer).

##### Relationships and Interactions
- Terraform orchestrates the entire AWS infrastructure, connecting components such as network, IAM, ECS, ECR, and LB.
- The Flask app is containerized using Docker, then built and pushed to the ECR repository.
- The ECS cluster pulls the container image from ECR and runs the Docker container as a service.
- The load balancer (ALB) routes external HTTP/HTTPS traffic to the running ECS tasks via defined listener rules and target groups.
- S3 and DynamoDB are used by Terraform to store the state and manage locking, ensuring consistent deployments.
- IAM roles granted to the container allow secure access to necessary AWS services (e.g., pulling images from ECR).

##### Architectural Patterns and Design Principles
- Separation of Concerns: Infrastructure code is clearly separated by AWS service and region. The application code is isolated in its own directory.
- Modular Design: Use of Terraform modules (for example, the iam-role module) promotes reuse and consistency.
- Declarative Infrastructure: The approach leverages Terraform’s declarative model to define dependencies and interactions.
- Continuous Deployment Pipeline: The process includes automated Docker image builds (via build.sh), pushing to ECR, and updating ECS with a new image tag via Terraform.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Terraform Infrastructure Subgraph
    subgraph "Terraform Infrastructure"
        TF["Terraform (Infrastructure as Code)"]:::terraform
        SL["State & Lock Management (S3 & DynamoDB)"]:::state
        NET["Networking (VPC, Subnets, Security Groups)"]:::network
        IAM_G["Global IAM Roles"]:::iam
        IAM_M["IAM Role Module"]:::iam
        ECS["ECS Cluster & Service"]:::ecs
        ECR["Container Registry (ECR)"]:::ecr
        LB["Load Balancer (ALB)"]:::lb
        DNS["DNS Configuration"]:::dns
    end

    %% Application Deployment Subgraph
    subgraph "Application Deployment"
        APP["Containerized Flask App"]:::app
        Client["Client/Browser"]:::client
    end

    %% Terraform provisioning arrows
    TF -->|"provisions"| SL
    TF -->|"provisions"| NET
    TF -->|"provisions"| IAM_G
    TF -->|"provisions"| IAM_M
    TF -->|"updates"| ECS
    TF -->|"configures"| ECR
    TF -->|"configures"| LB
    TF -->|"configures"| DNS

    %% Deployment process arrows
    APP -->|"push_image"| ECR
    ECR -->|"pull_image"| ECS
    APP -->|"trigger_deploy"| TF
    LB -->|"route_traffic"| ECS
    Client -->|"HTTP/HTTPS"| LB

    %% Click Events
    click TF "https://github.com/mvasilenko/terraform-aws-infrastructure/tree/main/terraform/eu-west-1"
    click SL "https://github.com/mvasilenko/terraform-aws-infrastructure/tree/main/terraform/eu-west-1/main"
    click NET "https://github.com/mvasilenko/terraform-aws-infrastructure/tree/main/terraform/eu-west-1/network"
    click IAM_G "https://github.com/mvasilenko/terraform-aws-infrastructure/tree/main/terraform/global/iam"
    click IAM_M "https://github.com/mvasilenko/terraform-aws-infrastructure/tree/main/terraform/modules/iam-role"
    click ECS "https://github.com/mvasilenko/terraform-aws-infrastructure/tree/main/terraform/eu-west-1/ecs/app"
    click ECR "https://github.com/mvasilenko/terraform-aws-infrastructure/tree/main/terraform/eu-west-1/ecr"
    click LB "https://github.com/mvasilenko/terraform-aws-infrastructure/tree/main/terraform/eu-west-1/lb"
    click DNS "https://github.com/mvasilenko/terraform-aws-infrastructure/tree/main/terraform/eu-west-1/dns"
    click APP "https://github.com/mvasilenko/terraform-aws-infrastructure/tree/main/app"

    %% Styles
    classDef terraform fill:#fceabb,stroke:#f8c471,stroke-width:2px;
    classDef state fill:#d5f5e3,stroke:#82e0aa,stroke-width:2px;
    classDef network fill:#d6eaf8,stroke:#aed6f1,stroke-width:2px;
    classDef iam fill:#f9e79f,stroke:#f7dc6f,stroke-width:2px;
    classDef ecs fill:#fadbd8,stroke:#f5b7b1,stroke-width:2px;
    classDef ecr fill:#e8daef,stroke:#d2b4de,stroke-width:2px;
    classDef lb fill:#fcf3cf,stroke:#f9e79f,stroke-width:2px;
    classDef dns fill:#d6eaf8,stroke:#aed6f1,stroke-width:2px;
    classDef app fill:#d5f5e3,stroke:#82e0aa,stroke-width:2px;
    classDef client fill:#f0b27a,stroke:#e67e22,stroke-width:2px;
```
