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