Wildcard API Gateway domain using $context.domainPrefix to dynamically call Lambdas by name
Considering the following scenario where I have multiple customers and each one has it's own sets of lambdas and a custom subdomain. Let's say.
client1.domain -> must call http-client1-lambda
client2.domain -> must call http-client2-lambda
I have reached a limit on the number of custom domains I can create and I want to have now a single one using wildcard and make it dynamically call the respective client lambda using $context.domainPrefix.
I could not find a way to do that.
I don't use any API Gateway specific feature, it can be that ALB or ELB could solve this issue for me, I just use API Gateway because the serverless framework deploys that for me.
With Application load balancer, you can make use host based routing. (https://aws.amazon.com/premiumsupport/knowledge-center/elb-configure-host-based-routing-alb/ )
That way:-
``
client1.domain -> ALB -----(Rule-1)------> Target group-1 that hosts client1.domain.
client2.domain -> ALB -----(Rule-2)------> Target group-2 that hosts client2.domain.
``
You can have lambda function as as a target, instead of re-writing everything on a EC2 again.
https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html
Haven't tried this myself, but I am thinking you may be able to achieve this using API Gateway and Step Functions, based on this capability announcement - https://aws.amazon.com/about-aws/whats-new/2021/05/amazon-api-gateway-rest-apis-integrates-with-step-funtions-synchronous-express-workflows/#:~:text=You%20can%20now%20create%20Amazon,a%20workflow%20of%20different%20microservices.
From the step function, you can branch out to the different lambdas depending on the client ID that you capture in your API Gateway and pass-on to the Step Function. Worth doing a POC.
If your processing can be asynchronous, then you can receive one primary lambda that receives the request from API Gateway, parses the Client ID and based on that drops the message in a different SQS queue for each Client. You can then have secondary lambdas for each client that get triggered by their respective SQS queue messages and do the processing. If you want to not hard-code the SQS queues in your primary lambda, you can create a look-up table in DynamoDB. That way new clients can be added without having to modify the primary lambda code and redeploy every time a new client is added.
I would need to work with http requests, so I think this approach will add latency to the requests
Relevant questions
S3 file drop fire multiple Lambdas
Accepted Answerasked 4 years agoHow can we create thousands of Lambdas, beyond quota limits?
asked 3 months agoWildcard API Gateway domain using $context.domainPrefix to dynamically call Lambdas by name
asked a month agoVPC Interface Endpoints and API Gateway called from Lambdas
Accepted Answerasked 7 months agoWhen to invoke a lambda directly or via API Gateway
asked 6 months agoHow to use multiple Lambdas to a single S3 used by amazon connect
asked 6 months agoHow to deploy 4 lambdas in typescript using a code pipeline ?
asked 4 months agoUsing Lambdas with Cognito
asked 2 months agoCreating a custom domain name for a stage in API Gateway and attaching the cert
Accepted Answerasked 4 years agoGhost Lambdas responding to IoT Cloud subscriptions
asked 3 years ago
that could be an option, I'll try it, but I'm afraid we will reach som Target Group limits soon