Skip to content

Commit 6b8c808

Browse files
authored
docs: getting started - cloud infrastructure (#30)
1 parent c701fe1 commit 6b8c808

File tree

6 files changed

+338
-38
lines changed

6 files changed

+338
-38
lines changed

docs/infrastructure/02-getting-started/basic-knowledge/Terraform.md

Whitespace-only changes.

docs/infrastructure/02-getting-started/basic-knowledge/_category_.yml

Lines changed: 0 additions & 2 deletions
This file was deleted.

docs/infrastructure/02-getting-started/basic-knowledge/index.md

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
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 the infrastructure
10+
using a declarative configuration language. This means you describe the desired
11+
state of the infrastructure, and Terraform takes care of creating and updating
12+
the resources to match that state.
13+
14+
:::important
15+
This documentation assumes you already have the Terraform CLI installed on your
16+
local machine. If you need help setting up Terraform, please refer to the
17+
[official documentation](https://learn.hashicorp.com/tutorials/terraform/install-cli).
18+
19+
You'll also need to be logged in with the Azure CLI and have the necessary
20+
permissions to manage the resources in our Azure subscription (more on this
21+
[here](../setup#azure-cli--kubectl)).
22+
23+
Once you have logged in to the Azure CLI, you need to set the necessary
24+
environment variables by running this command at the root of the repository:
25+
26+
```sh
27+
source ./access_key.sh
28+
```
29+
30+
More about this in the [repository README](https://github.com/PoliNetworkOrg/terraform/?tab=readme-ov-file#notes).
31+
:::
32+
33+
### Why We Use Terraform
34+
35+
Our organization leverages Terraform to manage our Azure cloud infrastructure for several important reasons:
36+
37+
- **Consistency:** Defining our infrastructure as code ensures that every environment is configured the same way, reducing the chance of human error.
38+
- **Automation:** Terraform automates the provisioning and modification of resources, streamlining our deployment processes.
39+
- **Version Control:** Storing Terraform configurations in version control systems allows us to track changes, collaborate effectively, and revert to previous versions if needed.
40+
- **Modularity:** Terraform encourages breaking down the infrastructure into reusable modules, which simplifies management and enhances maintainability.
41+
42+
### How Terraform Works
43+
44+
:::warning
45+
To run terraform commands you need to have the right permissions and credentials.
46+
If you are not sure about your permissions, please contact the IT department (but
47+
if you have to ask you probably shouldn't run `apply` yourself anyway).
48+
49+
If you haven't yet set up the Azure CLI please refer to the [setup guide](../setup#azure-cli--kubectl).
50+
:::
51+
52+
Terraform operates through a simple yet powerful workflow:
53+
54+
1. **Write:** You define the infrastructure in configuration files (typically with a `.tf` extension).
55+
2. **Plan:** Running `terraform plan` generates an execution plan, showing you the changes Terraform will make to achieve the desired state.
56+
3. **Apply:** Executing `terraform apply` implements the changes, creating or updating the infrastructure accordingly.
57+
58+
:::danger
59+
Running Terraform commands can modify the cloud infrastructure.
60+
61+
**THIS CAN INCUR COSTS OR CAUSE DOWNTIME AND PERMANENT DATA LOSS IF NOT DONE PROPERLY.**
62+
63+
Always review the execution plan (`terraform plan`) before applying changes.
64+
:::
65+
66+
### Terraform Code vs. Terraform State vs. Azure State
67+
68+
- **Terraform Code:**
69+
This consists of the configuration files that describe your desired
70+
infrastructure. These files are the blueprint for what you want the infrastructure to look like.
71+
72+
- **Terraform State:**
73+
The state file (which we named `state.tfstate`) records the current state
74+
of the infrastructure as managed by Terraform. This file is critical because
75+
it maps your configuration to the real-world resources, tracks dependencies,
76+
and allows Terraform to detect what needs to change during updates. Our state
77+
file is stored remotely (in an Azure Storage Account) and is locked using
78+
Azure's integrated leasing mechanism to prevent concurrent modifications.
79+
80+
- **Azure State:**
81+
This is the actual state of resources deployed in the Azure environment.
82+
While Terraform keeps track of these resources via its state file, the real
83+
configuration and runtime details exist within Azure. The Terraform state acts
84+
as an intermediary, ensuring that the desired state (from the Terraform code)
85+
and the actual state (in Azure) are in sync. If resources are modified directly
86+
in Azure without updating Terraform, discrepancies may occur, highlighting the
87+
importance of using Terraform as the single source of truth.
88+
89+
This clear separation between code, Terraform state, and the actual cloud state
90+
is key to maintaining a reliable and predictable infrastructure.
91+
92+
## Terraform Implementation Overview
93+
94+
Welcome to the documentation for the **polinetworkorg/terraform** repository.
95+
This page provides an in-depth look at our Terraform implementation on
96+
**Microsoft Azure**, detailing the architecture of our infrastructure and the
97+
key modules that make it up. While this overview highlights our main components,
98+
it’s important to note that there are alternative approaches to similar problems,
99+
and our setup is tailored to our current needs.
100+
101+
### Repository Overview
102+
103+
The **polinetworkorg/terraform** repository is our centralized solution for
104+
provisioning and managing cloud infrastructure on Azure. It follows a modular
105+
design that breaks down the overall architecture into focused components. To give
106+
you a rough idea of what is in there, here are some of the primary modules:
107+
108+
- **aks:** Defines all resources related to our Azure Kubernetes Service (AKS) instance.
109+
- **mariadb:** Manages the main MariaDB database.
110+
- **monitoring:** Sets up our monitoring stack using Grafana and Prometheus.
111+
- **argocd:** Configures our ArgoCD installation for GitOps-driven deployments.
112+
- **storage:** Handles the Azure Storage Account where the Terraform state is stored.
113+
114+
There are also additional modules in the repository that handle specific
115+
applications or services, but these are not detailed in this general overview.
116+
117+
:::info
118+
While the choices made in this implementation reflect our specific requirements
119+
and constraints, it's worth noting that other approaches exist for managing
120+
cloud infrastructure with Terraform.
121+
122+
Our modular design, state management strategy,
123+
and access controls are tailored to our current operational needs and funding
124+
situation—but they are not set in stone. Please feel free to reach out to the
125+
IT department if you have any questions or suggestions.
126+
:::
127+
128+
### Infrastructure Structure
129+
130+
Our repository is organized to clearly separate concerns and promote reusability.
131+
While the exact folder structure may vary, a typical layout looks like this:
132+
133+
```bash
134+
📁 polinetworkorg/terraform/
135+
├─ 📁 modules/ # All modules are kept in a dedicated directory
136+
│ ├─ 📁 aks/ # Module for Azure Kubernetes Service resources
137+
│ ├─ 📁 mariadb/ # Module for the main MariaDB database
138+
│ ├─ 📁 monitoring/ # Module for monitoring (Grafana + Prometheus)
139+
│ ├─ 📁 argocd/ # Module for ArgoCD installation
140+
│ ├─ 📁 storage/ # Module for Azure Storage Account where the state.tfstate is kept
141+
│ └─ [other modules] # Additional modules for specific services
142+
├── main.tf # Main Terraform configuration file
143+
├── variables.tf # Variables definition file
144+
├── outputs.tf # Outputs definition file
145+
├── README.md # General repository documentation
146+
└── [other files]
147+
```
148+
149+
### State Management
150+
151+
We use an **Azure Storage Account** for managing the Terraform state. The state
152+
file (`state.tfstate`) is stored within a dedicated container named `terraform-state`,
153+
as defined in our **storage** module. The integrated leasing mechanism provided
154+
by the Azure Storage Account is used for state locking, ensuring that concurrent
155+
Terraform operations do not conflict with each other.
156+
157+
### Access and Authorization
158+
159+
Given the critical nature of our infrastructure, modifications (e.g., running
160+
`terraform apply`) are restricted to personnel with sufficient clearance. As an
161+
association, any infrastructure changes require approval from either the Head of
162+
the IT Department or the Directive Council. All authorization is managed through
163+
**Microsoft's Entra ID system**, ensuring that only properly authenticated and
164+
authorized users can perform these operations.
165+
166+
### Funding and Nonprofit Considerations
167+
168+
As a nonprofit organization, we benefit from a **Microsoft Azure Grant for Nonprofits**,
169+
which provides us with a budget of **€2000 per year**. This support helps offset
170+
our cloud costs and underpins our commitment to maintaining a robust and scalable
171+
infrastructure.
172+
173+
:::info
174+
This is one of the core reasons we are structured as a nonprofit organization, and why
175+
we use Azure as our cloud provider.
176+
177+
You can find more about our benefits [here](https://www.microsoft.com/en-us/nonprofits/azure).
178+
179+
If you have access to the adminorg account, you can check the current status of
180+
our Azure grant [here](https://www.microsoftazuresponsorships.com/).
181+
:::
182+
183+
## Making Changes to the Infrastructure
184+
185+
This section provides a basic guide on how to update our infrastructure using
186+
Terraform and introduces some of the automated workflows that help us maintain
187+
consistency and control.
188+
189+
### Basic Workflow for Infrastructure Changes
190+
191+
1. **Edit the Terraform Code:**
192+
Modify the relevant configuration files to define your desired changes in the
193+
infrastructure.
194+
195+
2. **Review Your Changes Locally:**
196+
Run `terraform plan` to generate an execution plan. This command will show
197+
you what changes will be made to align the actual infrastructure with your
198+
code.
199+
200+
3. **Cost Estimation with Infracost:**
201+
When you open a pull request, our **Infracost workflow** is triggered
202+
automatically. This workflow calculates the estimated cost impact of your
203+
changes and posts a report in the pull request comments, so you can review
204+
any potential cost implications before merging.
205+
:::tip
206+
If you want to run Infracost locally, you can install it by following the
207+
[official documentation](https://www.infracost.io/docs/).
208+
:::
209+
210+
4. **Apply the Changes:**
211+
After your changes have been reviewed and approved, merge the pull request
212+
and run `terraform apply` to implement the changes. This command updates the
213+
Azure resources to match the configuration defined in your code.
214+
215+
5. **Drift Detection:**
216+
A nightly **Drift workflow** runs to detect any discrepancies between the
217+
current Terraform state and the actual state in Azure. If this workflow
218+
detects a drift from what the `terraform plan` output would predict, it
219+
automatically opens an issue for further investigation.
220+
221+
### Basic Terraform CLI Commands
222+
223+
- **`terraform init`**
224+
Initializes the working directory containing the Terraform configuration files.
225+
:::tip
226+
This has to be run as soon as you clone the repository or when you pull changes
227+
that include new modules or providers.
228+
:::
229+
230+
- **`terraform plan`**
231+
Generates an execution plan showing what changes will be applied to the infrastructure.
232+
233+
- **`terraform apply`**
234+
Applies the changes defined in your Terraform configuration to the cloud environment.
235+
236+
- **`terraform destroy`**
237+
Destroys the resources managed by Terraform, useful for tearing down test environments or decommissioning infrastructure.
238+
239+
:::danger
240+
If you ever happen to be as much as tempted of using `terraform destroy`, you
241+
better be **SURE AS SHIT** about what you are doing. Make sure to review the
242+
implications thoroughly before proceeding.
243+
> *One does not simply destroy PoliNetwork with one command.*
244+
:::
245+
246+
By following these steps and leveraging our automated workflows, you ensure that
247+
infrastructure changes are managed in a consistent, cost-aware, and controlled
248+
manner.
249+
250+
## References
251+
252+
- [Terraform Documentation](https://www.terraform.io/docs/index.html)
253+
- [Microsoft Azure Documentation](https://docs.microsoft.com/en-us/azure/)
254+
- [Infracost Documentation](https://www.infracost.io/docs/)
255+
- [Terraform Repository](https://github.com/polinetworkorg/terraform)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
label: Cloud Infrastructure
2+
position: 2
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: Cloud Infrastructure
3+
---
4+
import DocCardList from '@theme/DocCardList';
5+
6+
## Overview
7+
8+
This section explains how we organized our infrastructure.
9+
10+
Everything regarding the deployment of apps and services is divided into two repositories:
11+
12+
- [polinetworkorg/terraform](https://github.com/polinetworkorg/terraform) - Contains the Terraform code to deploy the infrastructure. This defines the resources that are created on Azure, from specific things like KeyVaults, Disks, and VMs, to the Kubernetes cluster itself.
13+
- [polinetworkorg/polinetwork-cd](https://github.com/polinetworkorg/polinetwork-cd) - Our GitOps repo, contains the manifests that define deployments specific for each app or service. This repository is connected to the ArgoCD instance running on the Kubernetes cluster.
14+
15+
```mermaid
16+
flowchart TD
17+
subgraph Github
18+
tf([**terraform**])
19+
cd([**polinetwork-cd**])
20+
end
21+
subgraph Azure
22+
vm[VMs and other resources]
23+
subgraph k8s[K8S cluster]
24+
argocd[ArgoCD]
25+
app@{ shape: processes, label: "Apps / Services" }
26+
end
27+
end
28+
29+
tf -.-> vm
30+
tf -.-> k8s
31+
vm <--> k8s
32+
cd <-.-> argocd --> app
33+
```
34+
35+
### **Terraform: Definition and Purpose**
36+
37+
The **Terraform** repository is responsible for managing and provisioning cloud infrastructure. In simple terms, it defines and creates the resources needed for the infrastructure, such as:
38+
39+
- **Networks and subnets** (Virtual Networks, Subnets, Security Groups)
40+
- **Virtual machines** (VMs, Containers, Kubernetes Clusters)
41+
- **Databases** (MariaDB, PostgreSQL)
42+
- **Storage** (Blob Storage, Virtual Disks, File Shares)
43+
- **Security services** (Identity and Access Management, Firewalls)
44+
45+
The code in **Terraform** describes these resources declaratively: it defines the desired state, and Terraform applies the necessary changes to reach that state. This repository is primarily used to **create and modify the cloud infrastructure** where applications will run.
46+
47+
### **PoliNetwork-CD: Definition and Purpose**
48+
49+
The **PoliNetwork-CD** (Continuous Deployment) repository, on the other hand, is dedicated to managing and updating applications once the infrastructure has been created. Specifically, it uses **GitOps** tools (in our case, **ArgoCD**, but **Flux** is also an opinionated option) to:
50+
51+
- **Define application deployments** in Kubernetes (or Kustomize, Helm, etc.)
52+
- **Automatically update applications** when code changes
53+
- **Manage application configurations** (e.g., configuration files, environment variables)
54+
- **Monitor the state of applications** and ensure they remain in sync with the repository
55+
56+
While Terraform is responsible for infrastructure, **PoliNetwork-CD** focuses on **managing the lifecycle of applications** running on that infrastructure, ensuring that code changes are applied automatically and securely.
57+
58+
### **Key Difference: Infrastructure vs. Deployment**
59+
60+
| Repository | Main Purpose | Example of Managed Resources |
61+
|------------|--------------|------------------------------|
62+
| **Terraform** | Creates and manages cloud infrastructure | Networks, VMs, databases, storage, security |
63+
| **PoliNetwork-CD** | Automates application deployment and updates | Deployments, configurations, GitOps updates |
64+
65+
In summary, **Terraform builds the foundation on which applications run**, while **PoliNetwork-CD manages the deployment and continuous updating of applications**. Both repositories work together to ensure a scalable and efficient cloud infrastructure.
66+
67+
<!-- markdownlint-disable-next-line -->
68+
<DocCardList />
69+
70+
## References
71+
72+
### **Links to Repositories**
73+
74+
- [Terraform Repository](https://github.com/polinetworkorg/terraform)
75+
- [PoliNetwork-CD Repository](https://github.com/polinetworkorg/polinetwork-cd)
76+
77+
### **Additional Resources**
78+
79+
- [Kubernetes Documentation](https://kubernetes.io/docs/)
80+
- [Terraform Documentation](https://www.terraform.io/docs/)
81+
- [ArgoCD Documentation](https://argo-cd.readthedocs.io/en/stable/)

0 commit comments

Comments
 (0)