By using AWS re:Post, you agree to the Terms of Use
/Wildcard API Gateway domain using $context.domainPrefix to dynamically call Lambdas by name/

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.

2 Answers

With Application load balancer, you can make use host based routing. ( )

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.

answered a month ago
  • that could be an option, I'll try it, but I'm afraid we will reach som Target Group limits soon


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 -,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.

answered a month ago
  • I would need to work with http requests, so I think this approach will add latency to the requests

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions