具有 Api 网关的 AWS Route 53 通配符子域

AWS Route 53 wildcard subdomain with Api gateway

我在 AWS Route53 DNS 服务上有一个托管区域名称 example.com。 我有多个子域记录集,例如:

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

所有记录都指向由 Api 网关自定义域生成的 Cloudfront。

我想为我的所有用户提供自定义子域:

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

我事先不知道我所有用户的用户名,可能有 1000 个。

我如何设置 Route53 以将所有未定义的子域路由到特定的 Cloudfront => Api 网关 => AWS Lambda 实例?

来自亚马逊 route53 文档: 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.

之后,您可以在 API 中获取请求的子域并为其执行特定操作,例如,select 使用子域的用户名

编辑: 不幸的是,AWS API 网关不支持通配符子域名,正如我们在 link 的第一点中看到的那样: Api Gateway Known Issues

编辑 2: 现在 AWS API 网关支持通配子域: API Gateway Wild Card sub domain 谢谢@justin 裁缝

如前所述,API 网关不支持通配符域。

但是,您可以使用负载均衡器实现相同的目的(尽管每月花费约 17 美元)。

  1. 在 EC2 > 负载均衡器下,创建一个 ALB
  2. 将其指向您的 lambda 函数
  3. 在 Route53 中,将 *.yourdomain.com 设置为 ALB
  4. 的 A 别名

AWS API 网关现在支持通配符自定义域。

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