HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 117 of 223

Terraform Associate 004. Associate level, HashiCorp. Free question with the correct answer and a full explanation.

HashiCorp Terraform Associate (004) Question 117

Single answer4f Define resource dependencies in configuration

A 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?

  1. A

    Add depends_on = [aws_instance.app] to the null_resource.

  2. B

    Add lifecycle { create_before_destroy = true } to the null_resource.

  3. C

    Run terraform apply -target=aws_instance.app before a normal apply.

  4. D

    Add provider = aws_instance.app to the null_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_on creates an explicit dependency when Terraform cannot infer one from expressions. In this scenario, the null_resource does not reference any attribute of aws_instance.app, so Terraform has no implicit dependency to follow. Adding depends_on = [aws_instance.app] ensures Terraform waits until the EC2 instance is created before creating the null_resource and running its provisioner.

  • B. Incorrect.

    Incorrect. create_before_destroy is a lifecycle rule that affects replacement behavior for a resource, not ordering between unrelated resources during creation. It does not tell Terraform that the null_resource must wait for the EC2 instance.

  • C. Incorrect.

    Incorrect. Using -target can 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 provider meta-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.

Timed practice exam

Take a HashiCorp Terraform Associate (004) practice test under exam conditions

70 questions in 60 minutes, drawn from this bank, with a score report and a per-question review when you finish.

Start timed exam