Terraform - Infrastructure as Code =================================== Terraform provisionne l'infrastructure OVH Public Cloud (VPS, réseau, SSH keys) de manière déclarative et reproductible. **Localisation** : ``infrastructure/terraform/`` Vue d'ensemble -------------- **Terraform** permet de définir l'infrastructure comme du code (IaC) : - ✅ Reproductible : Même config = même infrastructure - ✅ Versionné : Configuration dans Git - ✅ Déclaratif : Décrire l'état désiré, Terraform fait le reste - ✅ Idempotent : Plusieurs ``apply`` = même résultat - ✅ Plan/Apply : Prévisualiser avant déployer **Provider** : OVH via OpenStack API Structure --------- .. code-block:: text infrastructure/terraform/ ├── main.tf # Configuration principale (providers, resources) ├── variables.tf # Variables configurables ├── load-env.sh # Script chargement variables .env ├── .env.example # Template credentials OVH └── .env # Credentials OVH (git ignored) Prérequis --------- **1. Compte OVH Public Cloud** Créer projet OVH Public Cloud : https://www.ovh.com/manager/ **2. Credentials OVH API** Générer tokens API : https://api.ovh.com/createToken/ .. code-block:: text Droits requis: - GET /cloud/project/* - POST /cloud/project/* - PUT /cloud/project/* - DELETE /cloud/project/* **3. Credentials OpenStack** Télécharger ``openrc.sh`` depuis OVH Manager → Horizon → Project → API Access → Download OpenStack RC File. **4. Terraform CLI** .. code-block:: bash # Linux wget https://releases.hashicorp.com/terraform/1.9.0/terraform_1.9.0_linux_amd64.zip unzip terraform_1.9.0_linux_amd64.zip sudo mv terraform /usr/local/bin/ # macOS brew install terraform # Vérifier terraform --version Configuration ------------- Variables d'environnement ^^^^^^^^^^^^^^^^^^^^^^^^^^ **Créer .env** : .. code-block:: bash cd infrastructure/terraform cp .env.example .env nano .env # Éditer avec vos credentials **Exemple .env** : .. code-block:: bash # OVH API Credentials OVH_ENDPOINT=ovh-eu OVH_APPLICATION_KEY=your_app_key OVH_APPLICATION_SECRET=your_app_secret OVH_CONSUMER_KEY=your_consumer_key # OpenStack Credentials (depuis openrc.sh) OS_AUTH_URL=https://auth.cloud.ovh.net/v3 OS_PROJECT_ID=your_project_id OS_PROJECT_NAME=your_project_name OS_USERNAME=your_username OS_PASSWORD=your_password OS_REGION_NAME=GRA11 OS_IDENTITY_API_VERSION=3 **Charger variables** : .. code-block:: bash # IMPORTANT: Utilisez "source" (pas "./") source ./load-env.sh # Output: # ✓ OVH_ENDPOINT=ovh-eu # ✓ OVH_APPLICATION_KEY=abcd*** # ✓ OVH_APPLICATION_SECRET=wxyz*** # ... Variables Terraform ^^^^^^^^^^^^^^^^^^^ **variables.tf** : Variables configurables .. code-block:: hcl variable "ovh_endpoint" { description = "OVH API endpoint (ovh-eu, ovh-ca, etc.)" type = string default = "ovh-eu" } variable "ovh_service_name" { description = "ID du projet OVH Cloud" type = string } variable "instance_name" { description = "Nom de l'instance VPS" type = string default = "koprogo-vps" } variable "region" { description = "Région OVH (GRA11 = Gravelines, bas carbone)" type = string default = "GRA11" # 60g CO2/kWh } variable "ssh_public_key_path" { description = "Chemin vers votre clé SSH publique" type = string default = "~/.ssh/id_rsa.pub" } variable "domain" { description = "Nom de domaine (optionnel)" type = string default = "" } **Personnaliser** : .. code-block:: bash # Créer fichier terraform.tfvars cat > terraform.tfvars < Provider Version Conflict ^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: text Error: Inconsistent dependency lock file **Solution** : .. code-block:: bash # Mettre à jour lock file terraform init -upgrade CI/CD Terraform --------------- GitHub Actions Example ^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: yaml # .github/workflows/terraform.yml name: Terraform Apply on: push: branches: [main] paths: - 'infrastructure/terraform/**' jobs: terraform: runs-on: ubuntu-latest defaults: run: working-directory: infrastructure/terraform env: OVH_ENDPOINT: ${{ secrets.OVH_ENDPOINT }} OVH_APPLICATION_KEY: ${{ secrets.OVH_APPLICATION_KEY }} OVH_APPLICATION_SECRET: ${{ secrets.OVH_APPLICATION_SECRET }} OVH_CONSUMER_KEY: ${{ secrets.OVH_CONSUMER_KEY }} OS_AUTH_URL: ${{ secrets.OS_AUTH_URL }} OS_PROJECT_ID: ${{ secrets.OS_PROJECT_ID }} OS_USERNAME: ${{ secrets.OS_USERNAME }} OS_PASSWORD: ${{ secrets.OS_PASSWORD }} OS_REGION_NAME: ${{ secrets.OS_REGION_NAME }} steps: - uses: actions/checkout@v3 - name: Setup Terraform uses: hashicorp/setup-terraform@v2 with: terraform_version: 1.9.0 - name: Terraform Init run: terraform init - name: Terraform Validate run: terraform validate - name: Terraform Plan run: terraform plan - name: Terraform Apply if: github.ref == 'refs/heads/main' run: terraform apply -auto-approve **Secrets GitHub** : .. code-block:: bash # Ajouter secrets: Settings → Secrets → Actions OVH_ENDPOINT OVH_APPLICATION_KEY OVH_APPLICATION_SECRET OVH_CONSUMER_KEY OS_AUTH_URL OS_PROJECT_ID OS_USERNAME OS_PASSWORD OS_REGION_NAME Mise à jour Infrastructure --------------------------- Modifier Ressource ^^^^^^^^^^^^^^^^^^ .. code-block:: bash # Éditer main.tf nano main.tf # Changer flavor (scaling vertical) flavor_name = "d2-4" # 4 vCPU, 8GB RAM # Prévisualiser terraform plan # Appliquer terraform apply ⚠️ **Attention** : Certains changements détruisent VPS (rebuild). Terraform affiche ``forces replacement``. Scaling Vertical ^^^^^^^^^^^^^^^^ **Augmenter VPS** : .. code-block:: hcl # main.tf resource "openstack_compute_instance_v2" "koprogo_vps" { # Passer de d2-2 à d2-4 flavor_name = "d2-4" # 4 vCPU, 8GB RAM (~14€/mois) } .. code-block:: bash terraform apply **Downtime** : ~2-5 minutes (rebuild VPS). Import Ressource Existante ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Si VPS créé manuellement, l'importer dans Terraform : .. code-block:: bash # Obtenir ID instance openstack server list # Importer dans state terraform import openstack_compute_instance_v2.koprogo_vps Coûts ----- .. list-table:: :header-rows: 1 :widths: 40 30 30 * - Ressource - Coût Mensuel - Coût Annuel * - **VPS d2-2** - 6.96€ TTC - 83.52€ * - **IP Publique** - Inclus - Inclus * - **Bande Passante** - Illimité - Illimité * - **TOTAL** - **6.96€ TTC** - **~84€** **Évolution** : - VPS d2-4 (4 vCPU, 8GB) : ~14€/mois - VPS d2-8 (8 vCPU, 16GB) : ~28€/mois Références ---------- - Terraform OVH Provider : https://registry.terraform.io/providers/ovh/ovh/ - Terraform OpenStack Provider : https://registry.terraform.io/providers/terraform-provider-openstack/openstack/ - Terraform Docs : https://developer.hashicorp.com/terraform/docs - OVH Public Cloud : https://www.ovhcloud.com/fr/public-cloud/ - OVH API : https://api.ovh.com/