TF-Associate-004 - Terraform configuration - Section 4b

Refer to resource attributes and create cross-resource references.

Referencing one resource's attribute from another (type.name.attribute) both wires the value and creates an implicit dependency Terraform uses to order operations. Candidates should predict apply ordering from references and distinguish an argument set in configuration from an attribute exported after apply.

attribute referenceimplicit dependencycross-resource referenceargument versus attribute

Practice question for this objective

Free sampleTerraform configurationmedium

A configuration defines an aws_eip resource whose argument references the id of an aws_instance declared in the same file, and neither resource carries a depends_on argument. When the practitioner runs terraform apply on an empty state, in what order does Terraform create the two resources, and why?

resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = "t3.micro"
}
resource "aws_eip" "web" {
  instance = aws_instance.web.id
}
  • ATerraform creates the aws_instance first because the reference to aws_instance.web.id forms an implicit dependency in the graph. Correct
  • BTerraform creates the aws_eip first because Elastic IP addresses must exist before any instance can be attached to them.
  • CTerraform creates both resources in parallel because no depends_on argument was written to declare an ordering.
  • DTerraform creates them in the order they appear in the file, so the aws_instance happens to be first only because of its position.
An attribute reference between resources creates an implicit dependency that orders the referenced resource before the one referencing it. Terraform builds a dependency graph from expressions in the configuration. When aws_eip.web reads aws_instance.web.id, an edge is added so the instance is created before the Elastic IP, without any explicit depends_on.

Why A is correct: Referencing one resource's attribute inside another creates an implicit dependency, so Terraform orders the referenced aws_instance before the aws_eip that consumes its id.

Why B is wrong: This inverts the real relationship; the aws_eip consumes the instance id, so the instance must exist first regardless of any assumption about address allocation order.

Why C is wrong: It is tempting to think ordering needs depends_on, but the attribute reference already builds the edge, so the two cannot be created in parallel.

Why D is wrong: Block order in a file does not drive creation order; the dependency graph does, and here it happens to agree by coincidence rather than by rule.

See more TF-Associate-004 practice questions, answers explained.

More in this domain

Back to all Terraform configuration objectives, or the TF-Associate-004 cert hub.

Examworthy is not affiliated with or endorsed by HashiCorp. Original, blueprint-aligned practice material only.