Skip to content

Commit

Permalink
Add ALB and NLB as CodeDeploy load balancers.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Sep 26, 2018
1 parent 745a81f commit 7f8abd2
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 3 deletions.
43 changes: 43 additions & 0 deletions packages/@aws-cdk/aws-codedeploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,49 @@ const deploymentGroup = codedeploy.ServerDeploymentGroupRef.import(this, 'Existi
});
```

#### Load balancers

You can specify a load balancer with the `loadBalancer` property when creating a Deployment Group.

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,
});
```

With Application Load Balancer or Network Load Balancer,
you provide a Target Group as the load balancer:

```ts
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,
});
```

### Deployment Configurations

You can also pass a Deployment Configuration when creating the Deployment Group:
Expand Down
8 changes: 6 additions & 2 deletions packages/@aws-cdk/aws-codedeploy/lib/deployment-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,15 @@ export class ServerDeploymentGroup extends ServerDeploymentGroupRef {
case codedeploylb.LoadBalancerGeneration.FIRST:
return {
elbInfoList: [
{ name : lbProvider.codeDeployLoadBalancerName() },
{ name: lbProvider.codeDeployLoadBalancerName() },
],
};
case codedeploylb.LoadBalancerGeneration.SECOND:
throw new Error('not implemented yet');
return {
targetGroupInfoList: [
{ name: lbProvider.codeDeployLoadBalancerName() },
]
};
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codedeploy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@aws-cdk/assert": "^0.9.2",
"@aws-cdk/aws-ec2": "^0.9.2",
"@aws-cdk/aws-elasticloadbalancing": "^0.9.2",
"@aws-cdk/aws-elasticloadbalancingv2": "^0.9.2",
"cdk-build-tools": "^0.9.2",
"cdk-integ-tools": "^0.9.2",
"cfn2ts": "^0.9.2",
Expand Down
63 changes: 63 additions & 0 deletions packages/@aws-cdk/aws-codedeploy/test/test.deployment-group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, haveResource } from '@aws-cdk/assert';
import autoscaling = require('@aws-cdk/aws-autoscaling');
import ec2 = require('@aws-cdk/aws-ec2');
import lbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
import cdk = require('@aws-cdk/cdk');
import { Test } from 'nodeunit';
import codedeploy = require('../lib');
Expand Down Expand Up @@ -88,5 +89,67 @@ export = {

test.done();
},

'can be created with an ALB Target Group as the load balancer'(test: Test) {
const stack = new cdk.Stack();

const alb = new lbv2.ApplicationLoadBalancer(stack, 'ALB', {
vpc: new ec2.VpcNetwork(stack, 'VPC'),
});
const listener = alb.addListener('Listener', { protocol: lbv2.ApplicationProtocol.Http });
const targetGroup = listener.addTargets('Fleet', { protocol: lbv2.ApplicationProtocol.Http });

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

expect(stack).to(haveResource('AWS::CodeDeploy::DeploymentGroup', {
"LoadBalancerInfo": {
"TargetGroupInfoList": [
{
"Name": {
"Fn::GetAtt": [
"ALBListenerFleetGroup008CEEE4",
"TargetGroupName"
]
},
}
]
}
}));

test.done();
},

'can be created with an NLB Target Group as the load balancer'(test: Test) {
const stack = new cdk.Stack();

const alb = new lbv2.NetworkLoadBalancer(stack, 'NLB', {
vpc: new ec2.VpcNetwork(stack, 'VPC'),
});
const listener = alb.addListener('Listener', { port: 80 });
const targetGroup = listener.addTargets('Fleet', { port: 80 });

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

expect(stack).to(haveResource('AWS::CodeDeploy::DeploymentGroup', {
"LoadBalancerInfo": {
"TargetGroupInfoList": [
{
"Name": {
"Fn::GetAtt": [
"NLBListenerFleetGroupB882EC86",
"TargetGroupName"
]
},
}
]
}
}));

test.done();
},
},
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import codedeploy = require('@aws-cdk/aws-codedeploy-api');
import ec2 = require('@aws-cdk/aws-ec2');
import cdk = require('@aws-cdk/cdk');
import { cloudformation } from '../elasticloadbalancingv2.generated';
Expand Down Expand Up @@ -119,7 +120,7 @@ export interface HealthCheck {
/**
* Define the target of a load balancer
*/
export abstract class BaseTargetGroup extends cdk.Construct implements ITargetGroup {
export abstract class BaseTargetGroup extends cdk.Construct implements ITargetGroup, codedeploy.LoadBalancerProvider {
/**
* The ARN of the target group
*/
Expand Down Expand Up @@ -234,6 +235,14 @@ export abstract class BaseTargetGroup extends cdk.Construct implements ITargetGr
this.resource.addDependency(...other);
}

public codeDeployLoadBalancerGeneration(): codedeploy.LoadBalancerGeneration {
return codedeploy.LoadBalancerGeneration.SECOND;
}

public codeDeployLoadBalancerName(): string {
return this.targetGroupName;
}

/**
* Register the given load balancing target as part of this group
*/
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-elasticloadbalancingv2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
},
"dependencies": {
"@aws-cdk/cdk": "^0.9.2",
"@aws-cdk/aws-codedeploy-api": "^0.9.2",
"@aws-cdk/aws-ec2": "^0.9.2",
"@aws-cdk/aws-s3": "^0.9.2"
},
Expand Down

0 comments on commit 7f8abd2

Please sign in to comment.