Seth Geoghegan
05/04/2022, 2:30 PMSeth Geoghegan
05/04/2022, 2:30 PM# index.ts
import StackA from './StackA'
import StackB from './StackB'
import StackC from './StackC'
import { App } from "@serverless-stack/resources";
export default function main(app: App) {
const stackA = new StackA(this,'stack-a')
const stackB = new StackB(this,'stack-b',{table: stackA.table}
const stackC = new StackC(this,'stack-c',{table: stackA.table, queue: stackB.queue})
// more stuff here...
}
Seth Geoghegan
05/04/2022, 2:33 PM# index.ts
import {StackA} from './StackA'
import {StackB} from './StackB'
import {StackC} from './StackC'
import { use, App } from "@serverless-stack/resources";
export default function main(app: App) {
const { table } = use(StackA);
const { queue } = use(StackB);
// not sure what to do with table/queue here?
app.stack(stackA).stack(stackB).stack(stackC);
}
Seth Geoghegan
05/04/2022, 2:35 PMapp.stack(...)
or re-think how I'm managing my dependenciesSeth Geoghegan
05/04/2022, 2:35 PMArpad
05/04/2022, 2:39 PMSeth Geoghegan
05/04/2022, 2:44 PMCarlos Daniel
05/04/2022, 2:46 PMSeth Geoghegan
05/04/2022, 2:48 PMArpad
05/04/2022, 3:08 PMArpad
05/04/2022, 3:10 PMthdxr
05/04/2022, 3:41 PMmain
functionthdxr
05/04/2022, 3:41 PMthdxr
05/04/2022, 3:41 PMthdxr
05/04/2022, 3:42 PMmain
file, which gets really messy as you have a more complicated setup discouraging people from creating many stacksthdxr
05/04/2022, 3:47 PMthdxr
05/04/2022, 3:47 PMSeth Geoghegan
05/04/2022, 3:47 PMmain
file. That does feel messy. One thing I sometimes do in the main
file is set application-wide dependencies, like default function environment variables
app.addDefaultFunctionEnv({TABLE_NAME: stackA.tableName, QUEUE_NAME: stackB.queueName, ...}})
In the functional approach, would you have each stack add environment variables to the app
directly?
export function StackA({ stack, app }: sst.StackContext) {
const table = sst.Table(...)
app.addDefaultFunctionEnv({TABLE_NAME: table.tableName})
}
export function StackB({ stack, app }: sst.StackContext) {
const table = sst.Queue(...)
app.addDefaultFunctionEnv({QUEUE_NAME: queue.queueName})
}
thdxr
05/04/2022, 3:48 PMapp.addDefaultFunctionEnv
in mainthdxr
05/04/2022, 3:48 PMthdxr
05/04/2022, 3:49 PMthdxr
05/04/2022, 3:49 PMSeth Geoghegan
05/04/2022, 3:49 PMSeth Geoghegan
05/04/2022, 3:49 PMSeth Geoghegan
05/04/2022, 4:37 PMindex.ts
look like when using MyStack
and AnotherStack
?
I'm doing something like this:
# index.ts
import {AnotherStack} from './AnotherStack'
export default function main(app: App) {
app.setDefaultFunctionProps(...)
app.stack(AnotherStack)
}
where AnotherStack looks like this
# AnotherStack.ts
import { StackContext, use } from "@serverless-stack/resources"
import { MyStack } from "./MyStack"
export function AnotherStack({ stack }: StackContext) {
const { table } = use(MyStack);
}
and it's telling me
Error: StackWrongOrder: Initialize "MyStack" stack before "AnotherStack" stack
When I update index.ts
to include MyStack before AnotherStack
app.stack(MyStack).stack(AnotherStack);
I get an error message from Cloudformation saying the resource (in my case a Table) already existsthdxr
05/04/2022, 4:38 PMthdxr
05/04/2022, 4:39 PMSeth Geoghegan
05/04/2022, 4:43 PMDELETE_SKIPPED
statusSeth Geoghegan
05/04/2022, 4:43 PMthdxr
05/04/2022, 4:44 PMSeth Geoghegan
05/04/2022, 5:06 PMAdam Fanello
05/04/2022, 7:57 PMuse
function. Isn't this highly coupling the stacks? The app used to be the arbitrator of this so that dependent stacks didn't need to know where resources came from.Adam Fanello
05/04/2022, 7:59 PMAdam Fanello
05/04/2022, 8:00 PMthdxr
05/04/2022, 8:08 PMAdam Fanello
05/04/2022, 8:10 PMthdxr
05/04/2022, 8:11 PMthdxr
05/04/2022, 8:11 PMAdam Fanello
05/04/2022, 8:16 PMthdxr
05/04/2022, 8:16 PMexport function StackBuilder(a: string, b: string) {
return function (ctx: StackContext) {
}
}
const StackA = StackBuilder("1", "2")
const StackB = StackBuilder("3", "4")
const StackC = StackBuilder("4", "5")
thdxr
05/04/2022, 8:17 PMAdam Fanello
05/04/2022, 8:17 PMDerek Kershner
05/04/2022, 8:27 PMfrom
methods.
I donāt see anything about the use
method increasing coupling more than other prop-based methods (itās Cfn Exports either way).Derek Kershner
05/04/2022, 8:29 PMthdxr
05/04/2022, 8:29 PMprops
as opposed to granular resourcesthdxr
05/04/2022, 8:30 PMconst vpc = use(Vpc)
which is a stack that has a vpc and nothing elseDerek Kershner
05/04/2022, 8:32 PMthdxr
05/04/2022, 8:32 PMthdxr
05/04/2022, 8:33 PMDerek Kershner
05/04/2022, 8:35 PMDerek Kershner
05/04/2022, 8:37 PMDerek Kershner
05/04/2022, 8:38 PMAdam Fanello
05/04/2022, 8:39 PMuse
? At that point though, perhaps the only new advantage is async functions. (Which I can probably use to get rid of some custom resources!)Derek Kershner
05/04/2022, 8:44 PMthdxr
05/04/2022, 8:45 PMDerek Kershner
05/04/2022, 8:46 PMAdam Fanello
05/04/2022, 8:59 PMDerek Kershner
05/04/2022, 9:02 PM