HashiCorp Terraform Associate (004) Question 119
Single answer4f Define resource dependencies in configurationA team is provisioning an AWS application stack with Terraform. An EC2 instance must not be created until a bootstrap script has been uploaded to an S3 bucket by a separate resource, even though the instance configuration does not directly reference any attributes from that S3 object. During testing, Terraform sometimes tries to create the EC2 instance before the upload completes, causing the instance startup to fail. What is the best way to ensure Terraform creates the EC2 instance only after the S3 object upload resource has completed?
- A
Add a depends_on meta-argument to the aws_instance resource that references the S3 object upload resource.
- B
Add the S3 object resource name as a tag value on the aws_instance so Terraform detects the dependency automatically.
- C
Use terraform refresh before apply so Terraform recalculates the correct creation order.
- D
Add lifecycle { create_before_destroy = true } to the aws_instance resource.
Show answer and explanation
Correct answer: A
Explanation
Terraform determines resource order primarily through its dependency graph. Implicit dependencies are created when one resource argument references another resource's attribute. When there is a real operational dependency but no direct attribute reference, Terraform best practice is to use the depends_on meta-argument to declare that relationship explicitly. This is especially important for side effects or sequencing requirements, such as ensuring a file upload, policy attachment, or external prerequisite completes before another resource starts. According to Terraform documentation on resource dependencies and the depends_on meta-argument, explicit dependencies should be used only when Terraform cannot infer the dependency automatically from configuration expressions.
- A. Correct.
Correct. Terraform automatically builds most dependencies from expressions that reference other resources. In this scenario, the EC2 instance does not consume any attribute from the S3 object resource, so there is no implicit dependency. The correct solution is to declare an explicit dependency with depends_on on the aws_instance resource. This tells Terraform to wait until the referenced resource has completed before creating the instance.
- B. Incorrect.
Incorrect. Terraform detects implicit dependencies from expression references in configuration, not from arbitrary metadata such as tags unless those tags actually reference a resource attribute in a meaningful expression. Simply placing a resource name as a literal tag value does not create a dependency and is a common misconception.
- C. Incorrect.
Incorrect. terraform refresh updates Terraform state to reflect real infrastructure, but it does not redefine resource ordering for resources that lack dependency relationships. Running refresh would not solve the ordering problem between these resources.
- D. Incorrect.
Incorrect. create_before_destroy changes replacement behavior for a resource when Terraform must recreate it, reducing downtime during updates. It does not control creation order between unrelated resources and does not solve missing dependency relationships.