# nesrinechaouani/terraform-cicd architecture

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

- Interactive diagram: https://gitdiagram.com/nesrinechaouani/terraform-cicd
- Repository: https://github.com/nesrinechaouani/terraform-cicd

## Overview

##### Project Type and Purpose
- This is an infrastructure‐as‐code (IaC) project that provisions cloud resources and deploys a simple Python application (app.py) via automated CI/CD pipelines.
- Key files:
– Terraform configuration (main.tf, variables.tf, outputs.tf, terraform.tfvars*)
– CI/CD definitions (Jenkinsfile, .github/workflows/main_myappservice1.yml)
– Python application (app.py + requirements.txt)

##### Top-Level Components
a. Version Control
– GitHub repository (contains code, Terraform, and CI configs)
b. CI/CD Orchestration
– GitHub Actions workflow (.github/workflows/main_myappservice1.yml)
– Jenkins pipeline (Jenkinsfile)
c. Infrastructure Provisioning (Terraform)
– Terraform state and lock (.terraform.lock.hcl)
– Terraform modules and resources defined in .tf files
d. Application Layer
– Python app (app.py + dependencies)
– Runtime environment on target cloud resources
e. Cloud Provider
– Compute (e.g., EC2, App Service), networking, IAM, storage, etc.
– Outputs (e.g., public IP, DNS) published by Terraform

##### Diagramming the Relationships and Data Flows
- “Developer” → pushes code to “GitHub Repo.”
- From GitHub:
– Trigger “GitHub Actions” for IaC validation and (optionally) deploy.
– Trigger “Jenkins” for full pipeline (e.g., unit tests, Terraform plan/apply, app deployment).
- CI steps communicate with the Cloud Provider via Terraform:
– Authentication (using encrypted terraform.tfvars, environment secrets).
– `terraform init` → `terraform plan` → `terraform apply`.
- Upon successful apply, Terraform outputs are captured (e.g., IP addresses).
- Jenkins/GitHub Actions then:
– Build Docker image or package Python app.
– Deploy to provisioned infrastructure (copy files to VM, push to container registry + orchestrator).
- Monitoring/logging (not in repo, but you may represent as extensibility).

##### Architectural Patterns and Principles
- Infrastructure as Code: All resources declared in Terraform modules.
- GitOps/CI-driven Deployments: Pipeline triggers on Git events.
- Separation of Concerns:
– IaC (Terraform) vs. Application code (Python) vs. CI Configs.
- Idempotency & Declarative Provisioning: Terraform ensures drift detection.
- Secrets Management: terraform.tfvars.encrypted and GitHub/Jenkins credentials.

1. Developer commits code → GitHub
2. GitHub Actions workflow:
- Syntax check (YAML).
- Terraform fmt/validate.
3. Jenkins Pipeline (parallel or sequential):
- Checkout code
- `terraform init`
- `terraform plan` → review (manual approval?)
- `terraform apply`
- Outputs
- Build/package Python app
- Deploy to provisioned target
4. Provisioned Infrastructure:
- Compute instances or app service
- Networking (VPC, security groups)
- Access credentials injected via Terraform outputs or secrets
5. Application Running (app.py)
6. Optional: Monitoring/Logging integration

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Swimlane: Developer
    subgraph "Developer"
        Dev["Developer"]
    end

    %% Swimlane: Version Control & CI/CD
    subgraph "Version Control & CI/CD"
        GitRepo["GitHub Repository"]:::versioncontrol
        GHActions["GitHub Actions Workflow"]:::versioncontrol
        Jenkins["Jenkins Pipeline"]:::versioncontrol
    end

    %% Swimlane: Infrastructure Provisioning
    subgraph "Infrastructure Provisioning"
        TFLock[".terraform.lock.hcl"]:::infra
        TFModule["Terraform Modules (main.tf, *.tf)"]:::infra
        TFVars["variables.tf"]:::infra
        TFOutputDef["outputs.tf"]:::infra
        TFVarsFile["terraform.tfvars"]:::infra
        TFVarsEnc["terraform.tfvars.encrypted"]:::infra
        TFInit["terraform init"]:::infra
        TFPlan["terraform plan"]:::infra
        TFApply["terraform apply"]:::infra
        Outputs["Terraform Outputs (public IP, DNS)"]:::infra
    end

    %% Swimlane: Cloud Provider
    subgraph "Cloud Provider"
        Cloud["Cloud Provider API"]:::infra
        Compute["Compute (VM/App Service)"]:::infra
        Network["Networking (VPC, Security Groups)"]:::infra
    end

    %% Swimlane: Application Runtime
    subgraph "Application Runtime"
        BuildApp["Build/Package Python App"]:::app
        DeployApp["Deploy to Provisioned Infra"]:::app
        AppRuntime["Python Application (app.py)"]:::app
    end

    %% Flows
    Dev -->|"push code"| GitRepo
    GitRepo -->|"trigger on push"| GHActions
    GitRepo -->|"trigger on push"| Jenkins

    GHActions -->|"run IaC checks"| TFInit
    Jenkins -->|"run pipeline stages"| TFInit

    TFModule --> TFInit
    TFLock --> TFInit
    TFVars --> TFInit
    TFVarsEnc --> TFInit

    TFInit --> TFPlan
    TFPlan --> TFApply
    TFOutputDef --> TFApply
    TFVarsFile --> TFApply

    TFApply -->|"provision resources"| Cloud
    Cloud --> Compute
    Cloud --> Network
    TFApply --> Outputs

    TFApply --> BuildApp
    BuildApp --> DeployApp
    DeployApp --> AppRuntime
    Outputs --> DeployApp

    %% Click Events
    click GitRepo "https://github.com/nesrinechaouani/terraform-cicd/blob/main/.gitignore"
    click GitRepo "https://github.com/nesrinechaouani/terraform-cicd/blob/main/README.md"
    click GHActions "https://github.com/nesrinechaouani/terraform-cicd/blob/main/.github/workflows/main_myappservice1.yml"
    click Jenkins "https://github.com/nesrinechaouani/terraform-cicd/tree/main/Jenkinsfile"
    click TFLock "https://github.com/nesrinechaouani/terraform-cicd/blob/main/.terraform.lock.hcl"
    click TFModule "https://github.com/nesrinechaouani/terraform-cicd/blob/main/main.tf"
    click TFVars "https://github.com/nesrinechaouani/terraform-cicd/blob/main/variables.tf"
    click TFOutputDef "https://github.com/nesrinechaouani/terraform-cicd/blob/main/outputs.tf"
    click TFVarsFile "https://github.com/nesrinechaouani/terraform-cicd/blob/main/terraform.tfvars"
    click TFVarsEnc "https://github.com/nesrinechaouani/terraform-cicd/blob/main/terraform.tfvars.encrypted"
    click AppRuntime "https://github.com/nesrinechaouani/terraform-cicd/blob/main/app.py"
    click BuildApp "https://github.com/nesrinechaouani/terraform-cicd/blob/main/requirements.txt"

    %% Styles
    classDef versioncontrol fill:#ADD8E6,stroke:#000;
    classDef infra fill:#90EE90,stroke:#000;
    classDef app fill:#FFA500,stroke:#000;
```
