diff --git a/packages/@aws-cdk/aws-route53-targets/README.md b/packages/@aws-cdk/aws-route53-targets/README.md index b5c12600cbecb..b79d6d54d5f27 100644 --- a/packages/@aws-cdk/aws-route53-targets/README.md +++ b/packages/@aws-cdk/aws-route53-targets/README.md @@ -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. diff --git a/packages/@aws-cdk/aws-route53-targets/lib/classic-load-balancer-target.ts b/packages/@aws-cdk/aws-route53-targets/lib/classic-load-balancer-target.ts new file mode 100644 index 0000000000000..88ce500e06935 --- /dev/null +++ b/packages/@aws-cdk/aws-route53-targets/lib/classic-load-balancer-target.ts @@ -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 + }; + } +} diff --git a/packages/@aws-cdk/aws-route53-targets/lib/index.ts b/packages/@aws-cdk/aws-route53-targets/lib/index.ts index 6f8cfee50c9a8..45296c9570550 100644 --- a/packages/@aws-cdk/aws-route53-targets/lib/index.ts +++ b/packages/@aws-cdk/aws-route53-targets/lib/index.ts @@ -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'; diff --git a/packages/@aws-cdk/aws-route53-targets/package.json b/packages/@aws-cdk/aws-route53-targets/package.json index e4006ade4fe35..f9b1f4472610e 100644 --- a/packages/@aws-cdk/aws-route53-targets/package.json +++ b/packages/@aws-cdk/aws-route53-targets/package.json @@ -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" @@ -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" diff --git a/packages/@aws-cdk/aws-route53-targets/test/classic-load-balancer-target.test.ts b/packages/@aws-cdk/aws-route53-targets/test/classic-load-balancer-target.test.ts new file mode 100644 index 0000000000000..d934291c7b1a2 --- /dev/null +++ b/packages/@aws-cdk/aws-route53-targets/test/classic-load-balancer-target.test.ts @@ -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" ] } + }, + }); +});