Skip to content

Commit

Permalink
feat(route53): add classic elb target support (aws#3380)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Jimmy Gaussen authored and mergify[bot] committed Aug 9, 2019
1 parent 1509399 commit b0720dd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-route53-targets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,13 @@ This library contains Route53 Alias Record targets for:
// or - route53.RecordTarget.fromAlias(new alias.ApiGatewayDomainName(domainName)),
});
```
* Classic load balancers
```ts
new route53.ARecord(this, 'AliasRecord', {
zone,
target: route53.RecordTarget.fromAlias(new alias.ClassicLoadBalancerTarget(elb)),
// or - route53.RecordTarget.fromAlias(new alias.ApiGatewayDomainName(domainName)),
});
```

See the documentation of `@aws-cdk/aws-route53` for more information.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import elb = require('@aws-cdk/aws-elasticloadbalancing');
import route53 = require('@aws-cdk/aws-route53');

/**
* Use a classic ELB as an alias record target
*/
export class ClassicLoadBalancerTarget implements route53.IAliasRecordTarget {
constructor(private readonly loadBalancer: elb.LoadBalancer) {
}

public bind(_record: route53.IRecordSet): route53.AliasRecordTargetConfig {
return {
hostedZoneId: this.loadBalancer.loadBalancerCanonicalHostedZoneNameId,
dnsName: this.loadBalancer.loadBalancerDnsName
};
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-route53-targets/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './api-gateway-domain-name';
export * from './classic-load-balancer-target';
export * from './cloudfront-target';
export * from './load-balancer-target';
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-route53-targets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"@aws-cdk/aws-apigateway": "^1.3.0",
"@aws-cdk/aws-cloudfront": "^1.3.0",
"@aws-cdk/aws-elasticloadbalancingv2": "^1.3.0",
"@aws-cdk/aws-elasticloadbalancing": "^1.3.0",
"@aws-cdk/aws-iam": "^1.3.0",
"@aws-cdk/aws-route53": "^1.3.0",
"@aws-cdk/core": "^1.3.0"
Expand All @@ -94,6 +95,7 @@
"@aws-cdk/aws-apigateway": "^1.3.0",
"@aws-cdk/aws-cloudfront": "^1.3.0",
"@aws-cdk/aws-elasticloadbalancingv2": "^1.3.0",
"@aws-cdk/aws-elasticloadbalancing": "^1.3.0",
"@aws-cdk/aws-iam": "^1.3.0",
"@aws-cdk/aws-route53": "^1.3.0",
"@aws-cdk/core": "^1.3.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import '@aws-cdk/assert/jest';
import ec2 = require('@aws-cdk/aws-ec2');
import elb = require('@aws-cdk/aws-elasticloadbalancing');
import route53 = require('@aws-cdk/aws-route53');
import { Stack } from '@aws-cdk/core';
import targets = require('../lib');

test('use classic ELB as record target', () => {
// GIVEN
const stack = new Stack();
const vpc = new ec2.Vpc(stack, 'VPC', {
maxAzs: 2
});
const lb = new elb.LoadBalancer(stack, 'LB', {
vpc,
internetFacing: true
});

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

// WHEN
new route53.ARecord(zone, 'Alias', {
zone,
recordName: '_foo',
target: route53.AddressRecordTarget.fromAlias(new targets.ClassicLoadBalancerTarget(lb))
});

// THEN
expect(stack).toHaveResource('AWS::Route53::RecordSet', {
AliasTarget: {
DNSName: { "Fn::GetAtt": [ "LB8A12904C", "DNSName" ] },
HostedZoneId: { "Fn::GetAtt": [ "LB8A12904C", "CanonicalHostedZoneNameID" ] }
},
});
});

0 comments on commit b0720dd

Please sign in to comment.