terraform-cdk
                                
                                 terraform-cdk copied to clipboard
                                
                                    terraform-cdk copied to clipboard
                            
                            
                            
                        Cross-stack dependencies: When referring to a terraform resource across stacks, I receive an invalid reference error
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
- Create 2 stacks, stack1andstack2
- Set an instance variable of stack1to a terraform resource. In our case, this is aservice_networking_connection.
- Pass stack1.service_networking_connectiontostack2and havestack2pass it to a construct. In that construct define a terraform resource (in our case anotebooks_runtime) that adds theservice_networking_connectionto itsdepends_on.
- Run cdktf diff 'stack1'
- Run cdktf deploy 'stack2'
- Verify this errors with an invalid depends_onerror.
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
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.
We might not have the capacity for a permanent fix soon, but we are open for community PRs on this topic 👍