亚马逊路53 建构库
AWS CDK v1在2023-06-01上达到了支持结束. 此软件包不再更新,用户应该迁移到 AWS CDK v2.
关于如何移徙的更多信息,请参见: 移动到 AWS CDK v2 指南。 。 。 .
添加公共托管区域:
new route53.PublicHostedZone(this, 'HostedZone', {
zoneName: 'fully.qualified.domain.com',
});
要添加私有主机区, 请使用 PrivateHostedZone。注意:
enableDnsHostnames 财务报告和审定财务报表 enableDnsSupport 必须是已启用
VPC你正在配置 私人托管区.
declare const vpc: ec2.Vpc;
const zone = new route53.PrivateHostedZone(this, 'HostedZone', {
zoneName: 'fully.qualified.domain.com',
vpc, // At least one VPC has to be added to a Private Hosted Zone.
});
可添加额外的 VPC zone.addVpc()。 。 。 .
添加记录
要在您的区域中添加 TXT 记录 :
declare const myZone: route53.HostedZone;
new route53.TxtRecord(this, 'TXTRecord', {
zone: myZone,
recordName: '_foo', // If the name ends with a ".", it will be used as-is;
// if it ends with a "." followed by the zone name, a trailing "." will be added automatically;
// otherwise, a ".", the zone name, and a trailing "." will be added automatically.
// Defaults to zone root if not specified.
values: [ // Will be quoted for you, and " will be escaped automatically.
'Bar!',
'Baz?',
],
ttl: Duration.minutes(90), // Optional - default is 30 minutes
});
在您的区域中添加NS记录:
declare const myZone: route53.HostedZone;
new route53.NsRecord(this, 'NSRecord', {
zone: myZone,
recordName: 'foo',
values: [
'ns-1.awsdns.co.uk.',
'ns-2.awsdns.com.',
],
ttl: Duration.minutes(90), // Optional - default is 30 minutes
});
在您的区域中添加 DS 记录:
declare const myZone: route53.HostedZone;
new route53.DsRecord(this, 'DSRecord', {
zone: myZone,
recordName: 'foo',
values: [
'12345 3 1 123456789abcdef67890123456789abcdef67890',
],
ttl: Duration.minutes(90), // Optional - default is 30 minutes
});
在您的区域中添加一个记录:
declare const myZone: route53.HostedZone;
new route53.ARecord(this, 'ARecord', {
zone: myZone,
target: route53.RecordTarget.fromIpAddresses('1.2.3.4', '5.6.7.8'),
});
要在您的区域中添加一个带有弹性IP(EIP)的EC2实例的记录:
declare const instance: ec2.Instance;
const elasticIp = new ec2.CfnEIP(this, 'EIP', {
domain: 'vpc',
instanceId: instance.instanceId,
});
declare const myZone: route53.HostedZone;
new route53.ARecord(this, 'ARecord', {
zone: myZone,
target: route53.RecordTarget.fromIpAddresses(elasticIp.ref),
});
要添加一个 AAAA 记录指向 CloudFront 分布:
import * as cloudfront from '@aws-cdk/aws-cloudfront';
declare const myZone: route53.HostedZone;
declare const distribution: cloudfront.CloudFrontWebDistribution;
new route53.AaaaRecord(this, 'Alias', {
zone: myZone,
target: route53.RecordTarget.fromAlias(new targets.CloudFrontTarget(distribution)),
});
A, AAAA, CAA, CNAME, MX, NS, SRV和TXT等记录的构造都有.
使用 CaaAmazonRecord 易于限制证书当局的构造
仅允许向亚马逊发布一个域的证书.
要将NS记录添加到不同账户的HostedZone中,您可以做到如下:
在包含主机区的账户中:
const parentZone = new route53.PublicHostedZone(this, 'HostedZone', {
zoneName: 'someexample.com',
crossAccountZoneDelegationPrincipal: new iam.AccountPrincipal('12345678901'),
crossAccountZoneDelegationRoleName: 'MyDelegationRole',
});
在包含待授权的儿童区的账户中:
const subZone = new route53.PublicHostedZone(this, 'SubZone', {
zoneName: 'sub.someexample.com',
});
// import the delegation role by constructing the roleArn
const delegationRoleArn = Stack.of(this).formatArn({
region: '', // IAM is global in each partition
service: 'iam',
account: 'parent-account-id',
resource: 'role',
resourceName: 'MyDelegationRole',
});
const delegationRole = iam.Role.fromRoleArn(this, 'DelegationRole', delegationRoleArn);
// create the record
new route53.CrossAccountZoneDelegationRecord(this, 'delegate', {
delegatedZone: subZone,
parentHostedZoneName: 'someexample.com', // or you can use parentHostedZoneId
delegationRole,
});
进口
如果你不知道要导入的主控区的ID, 你可以使用
HostedZone.fromLookup数字 :
route53.HostedZone.fromLookup(this, 'MyZone', {
domainName: 'example.com',
});
HostedZone.fromLookup 需要配置环境。 检查
离开 文档 更多的文件和实例。 光盘
自动查看您的 ~/.aws/config 文档 [default] 简介.
如果您想要指定不同的账户运行 cdk deploy --profile [profile]。 。 。 .
new MyDevStack(app, 'dev', {
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
},
});
如果您知道主控区的ID和名称,可以直接导入:
const zone = route53.HostedZone.fromHostedZoneAttributes(this, 'MyZone', {
zoneName: 'example.com',
hostedZoneId: 'ZOJJZC49E0EPZ',
});
或者,使用 HostedZone.fromHostedZoneId 以导入主机区,如果
你知道身份和检索 zoneName 这是不受欢迎的.
const zone = route53.HostedZone.fromHostedZoneId(this, 'MyZone', 'ZOJJZC49E0EPZ');
您可以导入一个公共主机区以及类似的 PubicHostedZone.fromPublicHostedZoneId 财务报告和审定财务报表 PubicHostedZone.fromPublicHostedZoneAttributes 方法:
const zoneFromAttributes = route53.PublicHostedZone.fromPublicHostedZoneAttributes(this, 'MyZone', {
zoneName: 'example.com',
hostedZoneId: 'ZOJJZC49E0EPZ',
});
// Does not know zoneName
const zoneFromId = route53.PublicHostedZone.fromPublicHostedZoneId(this, 'MyZone', 'ZOJJZC49E0EPZ');
VPC 端点服务 私人 DNS
当创建了VPC端点服务时,AWS会生成消费者用来与服务通信的端点专用DNS主机名. 例如:vpce-1234-abcdev-us-east-1.vpce-svc-123345.us-east-1.vpce.amazonaws.com. 中国植物物种信息数据库. 默认情况下,您的消费者会使用DNS名称访问服务. 这会造成HTTPS流量出问题,因为DNS不会匹配后端证书:
curl: (60) SSL: no alternative certificate subject name matches target host name 'vpce-abcdefghijklmnopq-rstuvwx.vpce-svc-abcdefghijklmnopq.us-east-1.vpce.amazonaws.com'
实际上,终点似乎不可信。 为了缓解这种情况,客户端必须在"路由53"中为这个DNS名称创建别名.
端点服务的私人 DNS 允许您配置一个私人 DNS 名称以便消费者可以 使用现有的 DNS 名称访问服务而不创建此 Route53 DNS 化名 此 DNS 名称也可以保证与后端证书相匹配.
在消费者可以使用私用 DNS 名称之前,您必须验证您是否控制了域/子域.
假设您的账户对特定域/子域拥有所有权, 这个构造设置了端点服务的私人 DNS 配置, 创建所有必要的 Route53 条目,并验证域名所有权.
import { Stack } from '@aws-cdk/core';
import { Vpc, VpcEndpointService } from '@aws-cdk/aws-ec2';
import { NetworkLoadBalancer } from '@aws-cdk/aws-elasticloadbalancingv2';
import { PublicHostedZone, VpcEndpointServiceDomainName } from '@aws-cdk/aws-route53';
const stack = new Stack();
const vpc = new Vpc(stack, 'VPC');
const nlb = new NetworkLoadBalancer(stack, 'NLB', {
vpc,
});
const vpces = new VpcEndpointService(stack, 'VPCES', {
vpcEndpointServiceLoadBalancers: [nlb],
});
// You must use a public hosted zone so domain ownership can be verified
const zone = new PublicHostedZone(stack, 'PHZ', {
zoneName: 'aws-cdk.dev',
});
new VpcEndpointServiceDomainName(stack, 'EndpointDomain', {
endpointService: vpces,
domainName: 'my-stuff.aws-cdk.dev',
publicHostedZone: zone,
});