terraform-cdk icon indicating copy to clipboard operation
terraform-cdk copied to clipboard

Cross-stack dependencies: When referring to a terraform resource across stacks, I receive an invalid reference error

Open fathom-parth opened this issue 2 years ago • 2 comments

Expected Behavior

I expect to be able to pass a terraform resource from one stack to another to be used within a resource's depends_on argument.

Actual Behavior

Instead, the cross-stack reference errors when running a command like cdktf diff or cdktf deploy (though succeeds with synth) complaining that the resource in depends_on is invalid and must be a reference to a "whole object".

Steps to Reproduce

  1. Create 2 stacks, stack1 and stack2
  2. Set an instance variable of stack1 to a terraform resource. In our case, this is a service_networking_connection.
  3. Pass stack1.service_networking_connection to stack2 and have stack2 pass it to a construct. In that construct define a terraform resource (in our case a notebooks_runtime) that adds the service_networking_connection to its depends_on.
  4. Run cdktf diff 'stack1'
  5. Run cdktf deploy 'stack2'
  6. Verify this errors with an invalid depends_on error.

Versions

language: python cdktf-cli: 0.17.1 node: v18.16.1 cdktf: 0.17.1 constructs: 10.2.69 jsii: 1.85.0 terraform: 1.4.6 arch: x64 os: linux 5.15.49-linuxkit python: Python 3.7.16 pip: pip 21.3.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7) pipenv: null

Providers

┌───────────────┬──────────────────┬─────────┬────────────┬──────────────────────────────────┬─────────────────┐ │ Provider Name │ Provider Version │ CDKTF │ Constraint │ Package Name │ Package Version │ ├───────────────┼──────────────────┼─────────┼────────────┼──────────────────────────────────┼─────────────────┤ │ google │ 4.72.1 │ ^0.17.0 │ │ cdktf-cdktf-provider-google │ 8.0.4 │ ├───────────────┼──────────────────┼─────────┼────────────┼──────────────────────────────────┼─────────────────┤ │ google-beta │ 4.72.1 │ ^0.17.0 │ │ cdktf-cdktf-provider-google-beta │ 8.0.4 │ └───────────────┴──────────────────┴─────────┴────────────┴──────────────────────────────────┴─────────────────┘

Gist

https://gist.github.com/fathom-parth/b98e9e5c451e1da0e6a5d4494275554b

Possible Solutions

No response

Workarounds

Currently, rather than granularly passing the TerraformResource to the relevant resource's depends_on argument, I instead set the 2nd stack to be on dependent on the 1st by running

stack2.add_dependency(stack1)

Anything Else?

It's possible that passing complete TerraformResource objects isn't supported since cross-stack references look at remote state? However, I don't see anything in the documentation that states this.

Please let me know if I missed something very obvious.

References

  • https://developer.hashicorp.com/terraform/cdktf/concepts/stacks#cross-stack-references

Help Wanted

  • [ ] I'm interested in contributing a fix myself

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

fathom-parth avatar Jul 21 '23 15:07 fathom-parth

Hey there, there problem seems to be the usage of cross stack references within custom constructs, the workaround you mentioned is the correct thing to do, although it should ideally not be necessary.

DanielMSchmidt avatar Jul 28 '23 10:07 DanielMSchmidt

We might not have the capacity for a permanent fix soon, but we are open for community PRs on this topic 👍

DanielMSchmidt avatar Jul 28 '23 10:07 DanielMSchmidt