Skip to content

Commit b0720dd

Browse files
Jimmy Gaussenmergify[bot]
authored andcommitted
feat(route53): add classic elb target support (#3380)
* feat(route53-targets): add classic elb target support * fix(route53-targets): doc example Co-Authored-By: Jonathan Goldwasser <jogold@users.noreply.github.com> * chore(route53): fix export ordering
1 parent 1509399 commit b0720dd

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,13 @@ This library contains Route53 Alias Record targets for:
3333
// or - route53.RecordTarget.fromAlias(new alias.ApiGatewayDomainName(domainName)),
3434
});
3535
```
36+
* Classic load balancers
37+
```ts
38+
new route53.ARecord(this, 'AliasRecord', {
39+
zone,
40+
target: route53.RecordTarget.fromAlias(new alias.ClassicLoadBalancerTarget(elb)),
41+
// or - route53.RecordTarget.fromAlias(new alias.ApiGatewayDomainName(domainName)),
42+
});
43+
```
3644

3745
See the documentation of `@aws-cdk/aws-route53` for more information.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import elb = require('@aws-cdk/aws-elasticloadbalancing');
2+
import route53 = require('@aws-cdk/aws-route53');
3+
4+
/**
5+
* Use a classic ELB as an alias record target
6+
*/
7+
export class ClassicLoadBalancerTarget implements route53.IAliasRecordTarget {
8+
constructor(private readonly loadBalancer: elb.LoadBalancer) {
9+
}
10+
11+
public bind(_record: route53.IRecordSet): route53.AliasRecordTargetConfig {
12+
return {
13+
hostedZoneId: this.loadBalancer.loadBalancerCanonicalHostedZoneNameId,
14+
dnsName: this.loadBalancer.loadBalancerDnsName
15+
};
16+
}
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './api-gateway-domain-name';
2+
export * from './classic-load-balancer-target';
23
export * from './cloudfront-target';
34
export * from './load-balancer-target';

packages/@aws-cdk/aws-route53-targets/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"@aws-cdk/aws-apigateway": "^1.3.0",
8686
"@aws-cdk/aws-cloudfront": "^1.3.0",
8787
"@aws-cdk/aws-elasticloadbalancingv2": "^1.3.0",
88+
"@aws-cdk/aws-elasticloadbalancing": "^1.3.0",
8889
"@aws-cdk/aws-iam": "^1.3.0",
8990
"@aws-cdk/aws-route53": "^1.3.0",
9091
"@aws-cdk/core": "^1.3.0"
@@ -94,6 +95,7 @@
9495
"@aws-cdk/aws-apigateway": "^1.3.0",
9596
"@aws-cdk/aws-cloudfront": "^1.3.0",
9697
"@aws-cdk/aws-elasticloadbalancingv2": "^1.3.0",
98+
"@aws-cdk/aws-elasticloadbalancing": "^1.3.0",
9799
"@aws-cdk/aws-iam": "^1.3.0",
98100
"@aws-cdk/aws-route53": "^1.3.0",
99101
"@aws-cdk/core": "^1.3.0"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import '@aws-cdk/assert/jest';
2+
import ec2 = require('@aws-cdk/aws-ec2');
3+
import elb = require('@aws-cdk/aws-elasticloadbalancing');
4+
import route53 = require('@aws-cdk/aws-route53');
5+
import { Stack } from '@aws-cdk/core';
6+
import targets = require('../lib');
7+
8+
test('use classic ELB as record target', () => {
9+
// GIVEN
10+
const stack = new Stack();
11+
const vpc = new ec2.Vpc(stack, 'VPC', {
12+
maxAzs: 2
13+
});
14+
const lb = new elb.LoadBalancer(stack, 'LB', {
15+
vpc,
16+
internetFacing: true
17+
});
18+
19+
const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' });
20+
21+
// WHEN
22+
new route53.ARecord(zone, 'Alias', {
23+
zone,
24+
recordName: '_foo',
25+
target: route53.AddressRecordTarget.fromAlias(new targets.ClassicLoadBalancerTarget(lb))
26+
});
27+
28+
// THEN
29+
expect(stack).toHaveResource('AWS::Route53::RecordSet', {
30+
AliasTarget: {
31+
DNSName: { "Fn::GetAtt": [ "LB8A12904C", "DNSName" ] },
32+
HostedZoneId: { "Fn::GetAtt": [ "LB8A12904C", "CanonicalHostedZoneNameID" ] }
33+
},
34+
});
35+
});

0 commit comments

Comments
 (0)