Terraform: "known only after apply" ISSUE

10,185

Solution 1

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]

Solution 2

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.

Solution 3

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.

Share:
10,185
Red Bottle
Author by

Red Bottle

"Why wear gloves when you can knee right on the chin" -Joe Rogan

Updated on June 30, 2022

Comments

  • Red Bottle
    Red Bottle almost 2 years

    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?

  • Red Bottle
    Red Bottle about 3 years
    Thanks. I can really use a solution if you have one.
  • sydney
    sydney over 2 years
    Informative, but not helpful.