Skip to content

Commit

Permalink
fix(codedeploy): change the load balancer API in server Deployment Gr…
Browse files Browse the repository at this point in the history
…oup.

BREAKING CHANGE: the type of the `loadBalancer` property in ServerDeploymentGroupProps has been changed.

Fixes #2449
  • Loading branch information
skinny85 committed May 15, 2019
1 parent 82c1f10 commit ad62333
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 94 deletions.
8 changes: 3 additions & 5 deletions packages/@aws-cdk/aws-codedeploy-api/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
## AWS CodeDeploy Load Balancing API

This package contains the abstract API of Load Balancers
as required by the AWS CodeDeploy Construct Library.

You should never need to depend on it directly -
instead, depend on the `aws-codedeploy` package.
This package has been deprecated,
and will be removed in a future release of the Cloud Development Kit.
Please remove if from your dependencies.
1 change: 0 additions & 1 deletion packages/@aws-cdk/aws-codedeploy-api/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
export * from './load-balancer';
32 changes: 0 additions & 32 deletions packages/@aws-cdk/aws-codedeploy-api/lib/load-balancer.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codedeploy-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@aws-cdk/aws-codedeploy-api",
"version": "0.31.0",
"description": "Load Balancer API for AWS CodeDeploy",
"deprecated": "This package is now obsololete, please remove it from your dependencies",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"jsii": {
Expand Down
16 changes: 9 additions & 7 deletions packages/@aws-cdk/aws-codedeploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,23 @@ const deploymentGroup = codedeploy.ServerDeploymentGroup.import(this, 'ExistingC
You can [specify a load balancer](https://docs.aws.amazon.com/codedeploy/latest/userguide/integrations-aws-elastic-load-balancing.html)
with the `loadBalancer` property when creating a Deployment Group.

`LoadBalancer` is an abstract class with static factory methods that allow you to create instances of it from various sources.

With Classic Elastic Load Balancer, you provide it directly:

```ts
import lb = require('@aws-cdk/aws-elasticloadbalancing');

const elb = new lb.LoadBalancer(this, 'ELB', {
// ...
// ...
});
elb.addTarget(/* ... */);
elb.addListener({
// ...
// ...
});

const deploymentGroup = new codedeploy.ServerDeploymentGroup(this, 'DeploymentGroup', {
loadBalancer: elb,
loadBalancer: codedeploy.LoadBalancer.classic(elb),
});
```

Expand All @@ -115,17 +117,17 @@ you provide a Target Group as the load balancer:
import lbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');

const alb = new lbv2.ApplicationLoadBalancer(this, 'ALB', {
// ...
// ...
});
const listener = alb.addListener('Listener', {
// ...
// ...
});
const targetGroup = listener.addTargets('Fleet', {
// ...
// ...
});

const deploymentGroup = new codedeploy.ServerDeploymentGroup(this, 'DeploymentGroup', {
loadBalancer: targetGroup,
loadBalancer: codedeploy.LoadBalancer.application(targetGroup),
});
```

Expand Down
22 changes: 10 additions & 12 deletions packages/@aws-cdk/aws-codedeploy/lib/server/deployment-group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import autoscaling = require('@aws-cdk/aws-autoscaling');
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
import codedeploylb = require('@aws-cdk/aws-codedeploy-api');
import ec2 = require('@aws-cdk/aws-ec2');
import iam = require('@aws-cdk/aws-iam');
import s3 = require('@aws-cdk/aws-s3');
Expand All @@ -10,6 +9,7 @@ import { AutoRollbackConfig } from '../rollback-config';
import { arnForDeploymentGroup, renderAlarmConfiguration, renderAutoRollbackConfiguration } from '../utils';
import { IServerApplication, ServerApplication } from './application';
import { IServerDeploymentConfig, ServerDeploymentConfig } from './deployment-config';
import { LoadBalancer, LoadBalancerGeneration } from './load-balancer';

export interface IServerDeploymentGroup extends cdk.IResource {
readonly application: IServerApplication;
Expand Down Expand Up @@ -190,12 +190,12 @@ export interface ServerDeploymentGroupProps {

/**
* The load balancer to place in front of this Deployment Group.
* Can be either a classic Elastic Load Balancer,
* Can be created from either a classic Elastic Load Balancer,
* or an Application Load Balancer / Network Load Balancer Target Group.
*
* @default the Deployment Group will not have a load balancer defined
*/
readonly loadBalancer?: codedeploylb.ILoadBalancer;
readonly loadBalancer?: LoadBalancer;

/**
* All EC2 instances matching the given set of tags when a deployment occurs will be added to this Deployment Group.
Expand Down Expand Up @@ -387,25 +387,23 @@ export class ServerDeploymentGroup extends ServerDeploymentGroupBase {
}
}

private loadBalancerInfo(lbProvider?: codedeploylb.ILoadBalancer):
private loadBalancerInfo(loadBalancer?: LoadBalancer):
CfnDeploymentGroup.LoadBalancerInfoProperty | undefined {
if (!lbProvider) {
if (!loadBalancer) {
return undefined;
}

const lb = lbProvider.asCodeDeployLoadBalancer();

switch (lb.generation) {
case codedeploylb.LoadBalancerGeneration.First:
switch (loadBalancer.generation) {
case LoadBalancerGeneration.FIRST:
return {
elbInfoList: [
{ name: lb.name },
{ name: loadBalancer.name },
],
};
case codedeploylb.LoadBalancerGeneration.Second:
case LoadBalancerGeneration.SECOND:
return {
targetGroupInfoList: [
{ name: lb.name },
{ name: loadBalancer.name },
]
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codedeploy/lib/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './application';
export * from './deployment-config';
export * from './deployment-group';
export * from './load-balancer';
90 changes: 90 additions & 0 deletions packages/@aws-cdk/aws-codedeploy/lib/server/load-balancer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import elb = require('@aws-cdk/aws-elasticloadbalancing');
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');

/**
* The generations of AWS load balancing solutions.
*/
export enum LoadBalancerGeneration {
/**
* The first generation (ELB Classic).
*/
FIRST = 0,

/**
* The second generation (ALB and NLB).
*/
SECOND = 1
}

/**
* An interface of an abstract load balancer, as needed by CodeDeploy.
* Create instances using the static factory methods:
* {@link #classic}, {@link #application} and {@link #network}.
*/
export abstract class LoadBalancer {
/**
* Creates a new CodeDeploy load balancer from a Classic ELB Load Balancer.
*
* @param loadBalancer a classic ELB Load Balancer
*/
public static classic(loadBalancer: elb.LoadBalancer): LoadBalancer {
return new ClassicLoadBalancer(loadBalancer);
}

/**
* Creates a new CodeDeploy load balancer from an Application Load Balancer Target Group.
*
* @param albTargetGroup an ALB Target Group
*/
public static application(albTargetGroup: elbv2.ApplicationTargetGroup): LoadBalancer {
return new AlbLoadBalancer(albTargetGroup);
}

/**
* Creates a new CodeDeploy load balancer from a Network Load Balancer Target Group.
*
* @param nlbTargetGroup an NLB Target Group
*/
public static network(nlbTargetGroup: elbv2.NetworkTargetGroup): LoadBalancer {
return new NlbLoadBalancer(nlbTargetGroup);
}

public abstract readonly generation: LoadBalancerGeneration;
public abstract readonly name: string;
}

class ClassicLoadBalancer extends LoadBalancer {
public readonly generation: LoadBalancerGeneration;
public readonly name: string;

constructor(loadBalancer: elb.LoadBalancer) {
super();

this.generation = LoadBalancerGeneration.FIRST;
this.name = loadBalancer.loadBalancerName;
}
}

class AlbLoadBalancer extends LoadBalancer {
public readonly generation: LoadBalancerGeneration;
public readonly name: string;

constructor(targetGroup: elbv2.ApplicationTargetGroup) {
super();

this.generation = LoadBalancerGeneration.SECOND;
this.name = targetGroup.targetGroupName;
}
}

class NlbLoadBalancer extends LoadBalancer {
public readonly generation: LoadBalancerGeneration;
public readonly name: string;

constructor(targetGroup: elbv2.NetworkTargetGroup) {
super();

this.generation = LoadBalancerGeneration.SECOND;
this.name = targetGroup.targetGroupName;
}
}
8 changes: 4 additions & 4 deletions packages/@aws-cdk/aws-codedeploy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
"devDependencies": {
"@aws-cdk/assert": "^0.31.0",
"@aws-cdk/aws-ec2": "^0.31.0",
"@aws-cdk/aws-elasticloadbalancing": "^0.31.0",
"@aws-cdk/aws-elasticloadbalancingv2": "^0.31.0",
"cdk-build-tools": "^0.31.0",
"cdk-integ-tools": "^0.31.0",
"cfn2ts": "^0.31.0",
Expand All @@ -73,7 +71,8 @@
"dependencies": {
"@aws-cdk/aws-autoscaling": "^0.31.0",
"@aws-cdk/aws-cloudwatch": "^0.31.0",
"@aws-cdk/aws-codedeploy-api": "^0.31.0",
"@aws-cdk/aws-elasticloadbalancing": "^0.31.0",
"@aws-cdk/aws-elasticloadbalancingv2": "^0.31.0",
"@aws-cdk/aws-iam": "^0.31.0",
"@aws-cdk/aws-lambda": "^0.31.0",
"@aws-cdk/aws-s3": "^0.31.0",
Expand All @@ -83,7 +82,8 @@
"peerDependencies": {
"@aws-cdk/aws-autoscaling": "^0.31.0",
"@aws-cdk/aws-cloudwatch": "^0.31.0",
"@aws-cdk/aws-codedeploy-api": "^0.31.0",
"@aws-cdk/aws-elasticloadbalancing": "^0.31.0",
"@aws-cdk/aws-elasticloadbalancingv2": "^0.31.0",
"@aws-cdk/aws-iam": "^0.31.0",
"@aws-cdk/aws-lambda": "^0.31.0",
"@aws-cdk/aws-s3": "^0.31.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ elb.addListener({
new codedeploy.ServerDeploymentGroup(stack, 'CodeDeployGroup', {
deploymentConfig: codedeploy.ServerDeploymentConfig.AllAtOnce,
autoScalingGroups: [asg],
loadBalancer: elb,
loadBalancer: codedeploy.LoadBalancer.classic(elb),
alarms: [
new cloudwatch.Alarm(stack, 'Alarm1', {
metric: new cloudwatch.Metric({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export = {
const targetGroup = listener.addTargets('Fleet', { protocol: lbv2.ApplicationProtocol.Http });

new codedeploy.ServerDeploymentGroup(stack, 'DeploymentGroup', {
loadBalancer: targetGroup,
loadBalancer: codedeploy.LoadBalancer.application(targetGroup),
});

expect(stack).to(haveResource('AWS::CodeDeploy::DeploymentGroup', {
Expand Down Expand Up @@ -135,7 +135,7 @@ export = {
const targetGroup = listener.addTargets('Fleet', { port: 80 });

new codedeploy.ServerDeploymentGroup(stack, 'DeploymentGroup', {
loadBalancer: targetGroup,
loadBalancer: codedeploy.LoadBalancer.network(targetGroup),
});

expect(stack).to(haveResource('AWS::CodeDeploy::DeploymentGroup', {
Expand Down
10 changes: 1 addition & 9 deletions packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import codedeploy = require('@aws-cdk/aws-codedeploy-api');
import {
AnyIPv4, Connections, IConnectable, IPortRange, ISecurityGroup,
IVpcNetwork, IVpcSubnet, SecurityGroup, TcpPort } from '@aws-cdk/aws-ec2';
Expand Down Expand Up @@ -188,7 +187,7 @@ export enum LoadBalancingProtocol {
*
* Routes to a fleet of of instances in a VPC.
*/
export class LoadBalancer extends Resource implements IConnectable, codedeploy.ILoadBalancer {
export class LoadBalancer extends Resource implements IConnectable {
/**
* Control all connections from and to this load balancer
*/
Expand Down Expand Up @@ -314,13 +313,6 @@ export class LoadBalancer extends Resource implements IConnectable, codedeploy.I
return this.elb.loadBalancerSourceSecurityGroupOwnerAlias;
}

public asCodeDeployLoadBalancer(): codedeploy.ILoadBalancerProps {
return {
generation: codedeploy.LoadBalancerGeneration.First,
name: this.loadBalancerName,
};
}

/**
* Allow connections to all existing targets on new instance port
*/
Expand Down
2 changes: 0 additions & 2 deletions packages/@aws-cdk/aws-elasticloadbalancing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@
"pkglint": "^0.31.0"
},
"dependencies": {
"@aws-cdk/aws-codedeploy-api": "^0.31.0",
"@aws-cdk/aws-ec2": "^0.31.0",
"@aws-cdk/cdk": "^0.31.0"
},
"homepage": "https://github.com/awslabs/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-codedeploy-api": "^0.31.0",
"@aws-cdk/aws-ec2": "^0.31.0",
"@aws-cdk/cdk": "^0.31.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import codedeploy = require('@aws-cdk/aws-codedeploy-api');
import ec2 = require('@aws-cdk/aws-ec2');
import cdk = require('@aws-cdk/cdk');
import { CfnTargetGroup } from '../elasticloadbalancingv2.generated';
Expand Down Expand Up @@ -131,7 +130,7 @@ export interface HealthCheck {
/**
* Define the target of a load balancer
*/
export abstract class TargetGroupBase extends cdk.Construct implements ITargetGroup, codedeploy.ILoadBalancer {
export abstract class TargetGroupBase extends cdk.Construct implements ITargetGroup {
/**
* The ARN of the target group
*/
Expand Down Expand Up @@ -275,13 +274,6 @@ export abstract class TargetGroupBase extends cdk.Construct implements ITargetGr
};
}

public asCodeDeployLoadBalancer(): codedeploy.ILoadBalancerProps {
return {
generation: codedeploy.LoadBalancerGeneration.Second,
name: this.targetGroupName,
};
}

/**
* Register the given load balancing target as part of this group
*/
Expand Down
Loading

0 comments on commit ad62333

Please sign in to comment.