【问题标题】:Get dnsName and hostedZoneId from HttpApi in the same stack从同一个栈的HttpApi获取dnsName和hostedZoneId
【发布时间】:2022-01-05 18:40:15
【问题描述】:

使用 aws cdk,我正在创建一个由 lambda 函数支持的 apigatewayv2.HttpApi

我想为此创建的 API 设置一个自定义域 (apiCustomDomain) 名称,但是我似乎无法找到从创建的 API 中检索 dnsNamehostedZoneId 以创建 @ 987654325@.

    import * as apigateway2 from '@aws-cdk/aws-apigatewayv2';

    const apiCustomDomain = new apigateway2.DomainName(this, 'api-custom-domain', {
        domainName: subDomainName,
        certificate: certificate
    });

    const api = new apigateway2.HttpApi(this, 'my-api', {
        apiName: 'my-api',
        defaultDomainMapping: {
            domainName: apiCustomDomain
        },
        createDefaultStage: true,
        disableExecuteApiEndpoint: false,
        defaultAuthorizer: new HttpIamAuthorizer()
    });

    new route53.ARecord(this, 'a-api-record', {
        zone: hostedZone,
        recordName: subDomainName,
        target: route53.RecordTarget.fromAlias({
            bind(record: IRecordSet, zone?: IHostedZone): AliasRecordTargetConfig {
                return {
                     dnsName: api.?????, // what to put here 
                     hostedZoneId: api.????? // what to put here
                }
            }
        })
    });

现在在 apigateway 的 v1 中,这很简单,我们可以从 api 的 domainName 属性中获取这些值,类似于:

   dnsName = api.domainName!.domainNameAliasDomainName
   hostedZoneId = api.domainName!.domainNameAliasHostedZoneId

但我在 apigatewayv2 库中找不到相同的内容。

【问题讨论】:

    标签: amazon-web-services aws-api-gateway amazon-route53 aws-cdk


    【解决方案1】:

    将使用fromLookup 检索到的现有托管区域和ApiGatewayv2DomainProperties 目标从Route53 targets module 传递给ARecord

    const zone = route53.HostedZone.fromLookup(this, 'HostedZone', {
      domainName: domain,
    });
    
    new route53.ARecord(this, 'AliasRecord', {
      recordName: subdomain,
      zone,
      target: route53.RecordTarget.fromAlias(
        new targets.ApiGatewayv2DomainProperties(
          apiCustomDomain.regionalDomainName,
          apiCustomDomain.regionalHostedZoneId
        )
      ),
    });
    

    【讨论】:

    • 谢谢!它可以工作,但我对它如何将自定义域实际映射到 api 感到有点困惑。 1-是因为HttpApi中的defaultDomainMapping吗? 2- 还有为什么 API 的域名与其 Invoke url 不同?
    • 很高兴为您提供帮助。是的,根据我在docs 中看到的内容,我相信您是正确的。
    • 明白,谢谢。
    猜你喜欢
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多