|
| 1 | +--- |
| 2 | +title: "Terraform" |
| 3 | +description: "Detailed overview of our Azure-based infrastructure managed via the polinetworkorg/terraform repository." |
| 4 | +--- |
| 5 | + |
| 6 | +## What is Terraform? |
| 7 | + |
| 8 | +Terraform is an open-source Infrastructure as Code (IaC) tool developed by |
| 9 | +HashiCorp. It allows you to define, provision, and manage your infrastructure |
| 10 | +using a declarative configuration language. This means you describe the desired |
| 11 | +state of your infrastructure, and Terraform takes care of creating and updating |
| 12 | +the resources to match that state. |
| 13 | + |
| 14 | +### Why We Use Terraform |
| 15 | + |
| 16 | +Our organization leverages Terraform to manage our Azure cloud infrastructure for several important reasons: |
| 17 | + |
| 18 | +- **Consistency:** Defining our infrastructure as code ensures that every environment is configured the same way, reducing the chance of human error. |
| 19 | +- **Automation:** Terraform automates the provisioning and modification of resources, streamlining our deployment processes. |
| 20 | +- **Version Control:** Storing Terraform configurations in version control systems allows us to track changes, collaborate effectively, and revert to previous versions if needed. |
| 21 | +- **Modularity:** Terraform encourages breaking down the infrastructure into reusable modules, which simplifies management and enhances maintainability. |
| 22 | + |
| 23 | +### How Terraform Works |
| 24 | + |
| 25 | +Terraform operates through a simple yet powerful workflow: |
| 26 | + |
| 27 | +1. **Write:** You define your infrastructure in configuration files (typically with a `.tf` extension). |
| 28 | +2. **Plan:** Running `terraform plan` generates an execution plan, showing you the changes Terraform will make to achieve the desired state. |
| 29 | +3. **Apply:** Executing `terraform apply` implements the changes, creating or updating your infrastructure accordingly. |
| 30 | + |
| 31 | +### Terraform Code vs. Terraform State vs. Azure State |
| 32 | + |
| 33 | +- **Terraform Code:** |
| 34 | + This consists of the configuration files that describe your desired |
| 35 | + infrastructure. These files are the blueprint for what you want your infrastructure to look like. |
| 36 | + |
| 37 | +- **Terraform State:** |
| 38 | + The state file (commonly named `terraform.tfstate`) records the current state |
| 39 | + of the infrastructure as managed by Terraform. This file is critical because |
| 40 | + it maps your configuration to the real-world resources, tracks dependencies, |
| 41 | + and allows Terraform to detect what needs to change during updates. Our state |
| 42 | + file is stored remotely (in an Azure Storage Account) and is locked using |
| 43 | + Azure's integrated leasing mechanism to prevent concurrent modifications. |
| 44 | + |
| 45 | +- **Azure State:** |
| 46 | + This is the actual state of resources deployed in your Azure environment. |
| 47 | + While Terraform keeps track of these resources via its state file, the real |
| 48 | + configuration and runtime details exist within Azure. The Terraform state acts |
| 49 | + as an intermediary, ensuring that the desired state (from the Terraform code) |
| 50 | + and the actual state (in Azure) are in sync. If resources are modified directly |
| 51 | + in Azure without updating Terraform, discrepancies may occur, highlighting the |
| 52 | + importance of using Terraform as the single source of truth. |
| 53 | + |
| 54 | +This clear separation between code, Terraform state, and the actual cloud state |
| 55 | +is key to maintaining a reliable and predictable infrastructure. |
| 56 | + |
| 57 | +## Terraform Implementation Overview |
| 58 | + |
| 59 | +Welcome to the documentation for the **polinetworkorg/terraform** repository. |
| 60 | +This page provides an in-depth look at our Terraform implementation on |
| 61 | +**Microsoft Azure**, detailing the architecture of our infrastructure and the |
| 62 | +key modules that make it up. While this overview highlights our main components, |
| 63 | +it’s important to note that there are alternative approaches to similar problems, |
| 64 | +and our setup is tailored to our current needs. |
| 65 | + |
| 66 | +### Repository Overview |
| 67 | + |
| 68 | +The **polinetworkorg/terraform** repository is our centralized solution for |
| 69 | +provisioning and managing cloud infrastructure on Azure. It follows a modular |
| 70 | +design that breaks down the overall architecture into focused components. Some |
| 71 | +of the primary modules include: |
| 72 | + |
| 73 | +- **aks:** Defines all resources related to our Azure Kubernetes Service (AKS) instance. |
| 74 | +- **mariadb:** Manages the main MariaDB database. |
| 75 | +- **monitoring:** Sets up our monitoring stack using Grafana and Prometheus. |
| 76 | +- **argocd:** Configures our ArgoCD installation for GitOps-driven deployments. |
| 77 | + |
| 78 | +There are also additional modules in the repository that handle specific |
| 79 | +applications or services, but these are not detailed in this general overview. |
| 80 | + |
| 81 | +### Infrastructure Structure |
| 82 | + |
| 83 | +Our repository is organized to clearly separate concerns and promote reusability. |
| 84 | +While the exact folder structure may vary, a typical layout looks like this: |
| 85 | + |
| 86 | +```bash |
| 87 | +polinetworkorg/terraform/ |
| 88 | +├── modules/ |
| 89 | +│ ├── aks/ # Module for Azure Kubernetes Service resources |
| 90 | +│ ├── mariadb/ # Module for the main MariaDB database |
| 91 | +│ ├── monitoring/ # Module for monitoring (Grafana + Prometheus) |
| 92 | +│ ├── argocd/ # Module for ArgoCD installation |
| 93 | +│ └── [other modules] # Additional modules for specific services |
| 94 | +├── storage/ # Module for Azure Storage Account for state management |
| 95 | +│ └── state.tfstate # Terraform state file container (in a 'terraform-state' container) |
| 96 | +├── environments/ # Environment-specific configurations (e.g., dev, prod) |
| 97 | +├── main.tf # Main Terraform configuration file |
| 98 | +├── variables.tf # Variables definition file |
| 99 | +├── outputs.tf # Outputs definition file |
| 100 | +└── README.md # General repository documentation |
| 101 | +``` |
| 102 | + |
| 103 | +### State Management |
| 104 | + |
| 105 | +We use an **Azure Storage Account** for managing the Terraform state. The state |
| 106 | +file (`state.tfstate`) is stored within a dedicated container named `terraform-state`, |
| 107 | +as defined in our **storage** module. The integrated leasing mechanism provided |
| 108 | +by the Azure Storage Account is used for state locking, ensuring that concurrent |
| 109 | +Terraform operations do not conflict with each other. |
| 110 | + |
| 111 | +### Access and Authorization |
| 112 | + |
| 113 | +Given the critical nature of our infrastructure, modifications (e.g., running |
| 114 | +`terraform apply`) are restricted to personnel with sufficient clearance. As an |
| 115 | +association, any infrastructure changes require approval from either the Head of |
| 116 | +the IT Department or the Directive Council. All authorization is managed through |
| 117 | +**Microsoft's Entra ID system**, ensuring that only properly authenticated and |
| 118 | +authorized users can perform these operations. |
| 119 | + |
| 120 | +### Funding and Nonprofit Considerations |
| 121 | + |
| 122 | +As a nonprofit organization, we benefit from a **Microsoft Azure Grant for Nonprofits**, |
| 123 | +which provides us with a budget of **€2000 per year**. This support helps offset |
| 124 | +our cloud costs and underpins our commitment to maintaining a robust and scalable |
| 125 | +infrastructure. |
| 126 | + |
| 127 | +### Acknowledgements and Alternative Approaches |
| 128 | + |
| 129 | +While the choices made in this implementation reflect our specific requirements |
| 130 | +and constraints, it's worth noting that other approaches exist for managing |
| 131 | +cloud infrastructure with Terraform. Our modular design, state management strategy, |
| 132 | +and access controls are tailored to our current operational needs and funding |
| 133 | +situation—but they are not set in stone. We continuously review and update our |
| 134 | +practices as our needs evolve. |
| 135 | + |
| 136 | +## Making Changes to the Infrastructure |
| 137 | + |
| 138 | +This section provides a basic guide on how to update our infrastructure using |
| 139 | +Terraform and introduces some of the automated workflows that help us maintain |
| 140 | +consistency and control. |
| 141 | + |
| 142 | +### Basic Workflow for Infrastructure Changes |
| 143 | + |
| 144 | +1. **Edit the Terraform Code:** |
| 145 | + Modify the relevant configuration files to define your desired changes in the |
| 146 | + infrastructure. |
| 147 | + |
| 148 | +2. **Review Your Changes Locally:** |
| 149 | + Run `terraform plan` to generate an execution plan. This command will show |
| 150 | + you what changes will be made to align the actual infrastructure with your |
| 151 | + code. |
| 152 | + |
| 153 | +3. **Cost Estimation with Infracost:** |
| 154 | + When you open a pull request, our **Infracost workflow** is triggered |
| 155 | + automatically. This workflow calculates the estimated cost impact of your |
| 156 | + changes and posts a report in the pull request comments, so you can review |
| 157 | + any potential cost implications before merging. |
| 158 | + |
| 159 | +4. **Apply the Changes:** |
| 160 | + After your changes have been reviewed and approved, merge the pull request |
| 161 | + and run `terraform apply` to implement the changes. This command updates the |
| 162 | + Azure resources to match the configuration defined in your code. |
| 163 | + |
| 164 | +5. **Drift Detection:** |
| 165 | + A nightly **Drift workflow** runs to detect any discrepancies between the |
| 166 | + current Terraform state and the actual state in Azure. If this workflow |
| 167 | + detects a drift from what the `terraform plan` output would predict, it |
| 168 | + automatically opens an issue for further investigation. |
| 169 | + |
| 170 | +### Basic Terraform CLI Commands |
| 171 | + |
| 172 | +- **`terraform init`** |
| 173 | + Initializes the working directory containing the Terraform configuration files. |
| 174 | + |
| 175 | +- **`terraform plan`** |
| 176 | + Generates an execution plan showing what changes will be applied to your infrastructure. |
| 177 | + |
| 178 | +- **`terraform apply`** |
| 179 | + Applies the changes defined in your Terraform configuration to your cloud environment. |
| 180 | + |
| 181 | +- **`terraform destroy`** |
| 182 | + Destroys the resources managed by Terraform, useful for tearing down test environments or decommissioning infrastructure. |
| 183 | + |
| 184 | +By following these steps and leveraging our automated workflows, you ensure that |
| 185 | +infrastructure changes are managed in a consistent, cost-aware, and controlled |
| 186 | +manner. |
0 commit comments