Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Route 53 wildcard subdomain with Api gateway

I have a Hosted zone name example.com on AWS Route53 DNS service. I have multiple subdomain record sets like:

api.example.com - type A - ALIAS xxx.cloudfront.net
www.example.com - type A - ALIAS xxx.cloudfront.net
app.example.com - type A - ALIAS xxx.cloudfront.net

All the records point to a Cloudfront generated by Api Gateway Custom Domain.

I want to offer a custom subdomain to all of my users:

USERNAME.example.com
alice.example.com
bob.example.com
...

I do not know in advance the username of all my users and there can be 1000s of them.

How can I setup Route53 to route all non defined subdomains to a specific Cloudfront => Api Gateway => AWS Lambda instance?

like image 328
Giorgio Avatar asked Jul 11 '18 17:07

Giorgio


People also ask

Can Route 53 connect to API gateway?

The method that you use to route domain traffic to an API Gateway API is the same regardless of whether you created a regional API Gateway endpoint or an edge-optimized API Gateway endpoint. Regional API endpoint: You create a Route 53 alias record that routes traffic to the regional API endpoint.

Can you have a wildcard for a subdomain?

A wildcard DNS record allows you to point all existing and non-existing subdomains to a specific area. For example, www.example.com and test.example.com would both direct to www.example.com when a wildcard subdomain is enabled. If your main domain is example.com, then the wildcard subdomain will be *.


3 Answers

From amazon route53 docs: https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/DomainNameFormat.html

Using an Asterisk (*) in the Names of Hosted Zones and Records You can create hosted zones that include * in the name. Note the following:

You can't include an * in the leftmost label in a domain name. For example, *.example.com is not allowed.

If you include * in other positions, DNS treats it as an * character (ASCII 42), not as a wildcard.

You can also create records that include * in the name. DNS treats the * character either as a wildcard or as the * character (ASCII 42), depending on where it appears in the name. Note the following restrictions on using * as a wildcard in the name of records:

The * must replace the leftmost label in a domain name, for example, .example.com. It can't replace any of the middle labels, for example, marketing..example.com.

The * must replace the entire label. For example, you can't specify prod.example.com or prod.example.com.

You can't use the * as a wildcard for records that have a type of NS.

For records, if you include * in any position other than the leftmost label in a domain name, DNS treats it as an * character (ASCII 42), not as a wildcard.

After that you can get the subdomain of the request within your API and perform a certain action for it, e.g., select a user by username using subdomain.

EDIT: Unfortunately AWS API Gateway does not support wild-card subdomain name as we can see in the first point of this link: Api Gateway Known Issues

EDIT 2: Now the AWS API Gateway has support for wild-card subdomains: API Gateway Wild Card sub domain Thanks @justin tailor

like image 118
pedropinheiro75 Avatar answered Nov 05 '22 06:11

pedropinheiro75


As mentioned, API Gateway doesn't support wildcard domains.

However, you can achieve the same thing with a load balancer (though it costs ~$17/month).

  1. Under EC2 > Load balancers, create an ALB
  2. Point it to your lambda function
  3. In Route53, set up *.yourdomain.com as an A alias to the ALB
like image 42
Petr Bela Avatar answered Nov 05 '22 05:11

Petr Bela


Wildcard custom domains are now supported by AWS API Gateway.

https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html#wildcard-custom-domain-names

like image 3
Justin Taylor Avatar answered Nov 05 '22 07:11

Justin Taylor