Skip to content

Commit

Permalink
fix(route53-targets): move Alias Targets into their own package (#2617)
Browse files Browse the repository at this point in the history
In accordance with new guidelines, we're centralizing cross-service
integrations into their own package. In this case, centralizing
Route53 Alias Targets into `@aws-cdk/aws-route53-targets`.

Fixes #2448.

BREAKING CHANGE: using a CloudFront Distribution or an ELBv2
Load Balancer as an Alias Record Target now requires an integration
object from the `@aws-cdk/aws-route53-targets` package.
  • Loading branch information
rix0rrr authored May 23, 2019
1 parent bd60e87 commit f40fe98
Show file tree
Hide file tree
Showing 33 changed files with 5,605 additions and 81 deletions.
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/lib/distribution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Interface for CloudFront distributions
*/
export interface IDistribution {
/**
* The domain name of the distribution
*/
readonly domainName: string;
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-cloudfront/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './distribution';
export * from './web_distribution';

// AWS::CloudFront CloudFormation Resources:
Expand Down
18 changes: 2 additions & 16 deletions packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import route53 = require('@aws-cdk/aws-route53');
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import { CfnDistribution } from './cloudfront.generated';
import { IDistribution } from './distribution';

export enum HttpVersion {
HTTP1_1 = "http1.1",
Expand Down Expand Up @@ -487,14 +487,7 @@ interface BehaviorWithOrigin extends Behavior {
*
*
*/
export class CloudFrontWebDistribution extends cdk.Construct implements route53.IAliasRecordTarget {

/**
* The hosted zone Id if using an alias record in Route53.
* This value never changes.
*/
public readonly aliasHostedZoneId: string = "Z2FDTNDATAQYW2";

export class CloudFrontWebDistribution extends cdk.Construct implements IDistribution {
/**
* The logging bucket for this CloudFront distribution.
* If logging is not enabled for this distribution - this property will be undefined.
Expand Down Expand Up @@ -682,13 +675,6 @@ export class CloudFrontWebDistribution extends cdk.Construct implements route53.
this.distributionId = distribution.distributionId;
}

public asAliasRecordTarget(): route53.AliasRecordTargetProps {
return {
hostedZoneId: this.aliasHostedZoneId,
dnsName: this.domainName
};
}

private toBehavior(input: BehaviorWithOrigin, protoPolicy?: ViewerProtocolPolicy) {
let toReturn = {
allowedMethods: this.METHOD_LOOKUP_MAP[input.allowedMethods || CloudFrontAllowedMethods.GET_HEAD],
Expand Down
2 changes: 0 additions & 2 deletions packages/@aws-cdk/aws-cloudfront/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"@aws-cdk/aws-certificatemanager": "^0.31.0",
"@aws-cdk/aws-iam": "^0.31.0",
"@aws-cdk/aws-kms": "^0.31.0",
"@aws-cdk/aws-route53": "^0.31.0",
"@aws-cdk/aws-s3": "^0.31.0",
"@aws-cdk/cdk": "^0.31.0"
},
Expand All @@ -80,7 +79,6 @@
"@aws-cdk/aws-certificatemanager": "^0.31.0",
"@aws-cdk/aws-iam": "^0.31.0",
"@aws-cdk/aws-kms": "^0.31.0",
"@aws-cdk/aws-route53": "^0.31.0",
"@aws-cdk/aws-s3": "^0.31.0",
"@aws-cdk/cdk": "^0.31.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs-patterns/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AliasRecord, IHostedZone } from '@aws-cdk/aws-route53';
import targets = require('@aws-cdk/aws-route53-targets');
import cdk = require('@aws-cdk/cdk');
import { FargateService } from './fargate/fargate-service';
import { FargateTaskDefinition } from './fargate/fargate-task-definition';
Expand Down Expand Up @@ -124,7 +125,7 @@ export class LoadBalancedFargateService extends LoadBalancedServiceBase {
new AliasRecord(this, "DNS", {
zone: props.domainZone,
recordName: props.domainName,
target: this.loadBalancer
target: new targets.LoadBalancerTarget(this.loadBalancer),
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-ecs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"@aws-cdk/aws-lambda": "^0.31.0",
"@aws-cdk/aws-logs": "^0.31.0",
"@aws-cdk/aws-route53": "^0.31.0",
"@aws-cdk/aws-route53-targets": "^0.31.0",
"@aws-cdk/aws-secretsmanager": "^0.31.0",
"@aws-cdk/aws-servicediscovery": "^0.31.0",
"@aws-cdk/aws-sns": "^0.31.0",
Expand All @@ -106,6 +107,7 @@
"@aws-cdk/aws-lambda": "^0.31.0",
"@aws-cdk/aws-logs": "^0.31.0",
"@aws-cdk/aws-route53": "^0.31.0",
"@aws-cdk/aws-route53-targets": "^0.31.0",
"@aws-cdk/aws-secretsmanager": "^0.31.0",
"@aws-cdk/aws-servicediscovery": "^0.31.0",
"@aws-cdk/aws-sns": "^0.31.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import cloudwatch = require('@aws-cdk/aws-cloudwatch');
import ec2 = require('@aws-cdk/aws-ec2');
import iam = require('@aws-cdk/aws-iam');
import s3 = require('@aws-cdk/aws-s3');
import { Construct, IResource, Token } from '@aws-cdk/cdk';
import { BaseLoadBalancer, BaseLoadBalancerProps } from '../shared/base-load-balancer';
import { Construct, Token } from '@aws-cdk/cdk';
import { BaseLoadBalancer, BaseLoadBalancerProps, ILoadBalancerV2 } from '../shared/base-load-balancer';
import { IpAddressType } from '../shared/enums';
import { ApplicationListener, BaseApplicationListenerProps } from './application-listener';

Expand Down Expand Up @@ -468,7 +468,7 @@ export enum HttpCodeTarget {
/**
* An application load balancer
*/
export interface IApplicationLoadBalancer extends IResource, ec2.IConnectable {
export interface IApplicationLoadBalancer extends ILoadBalancerV2, ec2.IConnectable {
/**
* The ARN of this load balancer
*/
Expand Down Expand Up @@ -498,6 +498,20 @@ export interface ApplicationLoadBalancerAttributes {
* ID of the load balancer's security group
*/
readonly securityGroupId: string;

/**
* The canonical hosted zone ID of this load balancer
*
* @default - When not provided, LB cannot be used as Route53 Alias target.
*/
readonly loadBalancerCanonicalHostedZoneId?: string;

/**
* The DNS name of this load balancer
*
* @default - When not provided, LB cannot be used as Route53 Alias target.
*/
readonly loadBalancerDnsName?: string;
}

// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html#access-logging-bucket-permissions
Expand Down Expand Up @@ -544,7 +558,7 @@ class ImportedApplicationLoadBalancer extends Construct implements IApplicationL
*/
public readonly vpc?: ec2.IVpc;

constructor(scope: Construct, id: string, props: ApplicationLoadBalancerAttributes) {
constructor(scope: Construct, id: string, private readonly props: ApplicationLoadBalancerAttributes) {
super(scope, id);

this.loadBalancerArn = props.loadBalancerArn;
Expand All @@ -559,4 +573,16 @@ class ImportedApplicationLoadBalancer extends Construct implements IApplicationL
...props
});
}

public get loadBalancerCanonicalHostedZoneId(): string {
if (this.props.loadBalancerCanonicalHostedZoneId) { return this.props.loadBalancerCanonicalHostedZoneId; }
// tslint:disable-next-line:max-line-length
throw new Error(`'loadBalancerCanonicalHostedZoneId' was not provided when constructing Application Load Balancer ${this.node.path} from attributes`);
}

public get loadBalancerDnsName(): string {
if (this.props.loadBalancerDnsName) { return this.props.loadBalancerDnsName; }
// tslint:disable-next-line:max-line-length
throw new Error(`'loadBalancerDnsName' was not provided when constructing Application Load Balancer ${this.node.path} from attributes`);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
import ec2 = require('@aws-cdk/aws-ec2');
import { Construct, IResource, Resource } from '@aws-cdk/cdk';
import { BaseLoadBalancer, BaseLoadBalancerProps } from '../shared/base-load-balancer';
import { Construct, Resource } from '@aws-cdk/cdk';
import { BaseLoadBalancer, BaseLoadBalancerProps, ILoadBalancerV2 } from '../shared/base-load-balancer';
import { BaseNetworkListenerProps, NetworkListener } from './network-listener';

/**
Expand All @@ -16,22 +16,58 @@ export interface NetworkLoadBalancerProps extends BaseLoadBalancerProps {
readonly crossZoneEnabled?: boolean;
}

/**
* Properties to reference an existing load balancer
*/
export interface NetworkLoadBalancerAttributes {
/**
* ARN of the load balancer
*/
readonly loadBalancerArn: string;

/**
* The canonical hosted zone ID of this load balancer
*
* @default - When not provided, LB cannot be used as Route53 Alias target.
*/
readonly loadBalancerCanonicalHostedZoneId?: string;

/**
* The DNS name of this load balancer
*
* @default - When not provided, LB cannot be used as Route53 Alias target.
*/
readonly loadBalancerDnsName?: string;
}

/**
* Define a new network load balancer
*
* @resource AWS::ElasticLoadBalancingV2::LoadBalancer
*/
export class NetworkLoadBalancer extends BaseLoadBalancer implements INetworkLoadBalancer {
public static fromNetworkLoadBalancerArn(scope: Construct, id: string, networkLoadBalancerArn: string): INetworkLoadBalancer {
public static fromNetworkLoadBalancerAttributes(scope: Construct, id: string, attrs: NetworkLoadBalancerAttributes): INetworkLoadBalancer {
class Import extends Resource implements INetworkLoadBalancer {
public readonly loadBalancerArn = networkLoadBalancerArn;
public readonly loadBalancerArn = attrs.loadBalancerArn;
public readonly vpc?: ec2.IVpc = undefined;
public addListener(lid: string, props: BaseNetworkListenerProps): NetworkListener {
return new NetworkListener(this, lid, {
loadBalancer: this,
...props
});
}

public get loadBalancerCanonicalHostedZoneId(): string {
if (attrs.loadBalancerCanonicalHostedZoneId) { return attrs.loadBalancerCanonicalHostedZoneId; }
// tslint:disable-next-line:max-line-length
throw new Error(`'loadBalancerCanonicalHostedZoneId' was not provided when constructing Network Load Balancer ${this.node.path} from attributes`);
}

public get loadBalancerDnsName(): string {
if (attrs.loadBalancerDnsName) { return attrs.loadBalancerDnsName; }
// tslint:disable-next-line:max-line-length
throw new Error(`'loadBalancerDnsName' was not provided when constructing Network Load Balancer ${this.node.path} from attributes`);
}
}

return new Import(scope, id);
Expand Down Expand Up @@ -191,7 +227,7 @@ export class NetworkLoadBalancer extends BaseLoadBalancer implements INetworkLoa
/**
* A network load balancer
*/
export interface INetworkLoadBalancer extends IResource {
export interface INetworkLoadBalancer extends ILoadBalancerV2 {
/**
* The ARN of this load balancer
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ec2 = require('@aws-cdk/aws-ec2');
import route53 = require('@aws-cdk/aws-route53');
import { Construct, Resource, Token } from '@aws-cdk/cdk';
import { Construct, IResource, Resource, Token } from '@aws-cdk/cdk';
import { CfnLoadBalancer } from '../elasticloadbalancingv2.generated';
import { Attributes, ifUndefined, renderAttributes } from './util';

Expand Down Expand Up @@ -42,10 +41,28 @@ export interface BaseLoadBalancerProps {
readonly deletionProtection?: boolean;
}

export interface ILoadBalancerV2 extends IResource {
/**
* The canonical hosted zone ID of this load balancer
*
* @example Z2P70J7EXAMPLE
* @attribute
*/
readonly loadBalancerCanonicalHostedZoneId: string;

/**
* The DNS name of this load balancer
*
* @example my-load-balancer-424835706.us-west-2.elb.amazonaws.com
* @attribute
*/
readonly loadBalancerDnsName: string;
}

/**
* Base class for both Application and Network Load Balancers
*/
export abstract class BaseLoadBalancer extends Resource implements route53.IAliasRecordTarget {
export abstract class BaseLoadBalancer extends Resource {
/**
* The canonical hosted zone ID of this load balancer
*
Expand Down Expand Up @@ -151,11 +168,4 @@ export abstract class BaseLoadBalancer extends Resource implements route53.IAlia
public removeAttribute(key: string) {
this.setAttribute(key, undefined);
}

public asAliasRecordTarget(): route53.AliasRecordTargetProps {
return {
hostedZoneId: this.loadBalancerCanonicalHostedZoneId,
dnsName: this.loadBalancerDnsName
};
}
}
2 changes: 0 additions & 2 deletions packages/@aws-cdk/aws-elasticloadbalancingv2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"@aws-cdk/aws-cloudwatch": "^0.31.0",
"@aws-cdk/aws-ec2": "^0.31.0",
"@aws-cdk/aws-iam": "^0.31.0",
"@aws-cdk/aws-route53": "^0.31.0",
"@aws-cdk/aws-s3": "^0.31.0",
"@aws-cdk/cdk": "^0.31.0"
},
Expand All @@ -81,7 +80,6 @@
"@aws-cdk/aws-cloudwatch": "^0.31.0",
"@aws-cdk/aws-ec2": "^0.31.0",
"@aws-cdk/aws-iam": "^0.31.0",
"@aws-cdk/aws-route53": "^0.31.0",
"@aws-cdk/aws-s3": "^0.31.0",
"@aws-cdk/cdk": "^0.31.0"
},
Expand Down
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-route53-targets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*.js
tsconfig.json
tslint.json
*.js.map
*.d.ts
*.generated.ts
dist
lib/generated/resources.ts
.jsii

.LAST_BUILD
.nyc_output
coverage
.nycrc
.LAST_PACKAGE
*.snk
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-route53-targets/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Don't include original .ts files when doing `npm pack`
*.ts
!*.d.ts
coverage
.nyc_output
*.tgz

dist
.LAST_PACKAGE
.LAST_BUILD
!*.js

# Include .jsii
!.jsii

*.snk

*.tsbuildinfo
Loading

0 comments on commit f40fe98

Please sign in to comment.