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
Infrastructure as Code and GitOps: Terraform, Ansible and Argo CD
Manage infrastructure as code: declarative or imperative, Terraform (providers, state, plan, modu...
End-to-end CI/CD (2/5): continuous integration, testing and quality
Succeed with continuous integration: the test pyramid, unit, integration, contract and end-to-end...
DevSecOps: securing the software supply chain end to end
Integrate security into the DevOps pipeline: shift-left, threat modeling, secret detection, SAST,...
Infrastructure as Code on Azure: Bicep, Terraform, Cost Management and Well-Architected
Describe and deploy Azure as code with Bicep (modules, parameters, what-if, pipeline deployments)...
Infrastructure as Code on AWS: CloudFormation, CDK, Terraform and Well-Architected
Describe and deploy your AWS infrastructure as code with CloudFormation, the CDK or Terraform, au...
Ansible best practices: structure, idempotence, security and quality
Ansible best practices: project organization, naming, FQCN, idempotence, modules rather than shel...
Automation and infrastructure as code for Elasticsearch: API, Terraform, Ansible and CI/CD
Automate Elasticsearch administration: REST API and clients, the elasticstack Terraform provider ...
Operating Google Cloud: observability, security, infrastructure as code and costs
Operate Google Cloud: Cloud Monitoring, Cloud Logging and Cloud Trace (Google Cloud Observability...
Operating OVHcloud: Terraform, monitoring, security, costs and sovereignty
Operate OVHcloud in production: infrastructure as code with the ovh and openstack Terraform provi...