# legnakra/azure_ha_wordpress architecture

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

- Interactive diagram: https://gitdiagram.com/legnakra/azure_ha_wordpress
- Repository: https://github.com/legnakra/azure_ha_wordpress

## Overview

##### Project type and purpose
- This is an infrastructure‐as‐code and configuration‐management project that automates the deployment of a highly available WordPress environment on Microsoft Azure.
- Terraform is used to provision Azure resources (network, VMs, load balancer, MySQL), and Ansible to configure the VMs (install Docker, deploy WordPress containers, clean up temporary SSH access).

a. Operator workstation
- Scripts/Vault: `encrypt.sh` / `decrypt.sh` (Ansible Vault)
- Scripts/Terraform: `plan.sh` / `apply.sh` / `destroy.sh`
- Scripts/Ansible: `deploy.sh`
b. Terraform provisioning layer
- Azure Subscription (backend state stored in Azure Storage)
- Resource Group
- Virtual Network & Subnets
- Load Balancer (public frontend IP + probe + backend pool)
- Two Virtual Machines (in an availability zone or set)
- Azure Database for MySQL (or VM-based MySQL)
- Network Security Group (initially allows 80, 443; temporarily opens 22)
c. Ansible configuration layer
- Inventories/hosts (with the two VMs’ public IPs)
- Roles:
- docker-install – installs Docker on each VM
- wordpress – pulls and runs WordPress container, links to MySQL endpoint
- Handlers: clean up temporary public IPs and SSH rules
d. Runtime services
- Docker container running WordPress on each VM
- MySQL database service
- Azure Load Balancer distributing HTTP traffic across the two WordPress instances
- DNS record (`mariatec.es`) pointing to Load Balancer’s public IP

##### Relationships and data flows
- Operator runs `plan.sh` → Terraform init & plan → shows diff
- Operator runs `apply.sh` → Terraform provisions all Azure resources
- Terraform outputs the two VM public IPs and the load balancer VIP
- Operator updates `Ansible/inventories/hosts` with VM IPs
- Operator runs `deploy.sh` → Ansible connects via SSH (using SSHPass), runs playbooks
- docker-install role → installs Docker
- wordpress role → deploys WordPress containers, configures DB connection
- handlers → remove public IPs & port-22 rules
- End users browse `mariatec.es` → DNS → Load Balancer → WordPress VMs → MySQL

##### Architectural patterns & design principles
- Layered approach: Infrastructure provisioning (Terraform) separated from software configuration (Ansible)
- Immutable infrastructure principle: VMs are configured at provisioning time via playbooks
- Declarative configuration: Terraform .tf files, Ansible YAML playbooks
- Reusable modules: Terraform modules for networking, VMs, load balancer, database, storage
- Idempotent roles: Ansible roles for Docker & WordPress ensure consistent state

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Layers
    subgraph "Operator Workstation"
        direction TB
        subgraph "Vault Scripts" 
            VS1["encrypt.sh"]:::terraform
            VS2["decrypt.sh"]:::terraform
        end
        subgraph "Terraform Scripts"
            TS1["plan.sh"]:::terraform
            TS2["apply.sh"]:::terraform
            TS3["destroy.sh"]:::terraform
        end
        subgraph "Ansible Scripts"
            AS1["deploy.sh"]:::ansible
        end
    end

    subgraph "Provisioning (Terraform)" 
        direction TB
        TFL["Terraform Gear"]:::terraform
        subgraph "Azure Subscription" 
            direction TB
            TF_BACKEND["State Backend"]:::terraform
            RG["Resource Group"]:::terraform
            NET["VNet & Subnets"]:::terraform
            LB["Load Balancer"]:::terraform
            NSG["Network Security Group"]:::terraform
            subgraph "Compute"
                direction TB
                VM1["VM Instance"]:::runtime
                VM2["VM Instance"]:::runtime
            end
            DB["MySQL Database"]:::database
        end
        %% Modules
        subgraph "Modules"
            direction TB
            M_RG["resource-group"]:::terraform
            M_NET["networking"]:::terraform
            M_LB["load-balancer"]:::terraform
            M_VM["virtual-machines"]:::terraform
            M_DB["database"]:::terraform
            M_ST["storage"]:::terraform
        end
    end

    subgraph "Configuration (Ansible)"
        direction TB
        ANS["Ansible Gear"]:::ansible
        INV["Inventories/hosts"]:::ansible
        GV["Group Vars"]:::ansible
        ROLE1["docker-install"]:::ansible
        ROLE2["wordpress"]:::ansible
        PLAY["wordpress.yml"]:::ansible
    end

    subgraph "Runtime Services"
        direction TB
        Internet["Internet"]:::external
        DNS["DNS: mariatec.es"]:::external
        LB_RT["Load Balancer"]:::runtime
        VM1C["WordPress Container"]:::runtime
        VM2C["WordPress Container"]:::runtime
        DB_RT["MySQL Service"]:::database
    end

    %% Flows
    VS1 -->|encrypt/decrypt| TFL
    VS2 --> TFL
    TS1 --> TFL
    TS2 --> TFL
    TS3 --> TFL
    AS1 --> ANS
    TFL -.-> M_RG
    TFL -.-> M_NET
    TFL -.-> M_LB
    TFL -.-> M_VM
    TFL -.-> M_DB
    TFL -.-> M_ST
    M_RG --> RG
    M_NET --> NET
    M_LB --> LB
    M_VM --> VM1 & VM2
    M_DB --> DB
    NSG --> VM1 & VM2
    TF_BACKEND --> TFL
    ANS --> INV
    ANS --> GV
    ANS --> ROLE1
    ANS --> ROLE2
    PLAY -->|uses| ROLE1 & ROLE2
    INV --> VM1 & VM2
    ROLE1 --> VM1 & VM2
    ROLE2 --> VM1 & VM2
    VM1 --> VM1C
    VM2 --> VM2C
    Internet --> DNS
    DNS --> LB_RT
    LB_RT --> VM1C
    LB_RT --> VM2C
    VM1C --> DB_RT
    VM2C --> DB_RT

    %% Click Events
    click VS1 "https://github.com/legnakra/azure_ha_wordpress/blob/main/Scripts/Vault/encrypt.sh"
    click VS2 "https://github.com/legnakra/azure_ha_wordpress/blob/main/Scripts/Vault/decrypt.sh"
    click TS1 "https://github.com/legnakra/azure_ha_wordpress/blob/main/Scripts/Terraform/plan.sh"
    click TS2 "https://github.com/legnakra/azure_ha_wordpress/blob/main/Scripts/Terraform/apply.sh"
    click TS3 "https://github.com/legnakra/azure_ha_wordpress/blob/main/Scripts/Terraform/destroy.sh"
    click AS1 "https://github.com/legnakra/azure_ha_wordpress/blob/main/Scripts/Ansible/deploy.sh"
    click TF_BACKEND "https://github.com/legnakra/azure_ha_wordpress/tree/main/Terraform/backends/dev"
    click PLAY "https://github.com/legnakra/azure_ha_wordpress/blob/main/Ansible/wordpress.yml"
    click INV "https://github.com/legnakra/azure_ha_wordpress/tree/main/Ansible/inventories/hosts"
    click GV "https://github.com/legnakra/azure_ha_wordpress/tree/main/Ansible/inventories/group_vars/all"
    click M_RG "https://github.com/legnakra/azure_ha_wordpress/tree/main/Terraform/modules/resource-group"
    click M_NET "https://github.com/legnakra/azure_ha_wordpress/tree/main/Terraform/modules/networking"
    click M_LB "https://github.com/legnakra/azure_ha_wordpress/tree/main/Terraform/modules/load-balancer"
    click M_VM "https://github.com/legnakra/azure_ha_wordpress/tree/main/Terraform/modules/virtual-machines"
    click M_DB "https://github.com/legnakra/azure_ha_wordpress/tree/main/Terraform/modules/database"
    click M_ST "https://github.com/legnakra/azure_ha_wordpress/tree/main/Terraform/modules/storage"
    click PLAY "https://github.com/legnakra/azure_ha_wordpress/blob/main/Terraform/environments/dev/main.tf"
    click PLAY "https://github.com/legnakra/azure_ha_wordpress/blob/main/Terraform/environments/dev/outputs.tf"
    click PLAY "https://github.com/legnakra/azure_ha_wordpress/blob/main/Terraform/environments/dev/variables.tf"
    click TFL "https://github.com/legnakra/azure_ha_wordpress/blob/main/Terraform/tf_service_principal.txt"

    %% Styles
    classDef terraform fill:#b2f2bb,stroke:#2f8f45;
    classDef ansible fill:#a5d8ff,stroke:#2969a8;
    classDef runtime fill:#dee2e6,stroke:#495057;
    classDef database fill:#ffe8a1,stroke:#cc9a06;
    classDef external fill:#f8f9fa,stroke:#adb5bd;
```
