Module 4: Terraform Configuration

Lesson 4.1: Resource vs. Data Blocks

Plain language

resource data
Purpose Terraform creates, updates, and deletes the object Terraform reads an existing object
Lifecycle owner Terraform Something else (another team, another config, the platform)
Reference prefix docker_image.nginx data.docker_image.nginx
In state? Yes Yes, its last read result is recorded
Plan symbol + ~ - -/+ <= when read during apply
HCL
# Terraform manages this image (pull on create, remove on destroy)
resource "docker_image" "nginx" {
  name = "nginx:stable"
}

# Terraform only looks up an existing local image
data "docker_image" "base" {
  name = "alpine:3.20"
}

Under the hood

  • When data sources are read. Data sources are normally read during plan. A read is deferred to apply (shown as <=) in two cases: its arguments depend on values that are (known after apply), or it has depends_on pointing at something with pending changes.
  • terraform_data is a built-in managed resource, added in 1.4. It needs no provider download, because it comes from the built-in terraform.io/builtin/terraform provider. It stores an input value and mirrors it as output, and triggers_replace forces replacement when a value changes. It is the modern replacement for null_resource, which needs the hashicorp/null provider.
HCL
resource "terraform_data" "deploy_marker" {
  input            = var.release      # stored; readable as .output
  triggers_replace = var.release      # new value → resource replaced
}