/
record-set.ts
540 lines (482 loc) · 12.6 KB
/
record-set.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import { Construct, Duration, IResource, Resource, Token } from '@aws-cdk/core';
import { IAliasRecordTarget } from './alias-record-target';
import { IHostedZone } from './hosted-zone-ref';
import { CfnRecordSet } from './route53.generated';
import { determineFullyQualifiedDomainName } from './util';
/**
* A record set
*/
export interface IRecordSet extends IResource {
/**
* The domain name of the record
*/
readonly domainName: string;
}
/**
* The record type.
*/
export enum RecordType {
/**
* route traffic to a resource, such as a web server, using an IPv4 address in dotted decimal
* notation
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#AFormat
*/
A = 'A',
/**
* route traffic to a resource, such as a web server, using an IPv6 address in colon-separated
* hexadecimal format
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#AAAAFormat
*/
AAAA = 'AAAA',
/**
* A CAA record specifies which certificate authorities (CAs) are allowed to issue certificates
* for a domain or subdomain
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#CAAFormat
*/
CAA = 'CAA',
/**
* A CNAME record maps DNS queries for the name of the current record, such as acme.example.com,
* to another domain (example.com or example.net) or subdomain (acme.example.com or zenith.example.org).
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#CNAMEFormat
*/
CNAME = 'CNAME',
/**
* An MX record specifies the names of your mail servers and, if you have two or more mail servers,
* the priority order.
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#MXFormat
*/
MX = 'MX',
/**
* A Name Authority Pointer (NAPTR) is a type of record that is used by Dynamic Delegation Discovery
* System (DDDS) applications to convert one value to another or to replace one value with another.
* For example, one common use is to convert phone numbers into SIP URIs.
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#NAPTRFormat
*/
NAPTR = 'NAPTR',
/**
* An NS record identifies the name servers for the hosted zone
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#NSFormat
*/
NS = 'NS',
/**
* A PTR record maps an IP address to the corresponding domain name.
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#PTRFormat
*/
PTR = 'PTR',
/**
* A start of authority (SOA) record provides information about a domain and the corresponding Amazon
* Route 53 hosted zone
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#SOAFormat
*/
SOA = 'SOA',
/**
* SPF records were formerly used to verify the identity of the sender of email messages.
* Instead of an SPF record, we recommend that you create a TXT record that contains the applicable value.
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#SPFFormat
*/
SPF = 'SPF',
/**
* An SRV record Value element consists of four space-separated values. The first three values are
* decimal numbers representing priority, weight, and port. The fourth value is a domain name.
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#SRVFormat
*/
SRV = 'SRV',
/**
* A TXT record contains one or more strings that are enclosed in double quotation marks (").
*
* @see https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html#TXTFormat
*/
TXT = 'TXT'
}
/**
* Options for a RecordSet.
*/
export interface RecordSetOptions {
/**
* The hosted zone in which to define the new record.
*/
readonly zone: IHostedZone;
/**
* The domain name for this record.
*
* @default zone root
*/
readonly recordName?: string;
/**
* The resource record cache time to live (TTL).
*
* @default Duration.minutes(30)
*/
readonly ttl?: Duration;
/**
* A comment to add on the record.
*
* @default no comment
*/
readonly comment?: string;
}
/**
* Type union for a record that accepts multiple types of target.
*/
export class RecordTarget {
/**
* Use string values as target.
*/
public static fromValues(...values: string[]) {
return new RecordTarget(values);
}
/**
* Use an alias as target.
*/
public static fromAlias(aliasTarget: IAliasRecordTarget) {
return new RecordTarget(undefined, aliasTarget);
}
/**
* Use ip addresses as target.
*/
public static fromIpAddresses(...ipAddresses: string[]) {
return RecordTarget.fromValues(...ipAddresses);
}
/**
*
* @param values correspond with the chosen record type (e.g. for 'A' Type, specify one ore more IP addresses)
* @param aliasTarget alias for targets such as CloudFront distribution to route traffic to
*/
protected constructor(public readonly values?: string[], public readonly aliasTarget?: IAliasRecordTarget) {
}
}
/**
* Construction properties for a RecordSet.
*/
export interface RecordSetProps extends RecordSetOptions {
/**
* The record type.
*/
readonly recordType: RecordType;
/**
* The target for this record, either `RecordTarget.fromValues()` or
* `RecordTarget.fromAlias()`.
*/
readonly target: RecordTarget;
}
/**
* A record set.
*/
export class RecordSet extends Resource implements IRecordSet {
public readonly domainName: string;
constructor(scope: Construct, id: string, props: RecordSetProps) {
super(scope, id);
const ttl = props.target.aliasTarget ? undefined : ((props.ttl && props.ttl.toSeconds()) || 1800).toString();
const recordSet = new CfnRecordSet(this, 'Resource', {
hostedZoneId: props.zone.hostedZoneId,
name: determineFullyQualifiedDomainName(props.recordName || props.zone.zoneName, props.zone),
type: props.recordType,
resourceRecords: props.target.values,
aliasTarget: props.target.aliasTarget && props.target.aliasTarget.bind(this),
ttl,
comment: props.comment,
});
this.domainName = recordSet.ref;
}
}
/**
* Target for a DNS A Record
*
* @deprecated Use RecordTarget
*/
export class AddressRecordTarget extends RecordTarget {
}
/**
* Construction properties for a ARecord.
*/
export interface ARecordProps extends RecordSetOptions {
/**
* The target.
*/
readonly target: RecordTarget;
}
/**
* A DNS A record
*
* @resource AWS::Route53::RecordSet
*/
export class ARecord extends RecordSet {
constructor(scope: Construct, id: string, props: ARecordProps) {
super(scope, id, {
...props,
recordType: RecordType.A,
target: props.target,
});
}
}
/**
* Construction properties for a AaaaRecord.
*/
export interface AaaaRecordProps extends RecordSetOptions {
/**
* The target.
*/
readonly target: RecordTarget;
}
/**
* A DNS AAAA record
*
* @resource AWS::Route53::RecordSet
*/
export class AaaaRecord extends RecordSet {
constructor(scope: Construct, id: string, props: AaaaRecordProps) {
super(scope, id, {
...props,
recordType: RecordType.AAAA,
target: props.target,
});
}
}
/**
* Construction properties for a CnameRecord.
*/
export interface CnameRecordProps extends RecordSetOptions {
/**
* The domain name.
*/
readonly domainName: string;
}
/**
* A DNS CNAME record
*
* @resource AWS::Route53::RecordSet
*/
export class CnameRecord extends RecordSet {
constructor(scope: Construct, id: string, props: CnameRecordProps) {
super(scope, id, {
...props,
recordType: RecordType.CNAME,
target: RecordTarget.fromValues(props.domainName),
});
}
}
/**
* Construction properties for a TxtRecord.
*/
export interface TxtRecordProps extends RecordSetOptions {
/**
* The text values.
*/
readonly values: string[];
}
/**
* A DNS TXT record
*
* @resource AWS::Route53::RecordSet
*/
export class TxtRecord extends RecordSet {
constructor(scope: Construct, id: string, props: TxtRecordProps) {
super(scope, id, {
...props,
recordType: RecordType.TXT,
target: RecordTarget.fromValues(...props.values.map(v => JSON.stringify(v))),
});
}
}
/**
* Properties for a SRV record value.
*/
export interface SrvRecordValue {
/**
* The priority.
*/
readonly priority: number;
/**
* The weight.
*/
readonly weight: number;
/**
* The port.
*/
readonly port: number;
/**
* The server host name.
*/
readonly hostName: string;
}
/**
* Construction properties for a SrvRecord.
*/
export interface SrvRecordProps extends RecordSetOptions {
/**
* The values.
*/
readonly values: SrvRecordValue[];
}
/**
* A DNS SRV record
*
* @resource AWS::Route53::RecordSet
*/
export class SrvRecord extends RecordSet {
constructor(scope: Construct, id: string, props: SrvRecordProps) {
super(scope, id, {
...props,
recordType: RecordType.SRV,
target: RecordTarget.fromValues(...props.values.map(v => `${v.priority} ${v.weight} ${v.port} ${v.hostName}`)),
});
}
}
/**
* The CAA tag.
*/
export enum CaaTag {
/**
* Explicity authorizes a single certificate authority to issue a
* certificate (any type) for the hostname.
*/
ISSUE = 'issue',
/**
* Explicity authorizes a single certificate authority to issue a
* wildcard certificate (and only wildcard) for the hostname.
*/
ISSUEWILD = 'issuewild',
/**
* Specifies a URL to which a certificate authority may report policy
* violations.
*/
IODEF = 'iodef',
}
/**
* Properties for a CAA record value.
*/
export interface CaaRecordValue {
/**
* The flag.
*/
readonly flag: number;
/**
* The tag.
*/
readonly tag: CaaTag;
/**
* The value associated with the tag.
*/
readonly value: string;
}
/**
* Construction properties for a CaaRecord.
*/
export interface CaaRecordProps extends RecordSetOptions {
/**
* The values.
*/
readonly values: CaaRecordValue[];
}
/**
* A DNS CAA record
*
* @resource AWS::Route53::RecordSet
*/
export class CaaRecord extends RecordSet {
constructor(scope: Construct, id: string, props: CaaRecordProps) {
super(scope, id, {
...props,
recordType: RecordType.CAA,
target: RecordTarget.fromValues(...props.values.map(v => `${v.flag} ${v.tag} "${v.value}"`)),
});
}
}
/**
* Construction properties for a CaaAmazonRecord.
*/
export interface CaaAmazonRecordProps extends RecordSetOptions {}
/**
* A DNS Amazon CAA record.
*
* A CAA record to restrict certificate authorities allowed
* to issue certificates for a domain to Amazon only.
*
* @resource AWS::Route53::RecordSet
*/
export class CaaAmazonRecord extends CaaRecord {
constructor(scope: Construct, id: string, props: CaaAmazonRecordProps) {
super(scope, id, {
...props,
values: [
{
flag: 0,
tag: CaaTag.ISSUE,
value: 'amazon.com',
},
],
});
}
}
/**
* Properties for a MX record value.
*/
export interface MxRecordValue {
/**
* The priority.
*/
readonly priority: number;
/**
* The mail server host name.
*/
readonly hostName: string;
}
/**
* Construction properties for a MxRecord.
*/
export interface MxRecordProps extends RecordSetOptions {
/**
* The values.
*/
readonly values: MxRecordValue[];
}
/**
* A DNS MX record
*
* @resource AWS::Route53::RecordSet
*/
export class MxRecord extends RecordSet {
constructor(scope: Construct, id: string, props: MxRecordProps) {
super(scope, id, {
...props,
recordType: RecordType.MX,
target: RecordTarget.fromValues(...props.values.map(v => `${v.priority} ${v.hostName}`)),
});
}
}
/**
* Construction properties for a ZoneDelegationRecord
*/
export interface ZoneDelegationRecordProps extends RecordSetOptions {
/**
* The name servers to report in the delegation records.
*/
readonly nameServers: string[];
}
/**
* A record to delegate further lookups to a different set of name servers.
*/
export class ZoneDelegationRecord extends RecordSet {
constructor(scope: Construct, id: string, props: ZoneDelegationRecordProps) {
super(scope, id, {
...props,
recordType: RecordType.NS,
target: RecordTarget.fromValues(...Token.isUnresolved(props.nameServers)
? props.nameServers // Can't map a string-array token!
: props.nameServers.map(ns => (Token.isUnresolved(ns) || ns.endsWith('.')) ? ns : `${ns}.`),
),
ttl: props.ttl || Duration.days(2),
});
}
}