TP 29 : Construire une pipeline Terraform CI/CD
TP 29 : Construire une pipeline Terraform CI/CD
Section intitulée « TP 29 : Construire une pipeline Terraform CI/CD »Objectifs
Section intitulée « Objectifs »À l’issue de ce TP, vous serez capable de :
- structurer une pipeline Terraform en étapes claires ;
- vérifier le formatage (
fmt -check) et la validité (validate) ; - exécuter les tests (
terraform test) et un scan de sécurité (Trivy) ; - produire un plan et le publier comme artefact réutilisable ;
- épingler les versions d’outils pour une pipeline reproductible.
Prérequis
Section intitulée « Prérequis »- Un dépôt GitHub (ou une instance compatible GitHub Actions).
- Terraform
>= 1.7pour la partietest. - Notions du TP 27 (tests) et du TP 28 (validations).
Ce TP prépare le TP 30, qui remplacera les clés AWS statiques par une authentification OIDC.
Contexte
Section intitulée « Contexte »Chez InfraBank, chaque modification d’infrastructure doit passer par une pipeline qui garantit un code formaté, valide, testé et scanné, et qui produit un plan revu avant tout apply.
Étape 1 : Structure du dépôt
Section intitulée « Étape 1 : Structure du dépôt ».├── main.tf├── variables.tf├── versions.tf├── tests/│ └── module.tftest.hcl└── .github/ └── workflows/ └── terraform.ymlÉtape 2 : Étapes de la pipeline (vue d’ensemble)
Section intitulée « Étape 2 : Étapes de la pipeline (vue d’ensemble) »terraform fmt -check -recursive: le code doit être formaté.terraform init -backend=false: initialisation sans backend (pas d’apply en CI de vérification).terraform validate: cohérence syntaxique et sémantique.terraform test: tests unitaires du module (avec mocks, TP 27).- Scan Trivy en mode IaC : failles de configuration.
terraform plan -out=tfplan: génération du plan.- Publication de
tfplan(et de sa version lisible) comme artefact.
Étape 3 : Le workflow GitHub Actions
Section intitulée « Étape 3 : Le workflow GitHub Actions »name: terraform-ci
on: pull_request: paths: - "**.tf" - "**.tftest.hcl" - ".github/workflows/terraform.yml"
permissions: contents: read
env: TF_VERSION: "1.11.4" TRIVY_VERSION: "0.55.2"
jobs: terraform: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4
- name: Setup Terraform uses: hashicorp/setup-terraform@v3 with: terraform_version: ${{ env.TF_VERSION }}
- name: Format run: terraform fmt -check -recursive
- name: Init (sans backend) run: terraform init -backend=false
- name: Validate run: terraform validate -no-color
- name: Test run: terraform test -junit-xml=test-report.xml
- name: Trivy - scan IaC uses: aquasecurity/trivy-action@0.24.0 with: scan-type: config scan-ref: . severity: HIGH,CRITICAL exit-code: "1"
- name: Plan run: | terraform plan -input=false -no-color -out=tfplan terraform show -no-color tfplan > tfplan.txt
- name: Publier le plan uses: actions/upload-artifact@v4 with: name: terraform-plan path: | tfplan tfplan.txt retention-days: 5
- name: Publier le rapport de tests if: always() uses: actions/upload-artifact@v4 with: name: test-report path: test-report.xml retention-days: 5Remarques :
- Les versions d’outils (
TF_VERSION,TRIVY_VERSION, tags d’actions@v4,@0.24.0) sont épinglées : une pipeline reproductible ne dépend jamais d’un « latest » mouvant. exit-code: "1"sur Trivy fait échouer la pipeline si une faille HIGH/CRITICAL est détectée.if: always()publie le rapport de tests même en cas d’échec, pour faciliter le diagnostic.
Étape 4 : Le plan comme artefact
Section intitulée « Étape 4 : Le plan comme artefact »Publier tfplan permet d’appliquer exactement le plan revu (dans un job d’apply ultérieur ou après approbation manuelle), sans risque de dérive entre le plan revu et ce qui est réellement appliqué.
# Extrait d'un job d'apply consommant l'artefact (déclenché après approbation) - name: Récupérer le plan uses: actions/download-artifact@v4 with: name: terraform-plan
- name: Apply run: terraform apply -input=false tfplanNote : ce job d’apply nécessite un backend distant (TP 22) et des credentials — que le TP 30 fournira via OIDC.
Étape 5 : Reproduire les étapes en local
Section intitulée « Étape 5 : Reproduire les étapes en local »Avant de pousser, rejouez la pipeline localement :
terraform fmt -check -recursiveterraform init -backend=falseterraform validateterraform testtrivy config --severity HIGH,CRITICAL .terraform plan -out=tfplanterraform show tfplan > tfplan.txtVérification
Section intitulée « Vérification »- Un code mal formaté fait échouer l’étape
Format. - Une faille de configuration HIGH/CRITICAL fait échouer l’étape
Trivy. - L’artefact
terraform-planest disponible au terme d’un run réussi.
Nettoyage
Section intitulée « Nettoyage »rm -f tfplan tfplan.txt test-report.xmlrm -rf .terraform