Terraform: "known only after apply" ISSUE

Viewed 26779

I'm creating an aws_subnet and referencing it in another resource.

Example:

resource "aws_subnet" "mango" {
     vpc_id     = aws_vpc.mango.id
     cidr_block = "${var.subnet_cidr}"
  }

The reference

 network_configuration {
    subnets          = "${aws_subnet.mango.id}"
  }

When planning it I get aws_subnet.mango.id is a string, known only after apply error. I'm new to Terraform. Is there something similar to Cloudformation's DependsOn or Export/Import?

4 Answers

This is a case of explicit dependency. The argument depends_on similar to CloudFormation's DependsOn solves such dependencies.

Note: "Since Terraform will wait to create the dependent resource until after the specified resource is created, adding explicit dependencies can increase the length of time it takes for Terraform to create your infrastructure."

Example:

depends_on = [aws_subnet.mango]

This line:

 cidr_block = "${var.subnet_cidr}"

should look like

 cidr_block = var.subnet_cidr

And this line:

subnets          = "${aws_subnet.mango.id}"

should look like

subnets          = aws_subnet.mango.id

Terraform gives a warning when a string value only has a template in it. The reason is that for cases like yours, it's able to make a graph with the bare value and resolve it on apply, but it's unable to make the string without creating the resource first.

The information like ID or other such information which will be generated by AWS, cannot be predicted by terraform plan as this step only does a dry run and doesn't apply any changes.

The fields which have known only after apply is not an error, but just informs the user that these fields only get populated in terraform state after its applied. The dependency order is handled by Terraform and hence referring values (even those which have known only after apply) will be resolved at run time.

The error in this case is not the string "known only after apply" but the message "Incorrect attribute value type".

subnets (plural) requires a list of strings but you gave only one string.

network_configuration {
  subnets          = ["${aws_subnet.mango.id}"]
}

The depends_on is not necessary in this case, tf resolves this dependency by itself. depends_on is only important if tf can't get this by itself.

writing "${foo.bar}" instead of foo.bar is also no problem but doesn't follow tf's code-style-rules.

Related