1. What state is for #
Terraform's memory.
Principle #
The state file maps each resource in the code to a real object (identifier, attributes). Without it, Terraform would not know what it manages. It can contain secrets (generated passwords, keys): it must be encrypted and access to it restricted.
Common mistake: Never edit the state file by hand and never version it in Git.
2. Remote backends #
Sharing and locking.
Configuration #
terraform {
backend "s3" {
bucket = "etat-terraform-123"
key = "reseau/prod.tfstate"
region = "eu-west-3"
encrypt = true
use_lockfile = true
}
}| Backend | Locking |
|---|---|
| s3 | Lock file (or DynamoDB, historically) |
| azurerm | Lease on the blob |
| gcs | Native |
| HCP Terraform | Native, with history and remote runs |
3. Manipulating state #
Import, move, remove.
Commands and blocks #
terraform state list
terraform state show aws_s3_bucket.rapports
terraform plan -refresh-only # détecter la dérive sans rien changer# importer une ressource existante (Terraform 1.5+)
import {
to = aws_s3_bucket.ancien
id = "bucket-cree-a-la-main"
}
# renommer sans détruire
moved {
from = aws_s3_bucket.rapports
to = module.stockage.aws_s3_bucket.rapports
}
# ne plus gérer sans supprimer (Terraform 1.7+)
removed {
from = aws_instance.historique
lifecycle { destroy = false }
}Good practice: terraform plan -generate-config-out=import.tf generates the code for imported resources: you then only need to review and clean it up.
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
Terraform best practices: structure, state, security, review and maintenance
Terraform best practices for teams: repository structure, naming, pinned versions, encrypted and ...
Terraform multi-environment, workspaces and repository organization
Organize Terraform for several environments: one folder per environment, tfvars files, CLI worksp...
Terraform: principles, architecture and workflow
Understand Terraform: declarative infrastructure as code, architecture (core, providers, state), ...
Infrastructure as Code and GitOps: Terraform, Ansible and Argo CD
Manage infrastructure as code: declarative or imperative, Terraform (providers, state, plan, modu...
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...
The HCL language: resources, variables, outputs, data and expressions
Writing Terraform: terraform, provider, resource and data blocks, typed variables with validation...
Testing and securing Terraform: validate, terraform test, tflint, Checkov and policies
Ensure the quality of Terraform code: fmt and validate, native terraform test tests with mocked p...
Preparing for the HashiCorp Certified: Terraform Associate certification
Preparation guide for the Terraform Associate: format, official objectives (IaC concepts, workflo...
Terraform modules: designing, versioning and reusing
Structure Terraform with modules: root module and child modules, inputs and outputs, sources (loc...