Search code examples
amazon-web-servicesaws-api-gatewayamazon-cloudfrontmulti-tenantamazon-route53

How to route requests to right tenant api gateway?


I am creating a multi tenant silo mode architecture to support a SAAS application. Following this link.

I am able to register new tenants and create their respective stack like this: enter image description here

So far so good, the next step is to create each tenant its own domain, for example: tenant1.admin.foo.com, to access the same CloudFront distribution (the web front end must be the same for all). I can make this by creating a record in Route53 *.admin.foo.com that has access to CloudFront

THE PROBLEM:

I need to route every request to their respective tenant stack, for example: tenant1.api.foo.com/whatever should route to the api gateway created for tenant1.

At first I thought of creating an origin in CloudFront that routes to the api gateway, the problem with this is that CloudFront origins are limited to 25.

I was thinking in creating a record in Route53 to point to their respective api gateway, but the problem is that I will have to use custom domain in the api gateway, because they are limited to 120, and I expect to have more tenants than 120.

How can I make this routing?

Here is an illustration of a use case: enter image description here

PS: Any advice is welcome.


Solution

  • You can setup a distribution with a wildcard (*.api.foo.com) set for the Alternate Domain Name (CNAMEs). If you attach a Lambda@Edge to the Origin Request (Under Cache Behavior settings), you can dynamically modify the host header to point to the appropriate API Gateway host (xxxxxx.execute-api.us-east-1.amazonaws.com).

    AWS Blog where they did this, with S3 buckets for the origin. It should translate fairly closely to APIGateway hostnames instead:

    https://aws.amazon.com/blogs/networking-and-content-delivery/dynamically-route-viewer-requests-to-any-origin-using-lambdaedge/