Resource — describe what you want
hcl
resource "aws_instance" "web" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t3.micro" }
Run terraform apply and Terraform creates exactly one EC2 as described. Change instance_type and apply again — Terraform computes the diff and changes only what is needed.
State — Terraform's memory
The state file (terraform.tfstate) records the real infrastructure. Thanks to state, Terraform knows what already exists, what drifted outside Terraform, and what to delete on terraform destroy.
Golden rules for state
-
Never edit state by hand — use
terraform import/terraform state mv. - Never commit state to Git — it can contain secrets.
- Teams use a remote backend (S3 + DynamoDB lock) so two people never apply at once.
Basic workflow
bash
terraform init # download providers terraform plan # preview changes terraform apply # execute
plan before apply is mandatory — always know exactly what Terraform will do before it does it.