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.
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.