TP 2 : Utiliser les outputs
Objectif
Section intitulée « Objectif »Découvrir les outputs Terraform pour récupérer des informations après un déploiement.
Ce TP permet de voir comment afficher :
- le nom d’une ressource créée
- une URL d’accès
- un port exposé
- une information calculée à partir de variables
- un output sensible masqué
Niveau : Débutant : Environnement : Docker local
Prérequis
Section intitulée « Prérequis »- Terraform installé
- Docker installé et lancé
terraform versiondocker versionArborescence
Section intitulée « Arborescence »tp-terraform-outputs/├── main.tf├── variables.tf├── outputs.tf└── .gitignoreFichier .gitignore
Section intitulée « Fichier .gitignore ».terraform/*.tfstate*.tfstate.*.terraform.lock.hcl*.tfvarsFichier variables.tf
Section intitulée « Fichier variables.tf »variable "container_name" { description = "Nom du conteneur Docker" type = string default = "tp-outputs-nginx"}
variable "image_name" { description = "Image Docker utilisée" type = string default = "nginx:latest"}
variable "external_port" { description = "Port exposé sur la machine locale" type = number default = 8080}Fichier main.tf
Section intitulée « Fichier main.tf »terraform { required_providers { docker = { source = "kreuzwerker/docker" version = "~> 3.0" } }}
provider "docker" {}
resource "docker_image" "nginx" { name = var.image_name}
resource "docker_container" "web" { name = var.container_name image = docker_image.nginx.image_id
ports { internal = 80 external = var.external_port }}Fichier outputs.tf
Section intitulée « Fichier outputs.tf »output "container_name" { description = "Nom du conteneur créé" value = docker_container.web.name}
output "image_name" { description = "Image Docker utilisée" value = docker_image.nginx.name}
output "external_port" { description = "Port exposé sur la machine locale" value = var.external_port}
output "application_url" { description = "URL locale de l'application" value = "http://localhost:${var.external_port}"}
output "container_id" { description = "Identifiant du conteneur Docker" value = docker_container.web.id}Étape 1 : initialiser Terraform
Section intitulée « Étape 1 : initialiser Terraform »terraform initÉtape 2 : vérifier la configuration
Section intitulée « Étape 2 : vérifier la configuration »terraform fmtterraform validateÉtape 3 : afficher le plan
Section intitulée « Étape 3 : afficher le plan »terraform planÉtape 4 : créer le conteneur
Section intitulée « Étape 4 : créer le conteneur »terraform applyConfirmer avec yes.
À la fin de l’exécution, Terraform affiche automatiquement les outputs définis :
Outputs:
application_url = "http://localhost:8080"container_id = "xxxxxxxxxxxx"container_name = "tp-outputs-nginx"external_port = 8080image_name = "nginx:latest"Étape 5 : afficher les outputs manuellement
Section intitulée « Étape 5 : afficher les outputs manuellement »Afficher tous les outputs :
terraform outputAfficher uniquement l’URL :
terraform output application_urlAfficher uniquement le nom du conteneur :
terraform output container_nameÉtape 6 : tester l’application
Section intitulée « Étape 6 : tester l’application »curl http://localhost:8080Étape 7 : utiliser un output dans un script
Section intitulée « Étape 7 : utiliser un output dans un script »APP_URL=$(terraform output -raw application_url)echo $APP_URLcurl $APP_URLL’option
-rawretourne la valeur sans guillemets, directement utilisable dans un script shell ou une pipeline CI/CD.
Étape 8 : ajouter un output sans recréer l’infrastructure
Section intitulée « Étape 8 : ajouter un output sans recréer l’infrastructure »Ajouter dans outputs.tf :
output "docker_command" { description = "Commande Docker pour inspecter le conteneur" value = "docker inspect ${docker_container.web.name}"}Ajouter un output ne modifie aucune ressource :
terraform applyLa sortie indique Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
terraform output docker_commandÉtape 9 : output sensible
Section intitulée « Étape 9 : output sensible »Ajouter une variable sensible dans variables.tf :
variable "admin_password" { description = "Mot de passe fictif pour démonstration" type = string sensitive = true}Fournir la valeur via variable d’environnement — ne jamais la mettre en default :
Sous Linux ou macOS :
export TF_VAR_admin_password="password123"Sous PowerShell :
$env:TF_VAR_admin_password="password123"Ajouter dans outputs.tf :
output "admin_password" { description = "Exemple d'output sensible" value = var.admin_password sensitive = true}terraform applyTerraform masque la valeur dans tous les affichages :
admin_password = <sensitive>Pour afficher la valeur explicitement :
terraform output admin_passwordAttention :
terraform output <nom>affiche la valeur en clair dans le terminal, y compris dans l’historique de commandes.
sensitive = truemasque la valeur dans les logs Terraform, mais ne chiffre pas la valeur dans le fichier de state. Ne jamais stocker de secrets réels dans le state sans backend chiffré.
Étape 10 : nettoyage
Section intitulée « Étape 10 : nettoyage »terraform destroyPoints importants
Section intitulée « Points importants »Les outputs servent à afficher des informations utiles après un déploiement : URL, identifiant, adresse IP, nom de ressource, commande à utiliser ensuite.
Ils peuvent être consommés par d’autres outils — script shell, pipeline CI/CD, ou autre projet Terraform via terraform_remote_state.
sensitive = true masque une valeur dans les affichages Terraform, mais ne la protège pas dans le state. Pour les secrets de production, utiliser un gestionnaire de secrets comme HashiCorp Vault ou AWS Secrets Manager.
Variante sans Docker
Section intitulée « Variante sans Docker »Pour un TP sans Docker, utiliser le provider local natif — aucune installation supplémentaire nécessaire :
terraform { required_providers { local = { source = "hashicorp/local" version = "~> 2.0" } }}
resource "local_file" "example" { filename = "message.txt" content = "Fichier créé avec Terraform"}
output "file_path" { description = "Chemin du fichier créé" value = local_file.example.filename}
output "file_content" { description = "Contenu du fichier créé" value = local_file.example.content}