is there any way to (using SST/CDK) map one custom...
# help
s
is there any way to (using SST/CDK) map one custom API Gateway domain to multiple APIs in different stacks?
t
I was thinking about this. Was going to try using the proxy feature but was wondering what latency that adds
sst.Api supports it
a
I think it’s what I’m doing at the moment, sent screenshot.
Been doing it with GW v1 too.
s
is it possible right now? take this code:
Copy code
new Api(this, "Api", {
  customDomain: "<http://api.domain.com|api.domain.com>",
  routes: {
    "GET /notes": "src/list.main",
  },
});
what if we could do
new Api(this, 'Api2', { customDomain: '<http://api.domain.com|api.domain.com>', …
and it would intelligently reuse the same domain. oh.. we’d have to specify a path too, of course
I mean, I haven’t tried it yet, to be fair. lemme give this a quick shot to see if it works
a
You can find example in sst doc.
Check the API doc.
There is one example about this.
Is where I copied it from.
YES! perfect
a
😄
I’m using that stuff.
s
sucks that I’m going to have to split up my API into two stacks though
or multiple stacks. this actualy might make my builds go faster too
because my single API stack is huuuuuge and change sets take forever to create
a
Yeah.
s
ok. this is not a bad thing
a
You can define dependencies too.
And some stacks can be deployed in paralell.
And others in series.
f
@Sam Hulick u don’t want to add routes to the same Api from multiple stacks?
You get around the resource limit that way as well
s
@Frank that’s possible? crap. I just spent a bunch of time splitting it up..but I hate the way this will work 😄
so wait, one stack can have the API Gateway .. how would the other stacks just contain routes but no API G?
a
You expose the GW and the other stack does
addRouters
over this exposed variable.
s
ahhh wow. I can try that, that would be much better than defining 5 different API URLs in my front end’s env
a
For sure.
Or you can use the mapping.
5 APIs + 1 main entry point with mappings 🙂
s
@Frank is this the pattern we’re talking about? https://docs.serverless-stack.com/constructs/Api#lazily-adding-routes
f
Yup!
s
well, that’s a great tip 🙂 saved me a bunch of future headaches 😅 thanks!
had no idea you could put the routes in separate stacks from the API Gateway itself
d
@Adrián Mouly how do you expose the gateway exactly? Are you exporting the variable and importing into the other app?
a
The way I do it is storing it in the class as a
readonly
parameter.
Then on the
index.ts
is referenced on the other stack.
Copy code
const taskStack = new TasksStack(app, 'tasks-stack', {
    myApi: myApiStack.api,
    eventBus: messagingStack.tasksEventBus,
  });
Like this.
Copy code
export default class MyApiStack extends Stack {
  public readonly api: Api;

  constructor(scope: App, id: string, props: MyApiStackProps) {