KnowledgeHub
VI EN

Bài viết

Terraform Fundamentals: starting with resources and state

The two foundational Terraform concepts: resources describe what you want, state remembers what exists.

20/08/2026 · 0 phút đọc · 12 lượt xem

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

  1. Never edit state by hand — use terraform import / terraform state mv.
  2. Never commit state to Git — it can contain secrets.
  3. 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.