HashiCorp Terraform Associate (004) Question 96
Single answer4b Refer to resource attributes and create cross-resource referencesYour team is provisioning an AWS VPC and a subnet in the same Terraform configuration. The subnet must be created inside the VPC that Terraform creates during the same apply. A teammate writes the following configuration:
resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" }
resource "aws_subnet" "app" { cidr_block = "10.0.1.0/24" vpc_id = "aws_vpc.main.id" }
When running terraform plan, Terraform shows that the subnet argument is a literal string instead of a reference to the VPC ID. Which change correctly creates a cross-resource reference so Terraform can determine the dependency and use the VPC's ID at apply time?
- A
Change vpc_id to vpc_id = aws_vpc.main.id
- B
Change vpc_id to vpc_id = ${aws_vpc.main.id}
- C
Add depends_on = [aws_vpc.main.id] to aws_subnet.app and keep vpc_id unchanged
- D
Change vpc_id to vpc_id = var.aws_vpc.main.id
Show answer and explanation
Correct answer: A
Explanation
Terraform creates cross-resource references by using expression syntax to read one resource's attributes from another resource block. In this scenario, aws_subnet.app must set vpc_id = aws_vpc.main.id. That reference tells Terraform to use the ID produced by aws_vpc.main and automatically establishes an implicit dependency between the subnet and the VPC. Quoting the reference turns it into a plain string, so Terraform no longer evaluates it as an expression. HashiCorp documentation on references and expressions explains that managed resources are referenced with the pattern
- A. Correct.
Correct. In Terraform expression syntax, a resource attribute is referenced directly as aws_vpc.main.id. This creates both the value flow and an implicit dependency, so Terraform knows the subnet depends on the VPC and will use the VPC ID once it is known. This is the standard way to create cross-resource references in modern Terraform.
- B. Incorrect.
Incorrect. Although older Terraform examples sometimes showed interpolation-only expressions like ${aws_vpc.main.id}, current Terraform syntax uses direct expressions without wrapping the whole value in interpolation markers. In many contexts Terraform accepts interpolation syntax for backward compatibility, but the best and expected answer for Terraform Associate 004 is the direct reference aws_vpc.main.id.
- C. Incorrect.
Incorrect. depends_on controls dependency ordering, but it does not substitute the needed value into vpc_id. Keeping vpc_id = "aws_vpc.main.id" leaves it as a literal string, which is not a valid VPC ID. A cross-resource reference must be used for the argument value itself. Also, depends_on should reference resources or modules, not resource attributes like aws_vpc.main.id.
- D. Incorrect.
Incorrect. var.aws_vpc.main.id is not valid syntax for accessing a managed resource. The var object is only for input variables, such as var.vpc_id. A managed resource created in the configuration is referenced by its resource type, name, and attribute, such as aws_vpc.main.id.