Cross-Stack Java Dependencies

Hello. I’m having trouble getting output from one Java stack to another. I am able to add my outputs for my stack using

        TerraformOutput.Builder.create(this, "name")
                .value(rg.get("name"))
                .build();

But once I do that, how would I reference this stack’s output name?

You don’t need terraform outputs, at least no manual ones in this case (cdktf adds them for you). You need to pass the value you create in one stack into the configuration of a second stack. So e.g. use a public attribute on your one class and use that attribute in the constructor of your other class (and then in the configuration).

1 Like

Ahhhh I see. Thank you for the help. And some of these values aren’t going to be created until the first stack is created. Do I need to worry about passing null values around?

Or I probably just pass the stack through to the next stack and it will pull values from there.

So I think I’m close. One follow up question on this: a value I am passing from one stack is null until it is deployed. To add it to the variable map, I used

secondStackVars.put("id", firstStackOutput.id);

But when I try to create the secondStack, I get an error saying that variable id is required. How can I delay the value of this variable to stack creation?

Ah, actually this was a syntax error on my side. For documentation purposes, this is what I did to make it work.

  1. call your TerraformHclModule.Builder function
        final TerraformHclModule firstStack = TerraformHclModule.Builder.create(this, "firstStack")
                .providers(providers)
                .source(source)
                .variables(firstStackVars)
                .build();
  1. Use the get method to get the output values for cross-stack dependencies and assign it to a public Object
    public Object id;
...
        this.id = firstStack.get("id");
  1. Use that value in my secondStack
        secondStackVars.put("firstStackId", firstStackOutput.id);

Thanks for the quick response to point me in the right direction!