Skip to content

Commit 696f53f

Browse files
jogoldrix0rrr
authored andcommittedJun 7, 2019
feat(route53): improve constructs for basic records (#2741)
Constructs for records (CNAME, TXT, etc.) now extend the `RecordSet` construct and offer better typed properties interfaces. Add constructs for A, AAAA, CAA, MX and SRV records. Add support for multiple values in basic records. Make `recordName` optional with default to zone root. Add a "security" `CaaAmazonRecord` construct to easily restrict certificate authorities allowed to issue certificates for a domain to Amazon only. BREAKING CHANGE: `recordValue: string` prop in `route53.TxtRecord` changed to `values: string[]` * `recordValue` prop in `route53.CnameRecord` renamed to `domainName` * `route53.AliasRecord` has been removed, use `route53.ARecord` or `route53.AaaaRecord` with the `target` prop.
1 parent 6d83cb9 commit 696f53f

27 files changed

+1087
-820
lines changed
 

‎packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ecs = require('@aws-cdk/aws-ecs');
2-
import { AliasRecord, IHostedZone } from '@aws-cdk/aws-route53';
2+
import { AddressRecordTarget, ARecord, IHostedZone } from '@aws-cdk/aws-route53';
33
import targets = require('@aws-cdk/aws-route53-targets');
44
import cdk = require('@aws-cdk/cdk');
55
import { LoadBalancedServiceBase, LoadBalancedServiceBaseProps } from '../base/load-balanced-service-base';
@@ -120,10 +120,10 @@ export class LoadBalancedFargateService extends LoadBalancedServiceBase {
120120
throw new Error('A Route53 hosted domain zone name is required to configure the specified domain name');
121121
}
122122

123-
new AliasRecord(this, "DNS", {
123+
new ARecord(this, "DNS", {
124124
zone: props.domainZone,
125125
recordName: props.domainName,
126-
target: new targets.LoadBalancerTarget(this.loadBalancer),
126+
target: AddressRecordTarget.fromAlias(new targets.LoadBalancerTarget(this.loadBalancer)),
127127
});
128128
}
129129
}

‎packages/@aws-cdk/aws-route53-targets/lib/cloudfront-target.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class CloudFrontTarget implements route53.IAliasRecordTarget {
1414
constructor(private readonly distribution: cloudfront.CloudFrontWebDistribution) {
1515
}
1616

17-
public bind(_record: route53.IAliasRecord): route53.AliasRecordTargetConfig {
17+
public bind(_record: route53.IRecordSet): route53.AliasRecordTargetConfig {
1818
return {
1919
hostedZoneId: CLOUDFRONT_ZONE_ID,
2020
dnsName: this.distribution.domainName

‎packages/@aws-cdk/aws-route53-targets/lib/load-balancer-target.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class LoadBalancerTarget implements route53.IAliasRecordTarget {
88
constructor(private readonly loadBalancer: elbv2.ILoadBalancerV2) {
99
}
1010

11-
public bind(_record: route53.IAliasRecord): route53.AliasRecordTargetConfig {
11+
public bind(_record: route53.IRecordSet): route53.AliasRecordTargetConfig {
1212
return {
1313
hostedZoneId: this.loadBalancer.loadBalancerCanonicalHostedZoneId,
1414
dnsName: this.loadBalancer.loadBalancerDnsName

‎packages/@aws-cdk/aws-route53-targets/test/cloudfront-target.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ test('use CloudFront as record target', () => {
2424

2525
// WHEN
2626
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' });
27-
new route53.AliasRecord(zone, 'Alias', {
27+
new route53.ARecord(zone, 'Alias', {
2828
zone,
2929
recordName: '_foo',
30-
target: new targets.CloudFrontTarget(distribution)
30+
target: route53.AddressRecordTarget.fromAlias(new targets.CloudFrontTarget(distribution))
3131
});
3232

3333
// THEN
@@ -37,4 +37,4 @@ test('use CloudFront as record target', () => {
3737
HostedZoneId: "Z2FDTNDATAQYW2"
3838
},
3939
});
40-
});
40+
});

‎packages/@aws-cdk/aws-route53-targets/test/integ.alb-alias-target.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', {
1919

2020
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' });
2121

22-
new route53.AliasRecord(zone, 'Alias', {
22+
new route53.ARecord(zone, 'Alias', {
2323
zone,
2424
recordName: '_foo',
25-
target: new targets.LoadBalancerTarget(lb)
25+
target: route53.AddressRecordTarget.fromAlias(new targets.LoadBalancerTarget(lb))
2626
});
2727

2828
app.synth();

‎packages/@aws-cdk/aws-route53-targets/test/integ.cloudfront-alias-target.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ const distribution = new cloudfront.CloudFrontWebDistribution(stack, 'MyDistribu
2525
]
2626
});
2727

28-
new route53.AliasRecord(zone, 'Alias', {
28+
new route53.ARecord(zone, 'Alias', {
2929
zone,
3030
recordName: '_foo',
31-
target: new targets.CloudFrontTarget(distribution)
31+
target: route53.AddressRecordTarget.fromAlias(new targets.CloudFrontTarget(distribution))
3232
});
3333

3434
app.synth();

‎packages/@aws-cdk/aws-route53-targets/test/load-balancer-target.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ test('use ALB as record target', () => {
1919
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' });
2020

2121
// WHEN
22-
new route53.AliasRecord(zone, 'Alias', {
22+
new route53.ARecord(zone, 'Alias', {
2323
zone,
2424
recordName: '_foo',
25-
target: new targets.LoadBalancerTarget(lb)
25+
target: route53.AddressRecordTarget.fromAlias(new targets.LoadBalancerTarget(lb))
2626
});
2727

2828
// THEN

‎packages/@aws-cdk/aws-route53/README.md

+42-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ To add a public hosted zone:
1818
import route53 = require('@aws-cdk/aws-route53');
1919

2020
new route53.PublicHostedZone(this, 'HostedZone', {
21-
zoneName: 'fully.qualified.domain.com'
21+
zoneName: 'fully.qualified.domain.com'
2222
});
2323
```
2424

@@ -33,8 +33,8 @@ import route53 = require('@aws-cdk/aws-route53');
3333
const vpc = new ec2.VpcNetwork(this, 'VPC');
3434

3535
const zone = new route53.PrivateHostedZone(this, 'HostedZone', {
36-
zoneName: 'fully.qualified.domain.com',
37-
vpc // At least one VPC has to be added to a Private Hosted Zone.
36+
zoneName: 'fully.qualified.domain.com',
37+
vpc // At least one VPC has to be added to a Private Hosted Zone.
3838
});
3939
```
4040

@@ -46,24 +46,54 @@ To add a TXT record to your zone:
4646
```ts
4747
import route53 = require('@aws-cdk/aws-route53');
4848

49-
new route53.TxtRecord(zone, 'TXTRecord', {
50-
recordName: '_foo', // If the name ends with a ".", it will be used as-is;
51-
// if it ends with a "." followed by the zone name, a trailing "." will be added automatically;
52-
// otherwise, a ".", the zone name, and a trailing "." will be added automatically.
53-
recordValue: 'Bar!', // Will be quoted for you, and " will be escaped automatically.
54-
ttl: 90, // Optional - default is 1800
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: 90, // Optional - default is 1800
5560
});
5661
```
5762

63+
To add a A record to your zone:
64+
```ts
65+
import route53 = require('@aws-cdk/aws-route53');
66+
67+
new route53.ARecord(this, 'ARecord', {
68+
zone: myZone,
69+
target: route53.AddressRecordTarget.fromIpAddresses('1.2.3.4', '5.6.7.8')
70+
})
71+
```
72+
73+
To add a AAAA record pointing to a CloudFront distribution:
74+
```ts
75+
import route53 = require('@aws-cdk/aws-route53');
76+
import targets = require('@aws-cdk/aws-route53-targets');
77+
78+
new route53.AaaaRecord(this, 'Alias', {
79+
zone: myZone,
80+
target: route53.AddressRecordTarget.fromAlias(new targets.CloudFrontTarget(distribution))
81+
})
82+
```
83+
84+
Constructs are available for A, AAAA, CAA, CNAME, MX, NS, SRV and TXT records.
85+
86+
Use the `CaaAmazonRecord` construct to easily restrict certificate authorities
87+
allowed to issue certificates for a domain to Amazon only.
5888

5989
### Adding records to existing hosted zones
6090

6191
If you know the ID and Name of a Hosted Zone, you can import it directly:
6292

6393
```ts
6494
const zone = HostedZone.import(this, 'MyZone', {
65-
zoneName: 'example.com',
66-
hostedZoneId: 'ZOJJZC49E0EPZ',
95+
zoneName: 'example.com',
96+
hostedZoneId: 'ZOJJZC49E0EPZ',
6797
});
6898
```
6999

@@ -72,6 +102,6 @@ to discover and import it:
72102

73103
```ts
74104
const zone = new HostedZoneProvider(this, {
75-
domainName: 'example.com'
105+
domainName: 'example.com'
76106
}).findAndImport(this, 'MyZone');
77107
```

‎packages/@aws-cdk/aws-route53/lib/alias-record-target.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IAliasRecord } from "./records/alias";
1+
import { IRecordSet } from "./record-set";
22

33
/**
44
* Classes that are valid alias record targets, like CloudFront distributions and load
@@ -8,7 +8,7 @@ export interface IAliasRecordTarget {
88
/**
99
* Return hosted zone ID and DNS name, usable for Route53 alias targets
1010
*/
11-
bind(record: IAliasRecord): AliasRecordTargetConfig;
11+
bind(record: IRecordSet): AliasRecordTargetConfig;
1212
}
1313

1414
/**

‎packages/@aws-cdk/aws-route53/lib/hosted-zone.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ec2 = require('@aws-cdk/aws-ec2');
22
import { Construct, Resource, Token } from '@aws-cdk/cdk';
33
import { HostedZoneAttributes, IHostedZone } from './hosted-zone-ref';
4-
import { ZoneDelegationRecord } from './records';
4+
import { CaaAmazonRecord, ZoneDelegationRecord } from './record-set';
55
import { CfnHostedZone } from './route53.generated';
66
import { validateZoneName } from './util';
77

@@ -107,7 +107,19 @@ export class HostedZone extends Resource implements IHostedZone {
107107
}
108108
}
109109

110-
export interface PublicHostedZoneProps extends CommonHostedZoneProps { }
110+
/**
111+
* Construction properties for a PublicHostedZone.
112+
*/
113+
export interface PublicHostedZoneProps extends CommonHostedZoneProps {
114+
/**
115+
* Whether to create a CAA record to restrict certificate authorities allowed
116+
* to issue certificates for this domain to Amazon only.
117+
*
118+
* @default false
119+
*/
120+
readonly caaAmazon?: boolean;
121+
}
122+
111123
export interface IPublicHostedZone extends IHostedZone { }
112124

113125
/**
@@ -127,6 +139,12 @@ export class PublicHostedZone extends HostedZone implements IPublicHostedZone {
127139

128140
constructor(scope: Construct, id: string, props: PublicHostedZoneProps) {
129141
super(scope, id, props);
142+
143+
if (props.caaAmazon) {
144+
new CaaAmazonRecord(this, 'CaaAmazon', {
145+
zone: this
146+
});
147+
}
130148
}
131149

132150
public addVpc(_vpc: ec2.IVpc) {
@@ -142,7 +160,7 @@ export class PublicHostedZone extends HostedZone implements IPublicHostedZone {
142160
public addDelegation(delegate: IPublicHostedZone, opts: ZoneDelegationOptions = {}): void {
143161
new ZoneDelegationRecord(this, `${this.zoneName} -> ${delegate.zoneName}`, {
144162
zone: this,
145-
delegatedZoneName: delegate.zoneName,
163+
recordName: delegate.zoneName,
146164
nameServers: delegate.hostedZoneNameServers!, // PublicHostedZones always have name servers!
147165
comment: opts.comment,
148166
ttl: opts.ttl,

‎packages/@aws-cdk/aws-route53/lib/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * from './hosted-zone';
22
export * from './hosted-zone-provider';
33
export * from './hosted-zone-ref';
4-
export * from './records';
4+
export * from './record-set';
55
export * from './alias-record-target';
66

77
// AWS::Route53 CloudFormation Resources:

0 commit comments

Comments
 (0)