Module 4: Terraform Configuration

Lesson 4.2: References and Cross-Resource Dependencies

Reference syntax

Object Syntax Example
Managed resource attribute <TYPE>.<NAME>.<ATTR> docker_container.web.id
Data source attribute data.<TYPE>.<NAME>.<ATTR> data.docker_image.base.repo_digest
Instance with count <TYPE>.<NAME>[<INDEX>] local_file.by_count[0].filename
Instance with for_each <TYPE>.<NAME>["<KEY>"] local_file.by_key["alice"].id
All instances (splat) <TYPE>.<NAME>[*].<ATTR> local_file.by_count[*].filename
Input variable var.<NAME> var.environment
Local value local.<NAME> local.common_tags
Child module output module.<NAME>.<OUTPUT> module.network.vpc_id
Ephemeral resource ephemeral.<TYPE>.<NAME>.<ATTR> ephemeral.random_password.app.result
Filesystem and workspace path.module, path.root, path.cwd, terraform.workspace "${path.module}/files"
Iteration count.index, each.key, each.value
The object itself self Only in postconditions, provisioners, and connection blocks

Under the hood

Every reference creates an implicit dependency edge in the graph. Terraform creates referenced objects first, destroys them last, and runs unrelated objects in parallel.

HCL
resource "docker_container" "web" {
  name  = "web"
  image = docker_image.nginx.image_id # implicit dependency: image before container
}