From 7f8abd2965ebe940f608b0607663d5112f6118cd Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Wed, 26 Sep 2018 16:42:31 -0700 Subject: [PATCH] Add ALB and NLB as CodeDeploy load balancers. --- packages/@aws-cdk/aws-codedeploy/README.md | 43 +++++++++++++ .../aws-codedeploy/lib/deployment-group.ts | 8 ++- packages/@aws-cdk/aws-codedeploy/package.json | 1 + .../test/test.deployment-group.ts | 63 +++++++++++++++++++ .../lib/shared/base-target-group.ts | 11 +++- .../aws-elasticloadbalancingv2/package.json | 1 + 6 files changed, 124 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-codedeploy/README.md b/packages/@aws-cdk/aws-codedeploy/README.md index 601829edab23b..e1aa823615578 100644 --- a/packages/@aws-cdk/aws-codedeploy/README.md +++ b/packages/@aws-cdk/aws-codedeploy/README.md @@ -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: diff --git a/packages/@aws-cdk/aws-codedeploy/lib/deployment-group.ts b/packages/@aws-cdk/aws-codedeploy/lib/deployment-group.ts index 5b1c4a914389e..9a2c19c070c15 100644 --- a/packages/@aws-cdk/aws-codedeploy/lib/deployment-group.ts +++ b/packages/@aws-cdk/aws-codedeploy/lib/deployment-group.ts @@ -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() }, + ] + }; } } } diff --git a/packages/@aws-cdk/aws-codedeploy/package.json b/packages/@aws-cdk/aws-codedeploy/package.json index 96db8e2c610b7..f278de21c7bdc 100644 --- a/packages/@aws-cdk/aws-codedeploy/package.json +++ b/packages/@aws-cdk/aws-codedeploy/package.json @@ -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", diff --git a/packages/@aws-cdk/aws-codedeploy/test/test.deployment-group.ts b/packages/@aws-cdk/aws-codedeploy/test/test.deployment-group.ts index 99b9dc2e4411d..c490f75bfd749 100644 --- a/packages/@aws-cdk/aws-codedeploy/test/test.deployment-group.ts +++ b/packages/@aws-cdk/aws-codedeploy/test/test.deployment-group.ts @@ -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'); @@ -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(); + }, }, }; diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/base-target-group.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/base-target-group.ts index 3cf944ef54f0b..c75035ff2f3e0 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/base-target-group.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/base-target-group.ts @@ -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'; @@ -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 */ @@ -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 */ diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/package.json b/packages/@aws-cdk/aws-elasticloadbalancingv2/package.json index 1886040b02a45..7593bafcc80cb 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/package.json +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/package.json @@ -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" },