https://serverless-stack.com/ logo
#sst
Title
# sst
m

Michael Clifford

10/13/2021, 12:12 AM
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

thdxr

10/13/2021, 12:15 AM
Yep you can make public variables on stack1 and pass those into stack2
m

Michael Clifford

10/13/2021, 12:16 AM
@thdxr thank you for this info!
a

Adrián Mouly

10/13/2021, 12:20 AM
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

thdxr

10/13/2021, 12:22 AM
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

Adrián Mouly

10/13/2021, 12:25 AM
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

Frank

10/13/2021, 1:10 AM
@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

Adrián Mouly

10/13/2021, 2:02 AM
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

thdxr

10/13/2021, 2:05 AM
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

Simon Reilly

10/13/2021, 8:00 AM
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.