Hey all - is it possible to pass the output of one...
# sst
m
Hey all - is it possible to pass the output of one stack to another stack (example: I have two stacks and want to reference the URL of one stack in the other)
t
Yep you can make public variables on stack1 and pass those into stack2
m
@thdxr thank you for this info!
a
That means you can’t use…
this.addOutputs({
.
I was looking into that today.
I mean, we should be able to use outputs, instead of local-variable.
@thdxr thoughts?
t
The nice thing with the variable approach is it'll be strongly typed everywhere.
this.addOutputs
would be entirely runtime
We probably can provide a way to get outputs out of a stack though if you want to do that
a
Well, I’m thinking how it should be.
Because outputs is also used to share info.
So we have 2 ways.
I know about the typing.
But… why we have 2 ways to communicate or share?
Ideally an stack should have input/output, that’s it.
I know some are runtime and other are build-time… but.. that’s a technology thing… why we don’t build abstractions and we just think… INPUT/OUTPUT?
f
@Adrián Mouly sharing local variables also uses CFN export/import. It’s equivalent to
addOutputs
in 1 stack with export, and then importing it in another stack.
a
I know @Frank.
I agree with that.
But why we have to “code” it differently?
I mean, why there are 2 ways which actually makes the same thing?
I think that’s kind of confusing sometimes.
It’s not clear out of the box that declaring global variables does the output behind the scenes.
t
I think it's actually a good thing that the user can just share variables across instances the way they'd expect in normal js/ts without knowing about the underlying mechanism. That said I wish there was a bit more convention around it, rough idea of what that could be
Copy code
type Shared = { api_url: string }
class MyStack extends sst.Stack<Shared> {
  
}

const mystack = new MyStack()
const otherStack = new OtherStack({ mystack: mystack.shared})
That would keep everything fully typed
and would feel more like an explicit/official way to share stuff
s
Hhmm, yeah. I tend to do:
Copy code
export interface MultiStackProps extends cdk.StackProps {
  apiUrl?: sst.Api['url']
}
In the index.ts, and all my stacks implement that interface for their props.