State and Drift Detection
What state is, remote backends and locking, detecting and fixing drift, and importing existing databases.
What state is
Terraform keeps a state file: its record of every resource it manages, with each one's ID and settings. State is how Terraform links the code to real resources, works out what's changed, and notices drift. It needs to be stored safely, shared, and locked, because losing or corrupting it causes real trouble.
When Terraform creates a database, the cloud gives it an ID, such as an OCID. Terraform writes that ID, with the database's settings, into the state, so next time it knows that oci_database_autonomous_database.sales in the code is that particular database.
In this example, the code asks for 4 ECPUs, Terraform last built 2, and someone has since changed the real database to 3 in the console. The plan reports both: the drift from 2 to 3, and the change needed to reach 4.
Where state should live
By default, state is a local file, terraform.tfstate, in the project folder. That's fine for learning, but not for a team: everyone would have a different copy, and a lost laptop means lost state. Teams store state in a remote backend:
# versions.tf: keep state in an S3-compatible bucket, with locking
terraform {
backend "s3" {
bucket = "acme-terraform-state"
key = "sales-db/prod.tfstate"
region = "us-east-1"
use_lockfile = true
}
}
| Backend | Where state is stored |
|---|---|
s3 | Amazon S3, or any S3-compatible storage, such as OCI Object Storage |
azurerm | Azure Blob Storage |
gcs | Google Cloud Storage |
| HCP Terraform | HashiCorp's managed service, with history, locking, and runs built in |
| OCI Resource Manager | OCI's managed Terraform service, which stores state for you |
- Locking
- While one person or pipeline is applying, the state is locked, so a second apply can't run at the same time and corrupt it.
- Versioning
- Turn on versioning for the storage bucket, so an earlier copy of the state can be recovered if something goes wrong.
- Separate state per environment
- Keep development, test, and production state apart, such as
sales-db/dev.tfstateandsales-db/prod.tfstate, so a mistake in one can't touch another.
Drift detection
Drift is any difference between the real infrastructure and what Terraform expects, usually caused by a manual change: someone resized a database in the console during an incident, switched off automatic backups, or opened a firewall rule "temporarily". Every terraform plan checks for it:
$ terraform plan -var-file=prod.tfvars
Note: Objects have changed outside of Terraform
# oci_database_autonomous_database.sales has changed
~ resource "oci_database_autonomous_database" "sales" {
~ compute_count = 2 -> 3
}
Terraform will perform the following actions:
# oci_database_autonomous_database.sales will be updated in-place
~ resource "oci_database_autonomous_database" "sales" {
~ compute_count = 3 -> 4
}
Plan: 0 to add, 1 to change, 0 to destroy.
When drift is found, the team decides which is right:
- The code is right: apply, and Terraform puts the resource back to match the code.
- The manual change was right: update the code to match, through a pull request, so it becomes the agreed setting.
To see drift without proposing any changes, use terraform plan -refresh-only. Many teams run it on a schedule, every night for production, and send an alert when anything has drifted.
Bringing existing databases under Terraform
Most DBAs start with databases that already exist, created by hand. Terraform can import them, so it manages them from then on, without rebuilding anything. Add an import block naming the resource and its ID:
# import.tf: adopt an existing Autonomous Database
import {
to = oci_database_autonomous_database.legacy_sales
id = "ocid1.autonomousdatabase.oc1.iad.bbbb..."
}
# let Terraform write the matching resource code for you
$ terraform plan -generate-config-out=generated.tf
Review and tidy the generated code, then plan until it shows no changes: that proves the code exactly describes the real database. Then apply, and the database is under Terraform's control.
Key takeaways
- State records what Terraform manages, linking each resource in the code to a real one by its ID.
- Teams keep state in a remote backend, with locking, versioning, encryption, and separate state per environment.
- State can contain secrets: never commit it to Git, and limit who can read it.
- Drift is a difference between reality and what Terraform expects; every plan reports it, and
-refresh-onlyshows only drift. - Fix drift by applying the code, or by updating the code through a pull request.
- Existing databases can be brought under Terraform with
importblocks and generated configuration.