HashiCorp Terraform Associate (004) Question 117
Single answer4f Define resource dependencies in configurationA team manages AWS infrastructure with Terraform. In one module, an EC2 instance is created and then a null_resource uses a remote-exec provisioner to configure software on that instance over SSH. The provisioner connects using the instance's public IP, but the null_resource does not currently reference the EC2 instance anywhere in its arguments. During terraform apply, the team sometimes sees the null_resource start before the instance is fully created, causing connection failures. What is the best way to ensure Terraform creates the EC2 instance before running the null_resource?
- A
Add
depends_on = [aws_instance.app]to thenull_resource. - B
Add
lifecycle { create_before_destroy = true }to thenull_resource. - C
Run
terraform apply -target=aws_instance.appbefore a normal apply. - D
Add
provider = aws_instance.appto thenull_resource.
Show answer and explanation
Correct answer: A
Explanation
Terraform automatically builds a dependency graph from references in expressions, which are called implicit dependencies. When one resource does not directly reference another but still must wait for it, you should use the depends_on meta-argument to declare an explicit dependency. This is especially common with resources like null_resource, provisioners, or data flows where the dependency is operational rather than expressed through an attribute reference. HashiCorp documentation recommends relying on implicit dependencies when possible, and using depends_on only when Terraform cannot otherwise determine the correct order. In this scenario, because the null_resource does not reference aws_instance.app, the best practice is to add depends_on = [aws_instance.app].
- A. Correct.
Correct.
depends_oncreates an explicit dependency when Terraform cannot infer one from expressions. In this scenario, thenull_resourcedoes not reference any attribute ofaws_instance.app, so Terraform has no implicit dependency to follow. Addingdepends_on = [aws_instance.app]ensures Terraform waits until the EC2 instance is created before creating thenull_resourceand running its provisioner. - B. Incorrect.
Incorrect.
create_before_destroyis a lifecycle rule that affects replacement behavior for a resource, not ordering between unrelated resources during creation. It does not tell Terraform that thenull_resourcemust wait for the EC2 instance. - C. Incorrect.
Incorrect. Using
-targetcan force Terraform to apply a subset of resources, but it is not the recommended way to model normal resource ordering. Targeting is intended for exceptional situations such as recovery or troubleshooting, not as a substitute for defining dependencies in configuration. - D. Incorrect.
Incorrect. The
providermeta-argument selects which provider configuration a resource should use. It cannot point to another resource and does not create a dependency between resources. This option reflects a misunderstanding of how provider selection works in Terraform.