I didn’t realize CDK/SST could have a single App t...
# help
d
I didn’t realize CDK/SST could have a single App that creates two stacks, each in different regions. If that is indeed possible, is there a way to create a construct that adds another stack in a different region?
s
While reading The CDK Book, I was struck by this statement
In AWS CDK, you can, and should, specify the environment for every stack in the StackProps you pass to the constructor.
new cdk.Stack(this, "Stack1", new Struct { Env = new Struct {
Account = "123456789012",
Region = "eu-central-1"
}
});
I highly recommend providing this information explicitly and not using whatever credentials are currently active in your CLI to determine the target environment of your CDK stack instance. For example, suppose you have multiple CLI profiles on your machine and access to several accounts. In that case, you might deploy CDK apps into accounts of other customers, projects, or stages. This is something you definitely want to prevent.
But, I don't think this is something SST allows.
t
...someone try it
I think it might work
s
I briefly looked into this while trying to determine how to manage different resources lifecycles in the same App. I wanted an RDS database that only had dev/stage/prod stages and an API that had <username>/stage/prod stages. The <username> stage of the API should always use the dev RDS stage. The problem I ran into is that each SST app requires all stacks to share the same stage name. I assume the same would be true about Accounts and Regions. @Simon Reilly and I had a discussion about this recently in this channel
t
ah
I think the pattern I'd use is one set of stacks + multiple deployments
stacks can conditionally do things different based on where they're going
s
Yeah, I am currently experimenting with multiple SST apps to implement what I think you're describing. One is for my RDS instance and the other for my API, I deploy each independently. I'm not sure how to manage that workflow in the same SST app.
d
@Seth Geoghegan can your app not just make the needed adjustments based on whatever account / stage / region extra its being deployed to? We do this now with the stage and account.
Also, sorry to repost… but I think my thread was highjacked. 😆 https://serverless-stack.slack.com/archives/C01JG3B20RY/p1641576392079300 Still curious if its possible to have a construct that adds a stack to the root. Then be able to pass params into that stack and get outputs out for use in the init stack. Perhaps using something like this if the output passing is not natively supported.
o
@thdxr Doesn't look like it's possible see https://github.com/serverless-stack/serverless-stack/blob/master/packages/resources/src/Stack.ts#L28 it overrives the env to the root.region
t
ah
Yeah I'm not super sure if I love the pattern of a single app deploying to multiple regions. There's all kinds of things that will seem like it works but won't - like any cross stack deps
IMO you should deploy the same app to each region and the app can conditionally behave differently
o
99% of the time yes
s
Also, cdk bootstrap is needed in each region you deploy to I think
o
But when dealing with Cloudfront for instance you have to have the ssl cert in us-east-1
and Lambda@edge
@Simon Reilly interestingly sst detected both regions and bootstrapped them for me.
s
Oh noice
o
But then again it might be because I was already bootstrapped in my "default" region
Would need some testing from a fresh setup
t
Yeah so if you look at CDK under the hood (and in our codebase as well) there are instances of creating a resource in another region
This is done through custom resources usually
d
@thdxr Right…. thats how we have our Custom Resource for cross-region replica buckets setup…. but it feels like those resources are not really tightly coupled to a stack. Sort of managed by CODE that is managed by Cloudformation.
o
Ahhh
t
It's truly terrible 😢
For the cert example I actually have just manually created it outside of IaC 😬
o
I guess something similar could be done for generating a certificate in us-east-2?
@thdxr Likewise 😂
I just supply the arn for it at present in the stack (which feels horrible and dirty and bound to go wrong when I forget to do it)
t
yep I have an arn hardcoded 🤮
o
Well mine is pulled from the .env but still...
t
Someone could make a construct that does all that for you and hides the ugliness ins omething that looks normal
the nextjs construct is basically an example of that
o
You got any docs on making constructs?
a
Perhaps use SST for your application and directly CDK for IaC that you need in other accounts or regions?
o
Again an option but just feels like it's another step, another set of tools and things to break or forget to do!