For those of you working with the API and a bunch ...
# general
r
For those of you working with the API and a bunch of endpoints. How do you handle this? Actually I have one
API
and I just
addRoutes
to it whenever I need to, but I see my resources going up pretty fast in the CloudFormation stack and I know that I'll eventually hit the 500 resources limit. Do you guys have more than one API? And if so, do you just use the same
customDomain
with different
path
?
a
I separate my APIs out in categories - somewhat like microservices - and each is a stack. You can create the API in one stack (with no routes) and pass it to the others if you want them all on one domain. Usually, I don't care what the URL is and just have a separate API for each stack and keep whatever domain name AWS assigns it - if your API is public, then you probably want the custom domain and to share it.
r
So every stack that you have has it's own apiGateway. This way you won't have the resources problem. And I guess that the build/deploy time is faster that way since it will only rebuild one of the API if needed instead of the big CloudFormation stack
t
I think what adam is saying he has a single api stack but he adds routes to it from other stacks
you can share the api to another stack
r
Yeah I do that too
Problem is that my resources count is getting high
And CloudFormation has a 500 hard limit
t
ah sorry I misread both of your messages!
I tend to have really small granular stacks, it helps in a few places
r
No worry, you had a big day 🙂 Good job btw!
I am trying to find the best way to handle this, since I do not want to get to the 500 limit. But since we do have a mono repo, not too sure how to handle all the routes. I might go with multiple apiGateway, and have them all share the same custom domain, just wondering if there's a better way
a
I do have one API Gateway per stack, but that's because I don't use custom domains. If I were dealing with a public API and custom domain, then I would have just one as described. Another option is subdomains for each stack/API, but that isn't usually what you want to present to your users.
r
Yeah, which is the problem I have hehe. Since every endpoint takes up a lot of resources the count goes up pretty fast. Wish I could just have a single URL, but the max will keep me from doing it I'm affraid
a
If you define the API in one stack, and then attach routes in other stacks, I think you are good. All those resources will be distributed to the child stacks, not the one with the API definition. (I believe? Never done it.)
r
This is what I do, I have a stack that create the API then all my other stack juse
use()
it and addRoutes on it. Right now I have 46 routes, and I am up to 264 resources in the CloudFormation stack 😕
t
you could group related endpoints like CRUD actions for an entity together in one lambda
using something like lambda-api would still be more lightweight and granular than a monolithic server framework and avoid creating multiple resources for every individual endpoint
d
We do a setup with an API Gateway in 1 stack, and then add routes to it from other stacks.
It works for us, however I read this thread hoping to see optimizations
For us, all our stacks and endpoints are causing very long deployments
f
@Robert, when u create the
Api
in stackA, and in stackB you
use
it and call
addRoutes
, the resources for the routes are created in the stackB’s CloudFormation template.
Let me know if that’s not what you are seeing.
r
@Frank I have a stack that create my
apiGateway
with default stuff, like timeout, cors, some permissions, etc. Then in every other stack, let's say my Users stack, I add routes to the apiGateway. Something like
Copy code
apiGateway.addRoutes(ctx.stack, {
    'GET  /users': {
      function: {
        functionName: `${id}-${ctx.app.stage}-get-users`,
        handler: 'handlers/user.get',
      },
    },
  });
When I do this it creates a stack in CloudFormation for the
apiGateway
, and a stack for the
users
. But in both of them resources are added. After that, in every stack where I use my apiGateway to addRoutes in it, it will create a new CloudFormation stack, but it will also add resources to the apiGateway's CloudFormation stack. Only thing I can think of, could it be because I am using the
ApiGatewayV1Api
object?