使用CDK为HTTP API配置Route53自定义子域名报错及步骤确认
问题解决与步骤确认
错误修复:将HttpApi作为Route53 A记录目标
routeTargets.ApiGateway仅适配API Gateway REST API,无法直接用于HttpApi(API Gateway V2)。正确的做法是利用你创建的DomainName对象的区域属性构造别名目标:
方案1:手动构造别名配置
new ARecord(this, 'ApiARecord', { zone: myZone, recordName: apiDomainName, target: RecordTarget.fromAlias({ bind: () => ({ dnsName: domain.regionalDomainName, hostedZoneId: domain.regionalHostedZoneId, }), }), });
方案2:使用专用目标类(CDK新版本支持)
若你的CDK版本足够新,aws-route53-targets包提供了ApiGatewayV2Domain类,可直接传入DomainName实例:
import * as routeTargets from "aws-cdk-lib/aws-route53-targets"; new ARecord(this, 'ApiARecord', { zone: myZone, recordName: apiDomainName, target: RecordTarget.fromAlias(new routeTargets.ApiGatewayV2Domain(domain)), });
自定义子域名配置步骤确认
你的核心思路正确,但需明确步骤细节:
- 步骤1:绑定HttpApi与自定义域名
包含两个子操作:- 创建
DomainName实例,关联api.example.com域名和对应证书(注意:证书需覆盖api.example.com,若原证书是example.com,需确保包含通配符*.example.com) - 创建
ApiMapping实例,将HttpApi与DomainName绑定,并指定映射的API阶段
- 创建
- 步骤2:创建Route53记录
创建A/AAAA记录,将api.example.com指向DomainName的区域端点(而非直接指向HttpApi)
内容的提问来源于stack exchange,提问作者ReactNewbie123
相关产品推荐
相关产品推荐

