AWS CDK:如何从Route53定位API网关API

时间:2019-06-12 22:32:47

标签: amazon-web-services aws-cdk

我有一个在AWS Route53中注册的现有域名,并且已经在API Gateway中设置了自定义域名。在控制台中,我可以进行配置,例如从外部进行xxxxxx.zenxxxxxxfoundry.com,实际上可以到达API网关API,然后一直到我的Lambda函数。

现在我想通过AWS CDK实现这一目标。

我尝试了以下方法:

    const zone = route53.HostedZone.fromHostedZoneId(this, 'ZenithWebFoundryZone', 'ZXXXXXX04V8134');
    new route53.AliasRecord(this, 'BlogAPIRecord', {
      zone: zone,
      recordName: 'xxxxxx.zenxxxxxxfoundry.com',
      target: {
        bind: (): route53.AliasRecordTargetProps => ({
          dnsName: 'd-xxxxxxy00g.execute-api.ap-southeast-2.amazonaws.com',
          hostedZoneId: 'ZXXXXXX04V8134'
        })
      }
    });

可以正常构建npm run build,但是当我运行cdk synth时,我得到了一个相当钝的错误:

$ cdk synth
HostedZone.fromHostedZoneId doesn't support "zoneName"
Subprocess exited with error 1

打开--trace并没有太大帮助:附加信息:

Error: Subprocess exited with error 1
    at ChildProcess.proc.on.code (/Users/mikecoxon/.npm-packages/lib/node_modules/aws-cdk/lib/api/cxapp/exec.ts:108:23)
    at ChildProcess.emit (events.js:189:13)
    at ChildProcess.EventEmitter.emit (domain.js:441:20)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)

我已经浏览了整个堆栈脚本,并且在任何地方都没有引用zoneName。有人知道此错误来自何处吗?

5 个答案:

答案 0 :(得分:6)

我知道这是一个老问题,但是我使用cdk 1.36.1遇到了同样的问题,并设法在ApiGateway中使用新的自定义域名以及BasePath映射解决了此问题,然后在现有托管主机中添加了CName记录指向新的自定义域的route53上的区域:

import {BasePathMapping, DomainName, EndpointType, LambdaRestApi} from '@aws-cdk/aws-apigateway';
import {Certificate} from '@aws-cdk/aws-certificatemanager';
import {HostedZone, CnameRecord} from '@aws-cdk/aws-route53'

// First create a custom domain:
const customDomain = new DomainName(this, 'customDomain', {
      domainName: 'api.xxxxxxx.com',
      certificate: Certificate.fromCertificateArn(this, 'ACM_Certificate', ACM_CERTIFICATE_ARN),
      endpointType: EndpointType.EDGE
});

// create a new ApiGateway instance and associate a lambda function with it:
const api = new LambdaRestApi(this, 'MainGatewayEndpoint', {
      handler: toyFunction,
});

// Associate the Custom domain that we created with new APIGateway using BasePathMapping:
new BasePathMapping(this, 'CustomBasePathMapping', {
      domainName: custom,
      restApi: api
});

// Get a reference to AN EXISTING hosted zone using the HOSTED_ZONE_ID. You can get this from route53
const hostedZone = HostedZone.fromHostedZoneAttributes(this, 'HostedZone', {
      hostedZoneId: PROD_HOSTED_ZONE_ID,
      zoneName: 'xxxxxxx.com'
});

// Finally, add a CName record in the hosted zone with a value of the new custom domain that was created above:
new CnameRecord(this, 'ApiGatewayRecordSet', {
      zone: hostedZone,
      recordName: 'api',
      domainName: customDomain.domainNameAliasDomainName
});


答案 1 :(得分:1)

使用aws-cdk v0.34.0,应该能够执行以下操作:

const zone = route53.HostedZone.fromHostedZoneAttributes(this, 'ZenithWebFoundryZone', {
  hostedZoneId: 'ZXXXXXX04V8134',
  zoneName: 'zenxxxxxxfoundry.com' // your zone name here
});
new route53.ARecord(this, 'BlogAPIRecord', {
  zone,
  recordName: 'xxxxxx.zenxxxxxxfoundry.com',
  target: route53.AddressRecordTarget.fromAlias({
    bind: (): route53.AliasRecordTargetConfig => ({
      dnsName: 'd-xxxxxxy00g.execute-api.ap-southeast-2.amazonaws.com', // Specify the applicable domain name for your API.
      hostedZoneId: 'XXXX', // Specify the hosted zone ID for your API.
    }),
  }),
});

如果您的API位于相同的堆栈/代码库中,则可以从中获取dnsNamehostedZoneId(这是CF属性)。

否则请参考DNSName中的HostedZoneIdAWS::Route53::RecordSet AliasTarget documentation

注意:别名记录的hostedZoneId与您自己区域的托管区域ID 不同

答案 2 :(得分:0)

我在以下方面遇到了相同的错误:

const zone = route53.HostedZone.fromHostedZoneId(this, 'MyZone', 'ZXXXXXXXXXXXXX');

我在Route53中有一个与以下区域类似的区域:

Domain Name:    example.com.
Type:           Public Hosted Zone
Hosted Zone ID: ZXXXXXXXXXXXXX

我更改为以下内容,并且它起作用了(CDK 0.34.0):

const zone = new route53.HostedZoneProvider(this,  {
  domainName: 'example.com',
}).findAndImport(this, 'MyZone');

答案 3 :(得分:0)

在AWS CDK 0.36.1中,您可以使用@ aws-cdk / aws-route53-targets包来创建别名。

import { HostedZone, RecordSet, RecordType, RecordTarget } from '@aws-cdk/aws-route53'
import { ApiGatewayDomain } from '@aws-cdk/aws-route53-targets'
import { Certificate } from '@aws-cdk/aws-certificatemanager'

// ...

  const customDomain = new apigateway.DomainName(this, 'CustomDomain', {
    domainName: props.apiDomain,
    certificate: Certificate.fromCertificateArn(this, 'Certificate', props.certificateArn),
    endpointType: apigateway.EndpointType.EDGE,
  })

  const hostedZone = HostedZone.fromHostedZoneAttributes(this, 'HostedZone', {
    hostedZoneId: props.hostedZoneId,
    zoneName: props.hostedZoneName,
  })

  new RecordSet(this, 'ApiRecordSetA', {
    zone: hostedZone,
    recordType: RecordType.A,
    recordName: 'api',
    target: RecordTarget.fromAlias(new ApiGatewayDomain(customDomain))
  })

  new RecordSet(this, 'ApiRecordSetAAAA', {
    zone: hostedZone,
    recordType: RecordType.AAAA,
    recordName: 'api',
    target: RecordTarget.fromAlias(new ApiGatewayDomain(customDomain))
  })

答案 4 :(得分:0)

我设法使它正常工作,但是被托管区域名称所吸引,要求使用“。”。在它的结尾。

这是针对现有的手动创建的托管区域,需要进行查找才能通过CDK添加别名记录。

const hostedZone = route53.HostedZone.fromHostedZoneAttributes(this, 'HostedZone', {
  hostedZoneId: config.apiHostedZoneId,
  name: `${config.apiDomainName}.`,
});

new route53.RecordSet(this, 'ApiRecordSetA', {
  zone: hostedZone,
  recordType: route53.RecordType.A,
  recordName: config.apiDomainName,
  target: route53.RecordTarget.fromAlias(new route53Targets.ApiGatewayDomain(apiDomain))
});

new route53.RecordSet(this, 'ApiRecordSetAAAA', {
  zone: hostedZone,
  recordType: route53.RecordType.AAAA,
  recordName: config.apiDomainName,
  target: route53.RecordTarget.fromAlias(new route53Targets.ApiGatewayDomain(apiDomain))
});