Solved: working with output variables

I have a bit of a chicken and egg situation and I’d welcome some input from more some more knowledgeable folks.

I’m attempting to use Mastercard’s restapi provider to interact with an API, but the difficulty is that I need to make use of an ID returned on creation of the resource.

I can get the ID of the cluster from the API’s response when initially creating the cluster, but then I would need to use that in the nodepool creation in order to specify the correct API path. Below is some code which I know won’t work, but it shows what I want to achieve.

resource "restapi_object" "cluster" {
...
}

output "clusterid" {
  value = jsondecode(restapi_object.cluster.api_response).id
}

resource "restapi_object" "nodepool" {
  path        = "/v5/clusters/${output.clusterid}/nodepools/"
...
  depends_on = [
    restapi_object.cluster,
  ]
}

Any suggestions welcome!

I ended up working around this by moving the two resources into their own modules and providing the output from the cluster ID to the nodepool module. This also introduced the dependency graph I needed.

The real problem is that outputs are only passed to parent module. So if your example has them in the same module it won’t render in the scope of that module. So instead, render it inline of the nodepool path.

resource "restapi_object" "cluster" {
...
}

resource "restapi_object" "nodepool" {
  path        = "/v5/clusters/${jsondecode(restapi_object.cluster.api_response).id}/nodepools/"
...
  depends_on = [
    restapi_object.cluster,
  ]
}

Your comment about breaking them out into seperate modules would fix it because it would be rendered before being passed to the second module. Doing it in the same module as I shown above will also keep the dependency graph correct.