
Example: A simple Azure Storage Account
Here is a minimalazurerm_storage_account resource using a variable for the storage account name:
terraform apply and pass the storage account name, Terraform does two things:
- Creates (or updates) the resource in Azure.
- Writes a local state file called
terraform.tfstate(by default).
terraform.tfstate file represents the current infrastructure as Terraform knows it.
What is inside the state file?
Terraform state acts as the authoritative mapping between your configuration and the real-world infrastructure. Think of it as Terraform’s memory: without it, Terraform cannot determine which resources exist, their IDs, or which attributes have changed. A simplified excerpt of a Terraform state file (JSON):Protect your state: state files can contain sensitive values (resource IDs, endpoints, and occasionally secrets). When storing state remotely, enable encryption and strict access controls to prevent unauthorized access.
Where is state stored?
- By default:
terraform.tfstatestored locally in your working directory. This is fine for learning, demos, or single-user scenarios. - For teams and automation: use a remote backend (Azure Storage, Amazon S3, Terraform Cloud, etc.) to centralize state, provide locking, and improve access control.
Warning: Local state lacks concurrency protection. Multiple users or CI jobs operating on the same local state can cause conflicts or corruption. Use a remote backend for team workflows.
Remote backends — why and when to use them
Remote backends provide several advantages:- Centralized storage so all team members and CI pipelines use the same state.
- Locking support (depending on backend) to prevent concurrent operations.
- Access control, auditability, and encryption at rest.
- Integration with remote runs and workspace concepts (e.g., Terraform Cloud).
- Azure Storage Account: reliable for Azure-native workflows.
- Amazon S3 (with DynamoDB locking): common for AWS and cross-cloud setups.
- Terraform Cloud: managed state, remote runs, and policy enforcement.
Quick comparison: local vs remote state
Why state matters (summary)
- Maps Terraform configuration to real resources (IDs, attributes).
- Enables incremental updates — Terraform only modifies what changed.
- Prevents duplicate resource creation and is required for plan/apply/destroy/import.
- Serves as the source of truth for Terraform operations.
terraform.tfstate to confirm the resources you’ve deployed.
Now that you understand Terraform state and why it’s important, the next step is to configure a remote backend suited to your team and automation needs.