Mic*_*xon 6 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'
})
}
});
Run Code Online (Sandbox Code Playgroud)
可以构建,npm run build但是当我运行时,cdk synth我得到了相当钝的错误:
$ cdk synth
HostedZone.fromHostedZoneId doesn't support "zoneName"
Subprocess exited with error 1
Run Code Online (Sandbox Code Playgroud)
开机--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)
Run Code Online (Sandbox Code Playgroud)
我浏览了整个堆栈脚本,没有zoneName任何地方引用。有人知道此错误来自何处吗?
我在使用 cdk 1.36.1 时遇到了同样的问题,并设法在 ApiGateway 中使用新的自定义域名以及 BasePath 映射来解决它,然后在 route53 上的现有托管区域中添加一个 CName 记录,指向新的自定义域:
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
});
Run Code Online (Sandbox Code Playgroud)
使用aws-cdkv1 应该能够做到以下几点:
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.RecordTarget.fromAlias({
bind() {
return {
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.
};
},
}),
});
Run Code Online (Sandbox Code Playgroud)
如果您的 API 在同一个堆栈/代码库中,您可以从中获取dnsName和hostedZoneId(它是一个 CF 属性)。
否则是指DNSName与HostedZoneId在AWS :: Route53 :: RecordSet的AliasTarget文档。
注:在hostedZoneId你的别名记录是不一样的自己带的托管区域ID。
在 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))
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
728 次 |
| 最近记录: |