Aller au contenu

TP 29 : Construire une pipeline Terraform CI/CD

À 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.
  • Un dépôt GitHub (ou une instance compatible GitHub Actions).
  • Terraform >= 1.7 pour la partie test.
  • 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.

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.

.
├── 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) »
  1. terraform fmt -check -recursive : le code doit être formaté.
  2. terraform init -backend=false : initialisation sans backend (pas d’apply en CI de vérification).
  3. terraform validate : cohérence syntaxique et sémantique.
  4. terraform test : tests unitaires du module (avec mocks, TP 27).
  5. Scan Trivy en mode IaC : failles de configuration.
  6. terraform plan -out=tfplan : génération du plan.
  7. Publication de tfplan (et de sa version lisible) comme artefact.
.github/workflows/terraform.yml
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: 5

Remarques :

  • 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.

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 tfplan

Note : ce job d’apply nécessite un backend distant (TP 22) et des credentials — que le TP 30 fournira via OIDC.

Avant de pousser, rejouez la pipeline localement :

Fenêtre de terminal
terraform fmt -check -recursive
terraform init -backend=false
terraform validate
terraform test
trivy config --severity HIGH,CRITICAL .
terraform plan -out=tfplan
terraform show tfplan > tfplan.txt
  • Un code mal formaté fait échouer l’étape Format.
  • Une faille de configuration HIGH/CRITICAL fait échouer l’étape Trivy.
  • L’artefact terraform-plan est disponible au terme d’un run réussi.
Fenêtre de terminal
rm -f tfplan tfplan.txt test-report.xml
rm -rf .terraform