RousselTMLEARNING

Article · Terraform: from discovery to expertise

State: backends, locking, import

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 #

Remote backend
Since Terraform 1.10, the S3 backend can lock without DynamoDB (use_lockfile).
terraform {
  backend "s3" {
    bucket       = "etat-terraform-123"
    key          = "reseau/prod.tfstate"
    region       = "eu-west-3"
    encrypt      = true
    use_lockfile = true
  }
}
BackendLocking
s3Lock file (or DynamoDB, historically)
azurermLease on the blob
gcsNative
HCP TerraformNative, 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.

📄 Terraform state: remote backends, locking, import and refactoring →

My progress

  • ⏱ Estimated time : ≈ 6 min

Sign in to track your progress and take notes. Sign in

⭐ Rate this lesson

3.0 initial rating from the teaching team

Sign in to rate this lesson →

💬 Questions about this lesson

No questions yet: be the first to ask for help or share your configuration.

Ask a question about this lesson →

Recommended Articles

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

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

HAProxy: adding and removing backends dynamically

Learn how to use the HAProxy Runtime API to create, configure and delete backends and servers on ...

Ansible: managing your infrastructure with playbooks (Infrastructure as Code)

Write production playbooks: project structure, plays, handlers, tags, serial strategy, error hand...

Operating Google Cloud: observability, security, infrastructure as code and costs

Operate Google Cloud: Cloud Monitoring, Cloud Logging and Cloud Trace (Google Cloud Observability...

Automation and infrastructure as code for Elasticsearch: API, Terraform, Ansible and CI/CD

Automate Elasticsearch administration: REST API and clients, the elasticstack Terraform provider ...

CaaS: state of the art and best practices

Container as a Service (CaaS) is a cloud computing model that lets companies manage and deploy ap...

OpenTelemetry Collector: exporters (sending telemetry to backends)

Configure OpenTelemetry Collector exporters: OTLP gRPC/HTTP, Prometheus, prometheusremotewrite, L...

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!