UNPKG

6.96 kBMarkdownView Raw
1# Amazon Route53 Construct Library
2<!--BEGIN STABILITY BANNER-->
3
4---
5
6![cfn-resources: Stable](https://img.shields.io/badge/cfn--resources-stable-success.svg?style=for-the-badge)
7
8![cdk-constructs: Stable](https://img.shields.io/badge/cdk--constructs-stable-success.svg?style=for-the-badge)
9
10---
11
12<!--END STABILITY BANNER-->
13
14To add a public hosted zone:
15
16```ts
17import * as route53 from '@aws-cdk/aws-route53';
18
19new route53.PublicHostedZone(this, 'HostedZone', {
20 zoneName: 'fully.qualified.domain.com'
21});
22```
23
24To add a private hosted zone, use `PrivateHostedZone`. Note that
25`enableDnsHostnames` and `enableDnsSupport` must have been enabled for the
26VPC you're configuring for private hosted zones.
27
28```ts
29import * as ec2 from '@aws-cdk/aws-ec2';
30import * as route53 from '@aws-cdk/aws-route53';
31
32const vpc = new ec2.Vpc(this, 'VPC');
33
34const zone = new route53.PrivateHostedZone(this, 'HostedZone', {
35 zoneName: 'fully.qualified.domain.com',
36 vpc // At least one VPC has to be added to a Private Hosted Zone.
37});
38```
39
40Additional VPCs can be added with `zone.addVpc()`.
41
42## Adding Records
43
44To add a TXT record to your zone:
45
46```ts
47import * as route53 from '@aws-cdk/aws-route53';
48
49new route53.TxtRecord(this, 'TXTRecord', {
50 zone: myZone,
51 recordName: '_foo', // If the name ends with a ".", it will be used as-is;
52 // if it ends with a "." followed by the zone name, a trailing "." will be added automatically;
53 // otherwise, a ".", the zone name, and a trailing "." will be added automatically.
54 // Defaults to zone root if not specified.
55 values: [ // Will be quoted for you, and " will be escaped automatically.
56 'Bar!',
57 'Baz?'
58 ],
59 ttl: Duration.minutes(90), // Optional - default is 30 minutes
60});
61```
62
63To add an A record to your zone:
64
65```ts
66import * as route53 from '@aws-cdk/aws-route53';
67
68new route53.ARecord(this, 'ARecord', {
69 zone: myZone,
70 target: route53.RecordTarget.fromIpAddresses('1.2.3.4', '5.6.7.8')
71});
72```
73
74To add an A record for an EC2 instance with an Elastic IP (EIP) to your zone:
75
76```ts
77import * as ec2 from '@aws-cdk/aws-ec2';
78import * as route53 from '@aws-cdk/aws-route53';
79
80const instance = new ec2.Instance(this, 'Instance', {
81 // ...
82});
83
84const elasticIp = new ec2.CfnEIP(this, 'EIP', {
85 domain: 'vpc',
86 instanceId: instance.instanceId
87});
88
89new route53.ARecord(this, 'ARecord', {
90 zone: myZone,
91 target: route53.RecordTarget.fromIpAddresses(elasticIp.ref)
92});
93```
94
95To add an AAAA record pointing to a CloudFront distribution:
96
97```ts
98import * as route53 from '@aws-cdk/aws-route53';
99import * as targets from '@aws-cdk/aws-route53-targets';
100
101new route53.AaaaRecord(this, 'Alias', {
102 zone: myZone,
103 target: route53.RecordTarget.fromAlias(new targets.CloudFrontTarget(distribution))
104});
105```
106
107Constructs are available for A, AAAA, CAA, CNAME, MX, NS, SRV and TXT records.
108
109Use the `CaaAmazonRecord` construct to easily restrict certificate authorities
110allowed to issue certificates for a domain to Amazon only.
111
112To add a NS record to a HostedZone in different account
113
114```ts
115import * as route53 from '@aws-cdk/aws-route53';
116
117// In the account containing the HostedZone
118const parentZone = new route53.PublicHostedZone(this, 'HostedZone', {
119 zoneName: 'someexample.com',
120 crossAccountZoneDelegationPrincipal: new iam.AccountPrincipal('12345678901')
121});
122
123// In this account
124const subZone = new route53.PublicHostedZone(this, 'SubZone', {
125 zoneName: 'sub.someexample.com'
126});
127
128new route53.CrossAccountZoneDelegationRecord(this, 'delegate', {
129 delegatedZone: subZone,
130 parentHostedZoneId: parentZone.hostedZoneId,
131 delegationRole: parentZone.crossAccountDelegationRole
132});
133```
134
135## Imports
136
137If you don't know the ID of the Hosted Zone to import, you can use the
138`HostedZone.fromLookup`:
139
140```ts
141HostedZone.fromLookup(this, 'MyZone', {
142 domainName: 'example.com'
143});
144```
145
146`HostedZone.fromLookup` requires an environment to be configured. Check
147out the [documentation](https://docs.aws.amazon.com/cdk/latest/guide/environments.html) for more documentation and examples. CDK
148automatically looks into your `~/.aws/config` file for the `[default]` profile.
149If you want to specify a different account run `cdk deploy --profile [profile]`.
150
151```ts
152new MyDevStack(app, 'dev', {
153 env: {
154 account: process.env.CDK_DEFAULT_ACCOUNT,
155 region: process.env.CDK_DEFAULT_REGION
156}});
157```
158
159If you know the ID and Name of a Hosted Zone, you can import it directly:
160
161```ts
162const zone = HostedZone.fromHostedZoneAttributes(this, 'MyZone', {
163 zoneName: 'example.com',
164 hostedZoneId: 'ZOJJZC49E0EPZ',
165});
166```
167
168Alternatively, use the `HostedZone.fromHostedZoneId` to import hosted zones if
169you know the ID and the retrieval for the `zoneName` is undesirable.
170
171```ts
172const zone = HostedZone.fromHostedZoneId(this, 'MyZone', 'ZOJJZC49E0EPZ');
173```
174
175## VPC Endpoint Service Private DNS
176
177When you create a VPC endpoint service, AWS generates endpoint-specific DNS hostnames that consumers use to communicate with the service.
178For example, vpce-1234-abcdev-us-east-1.vpce-svc-123345.us-east-1.vpce.amazonaws.com.
179By default, your consumers access the service with that DNS name.
180This can cause problems with HTTPS traffic because the DNS will not match the backend certificate:
181
182```console
183curl: (60) SSL: no alternative certificate subject name matches target host name 'vpce-abcdefghijklmnopq-rstuvwx.vpce-svc-abcdefghijklmnopq.us-east-1.vpce.amazonaws.com'
184```
185
186Effectively, the endpoint appears untrustworthy. To mitigate this, clients have to create an alias for this DNS name in Route53.
187
188Private DNS for an endpoint service lets you configure a private DNS name so consumers can
189access the service using an existing DNS name without creating this Route53 DNS alias
190This DNS name can also be guaranteed to match up with the backend certificate.
191
192Before consumers can use the private DNS name, you must verify that you have control of the domain/subdomain.
193
194Assuming your account has ownership of the particlar domain/subdomain,
195this construct sets up the private DNS configuration on the endpoint service,
196creates all the necessary Route53 entries, and verifies domain ownership.
197
198```ts
199import { Stack } from '@aws-cdk/core';
200import { Vpc, VpcEndpointService } from '@aws-cdk/aws-ec2';
201import { NetworkLoadBalancer } from '@aws-cdk/aws-elasticloadbalancingv2';
202import { PublicHostedZone } from '@aws-cdk/aws-route53';
203
204stack = new Stack();
205vpc = new Vpc(stack, 'VPC');
206nlb = new NetworkLoadBalancer(stack, 'NLB', {
207 vpc,
208});
209vpces = new VpcEndpointService(stack, 'VPCES', {
210 vpcEndpointServiceLoadBalancers: [nlb],
211});
212// You must use a public hosted zone so domain ownership can be verified
213zone = new PublicHostedZone(stack, 'PHZ', {
214 zoneName: 'aws-cdk.dev',
215});
216new VpcEndpointServiceDomainName(stack, 'EndpointDomain', {
217 endpointService: vpces,
218 domainName: 'my-stuff.aws-cdk.dev',
219 publicHostedZone: zone,
220});
221```