HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 100 of 223

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

HashiCorp Terraform Associate (004) Question 100

Single answer4b Refer to resource attributes and create cross-resource references

Your team is provisioning AWS infrastructure with Terraform. An EC2 instance must be launched into a subnet that Terraform also creates in the same configuration. A teammate hard-coded the subnet ID in the instance resource, which caused failures when the subnet was replaced in a different workspace. You want Terraform to automatically use the subnet created in this configuration and preserve the dependency relationship. Which configuration change is the best solution?

  1. A

    Set subnet_id = aws_subnet.app.id in the aws_instance resource.

  2. B

    Set subnet_id = "${var.subnet_id}" and update the variable value after each apply.

  3. C

    Add depends_on = [aws_subnet.app] to the aws_instance resource, but keep the hard-coded subnet_id.

  4. D

    Set subnet_id = aws_subnet.app[0].id even though aws_subnet.app is a single resource without count or for_each.

Show answer and explanation

Correct answer: A

Explanation

Terraform best practice is to reference managed resource attributes directly when one resource needs information produced by another resource. In this scenario, aws_instance should use aws_subnet.app.id for subnet_id. This both passes the correct value and creates an implicit dependency in Terraform's graph, allowing Terraform to determine proper creation, update, and replacement order. Using variables for values Terraform already knows, or relying only on depends_on, is less reliable and does not model the data flow as clearly. HashiCorp documentation on resource references and expressions explains that references to resource attributes are the standard mechanism for connecting resources within a configuration.

  • A. Correct.

    Correct. Referencing aws_subnet.app.id uses the managed resource's exported attribute directly. This creates an implicit dependency so Terraform knows the instance depends on the subnet and will use the current subnet ID from state during planning and apply. This is the recommended way to create cross-resource references in the same configuration.

  • B. Incorrect.

    Incorrect. Using a variable for a value that is already produced by another managed resource breaks the direct cross-resource reference. Although variables are valid inputs, manually updating the variable after each apply is error-prone and does not create an implicit dependency between the instance and the subnet resource.

  • C. Incorrect.

    Incorrect. depends_on can enforce ordering, but it does not solve the core problem of passing the correct subnet ID. If subnet_id remains hard-coded, Terraform may still attempt to use an outdated ID. Cross-resource references should use resource attributes, not only explicit dependencies, when one resource needs data from another.

  • D. Incorrect.

    Incorrect. Index syntax like [0] is only valid when the resource uses count, or key-based access when using for_each. For a single resource block without count or for_each, the correct reference is aws_subnet.app.id. Using aws_subnet.app[0].id would produce a configuration error.

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