1. Static analysis #
Even before the plan.
Tools #
terraform fmt -check -recursive
terraform validate
tflint --init && tflint --recursive
checkov -d . # ou : trivy config .2. Native tests #
terraform test.
Example #
# tests/app.tftest.hcl
mock_provider "aws" {
mock_resource "aws_instance" { defaults = { id = "i-0123456789" } }
}
variables { environnement = "recette" }
run "une_seule_instance_en_recette" {
command = plan
assert {
condition = length(aws_instance.web) == 1
error_message = "Une instance en recette."
}
}
run "refuse_un_environnement_inconnu" {
command = plan
variables { environnement = "demo" }
expect_failures = [var.environnement]
}Real tests: Tests can also run against an emulator (LocalStack) or a real test account with command = apply: they create and then destroy the resources.
3. Guardrails in the code #
Preconditions, check, secrets.
Examples #
resource "aws_instance" "web" {
instance_type = var.type
lifecycle {
precondition {
condition = !startswith(var.type, "p5")
error_message = "Pas d'instance GPU coûteuse ici."
}
}
}
check "site_repond" {
data "http" "accueil" { url = "https://${aws_lb.web.dns_name}/healthz" }
assert {
condition = data.http.accueil.status_code == 200
error_message = "Le site ne répond pas."
}
}- Sensitive variables marked
sensitive = true; values supplied by the CI or a vault (Vault, secrets manager), never in the code. - Terraform 1.10+: ephemeral values (
ephemeral) that are never written to the state.
My progress
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
The HCL language: resources, variables, outputs, data and expressions
Writing Terraform: terraform, provider, resource and data blocks, typed variables with validation...
Terraform in CI/CD: pipelines, plan review, OIDC and GitOps
Automate Terraform: plan pipeline on merge requests and apply after approval, posting the plan as...
Terraform: principles, architecture and workflow
Understand Terraform: declarative infrastructure as code, architecture (core, providers, state), ...
Terraform across multiple clouds: AWS, Azure, Google Cloud, OVHcloud and Kubernetes
Use Terraform with the main providers: aws, azurerm, google, ovh and openstack, kubernetes and he...
Terraform state: remote backends, locking, import and refactoring
Manage Terraform state: the role of the state file, sensitive data, remote backends (S3, azurerm,...
Terraform best practices: structure, state, security, review and maintenance
Terraform best practices for teams: repository structure, naming, pinned versions, encrypted and ...
Terraform modules: designing, versioning and reusing
Structure Terraform with modules: root module and child modules, inputs and outputs, sources (loc...
Terraform multi-environment, workspaces and repository organization
Organize Terraform for several environments: one folder per environment, tfvars files, CLI worksp...
Preparing for the HashiCorp Certified: Terraform Associate certification
Preparation guide for the Terraform Associate: format, official objectives (IaC concepts, workflo...