Terraform Basics: Providers, Resources, State and the Plan/Apply Workflow
Learn Terraform fundamentals: HCL, providers, resources, variables and outputs, how state works, remote backends with locking, and the plan/apply loop.
Published December 4, 2026
Clicking through a cloud console works for one server and fails for a hundred. Terraform describes infrastructure in code, so environments are reproducible, changes are reviewed in pull requests, and drift can be detected. Its core workflow is simple, but understanding state is what separates smooth operations from 2 a.m. surprises.
The concept
You write configuration in HCL: providers (plugins for AWS, Azure, Google Cloud, Cloudflare, GitHub, Kubernetes and hundreds more), resources (things to create), data sources (things to look up), variables and outputs. terraform init downloads providers, terraform plan shows what would change, and terraform apply makes the changes.
Terraform records what it manages in a state file: the mapping between resources in your code and real objects in the cloud, with their attributes. Plans compare configuration, state and reality. For teams, state must live in a remote backend (S3 with locking, Google Cloud Storage, Azure Blob, Terraform Cloud/HCP and others) so everyone shares it and concurrent applies are prevented. State can contain secrets, so protect it accordingly.
Plan and apply
Building blocks
| Concept | What it is | Example |
|---|---|---|
| Provider | Plugin that talks to an API | hashicorp/aws |
| Resource | Infrastructure object to manage | aws_s3_bucket.assets |
| Data source | Read existing info | data.aws_ami.ubuntu |
| Variable / output | Inputs and exported values | var.env, output.bucket_name |
| State | Record of managed objects | terraform.tfstate in remote backend |
| Module | Reusable group of resources | modules/vpc |
Why remote state needs locking
Practical examples
A small configuration with remote state
S3 backend with native state locking in recent Terraform versions.
terraform {
required_providers { aws = { source = "hashicorp/aws", version = "~> 5.0" } }
backend "s3" {
bucket = "example-tf-state"
key = "prod/web/terraform.tfstate"
region = "ap-south-1"
use_lockfile = true
encrypt = true
}
}
provider "aws" { region = "ap-south-1" }
resource "aws_s3_bucket" "assets" {
bucket = "example-prod-assets"
tags = { env = "prod", owner = "web" }
}
output "assets_bucket" { value = aws_s3_bucket.assets.bucket }Everyday commands
Always read the plan before applying.
terraform init
terraform fmt -check && terraform validate
terraform plan -out tfplan
terraform apply tfplan
terraform state listBring existing resources under management
Import blocks generate state without recreating the resource.
import {
to = aws_s3_bucket.legacy
id = "legacy-bucket-name"
}
# terraform plan -generate-config-out=generated.tfCommon mistakes
- Local state files on laptops, lost or out of sync between engineers.
- Committing state (which may contain secrets) to git.
- Applying without reading the plan, especially destroy/replace actions.
- Making manual console changes that drift from code.
Key takeaways
- Terraform turns infrastructure into reviewed, reproducible code.
- plan shows the diff; apply executes it.
- State maps code to real resources; keep it remote, locked and protected.
- Use import to adopt existing infrastructure.
FAQ
What happens if state is lost?
Terraform no longer knows what it manages; you must re-import resources. Enable versioning on the state bucket to recover.
How do I handle multiple environments?
Separate state per environment, via separate directories/stacks or workspaces, with shared modules.
Is Terraform free?
The CLI is source-available under the BSL licence; OpenTofu is an open-source fork under the Linux Foundation.