Skip to content

Commit 32f1c80

Browse files
authored
feat(codedeploy): loadbalancer support for imported Target Groups (#17848)
This PR fixes that imported alb and nlb target group be able to configure to loadBalancer property. Fixes #9677. example: ```TypeScript import * as cdk from '@aws-cdk/core'; import * as codedeploy from '@aws-cdk/aws-codedeploy'; import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2'; const deploymentGroup = new codedeploy.ServerDeploymentGroup(this, 'deploymentGroup', { ... // configurable imported application loadbalancer targetgroup loadBalancer: codedeploy.LoadBalancer.application( elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(this, 'importedAlbTg', { targetGroupArn: 'arn:aws:elasticloadbalancing:ap-northeast-2:111111111111:targetgroup/myAlbTargetgroup/abcd12345678efgf' }) ), // also network loadbalancer targetgroup loadBalancer: codedeploy.LoadBalancer.network( elbv2.NetworkTargetGroup.fromTargetGroupAttributes(this, 'importedNlbTg', { targetGroupArn: 'arn:aws:elasticloadbalancing:ap-northeast-2:111111111111:targetgroup/myNlbTargetgroup/wxyz09876543opqr' }) ), }); ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 65da9e1 commit 32f1c80

File tree

7 files changed

+70
-5
lines changed

7 files changed

+70
-5
lines changed

packages/@aws-cdk/aws-codedeploy/lib/server/load-balancer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export abstract class LoadBalancer {
4141
*
4242
* @param albTargetGroup an ALB Target Group
4343
*/
44-
public static application(albTargetGroup: elbv2.ApplicationTargetGroup): LoadBalancer {
44+
public static application(albTargetGroup: elbv2.IApplicationTargetGroup): LoadBalancer {
4545
class AlbLoadBalancer extends LoadBalancer {
4646
public readonly generation = LoadBalancerGeneration.SECOND;
4747
public readonly name = albTargetGroup.targetGroupName;
@@ -55,7 +55,7 @@ export abstract class LoadBalancer {
5555
*
5656
* @param nlbTargetGroup an NLB Target Group
5757
*/
58-
public static network(nlbTargetGroup: elbv2.NetworkTargetGroup): LoadBalancer {
58+
public static network(nlbTargetGroup: elbv2.INetworkTargetGroup): LoadBalancer {
5959
class NlbLoadBalancer extends LoadBalancer {
6060
public readonly generation = LoadBalancerGeneration.SECOND;
6161
public readonly name = nlbTargetGroup.targetGroupName;

packages/@aws-cdk/aws-codedeploy/test/server/deployment-group.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,30 @@ describe('CodeDeploy Server Deployment Group', () => {
412412

413413
expect(() => SynthUtils.toCloudFormation(stack)).toThrow(/deploymentInAlarm/);
414414
});
415+
416+
test('can be used with an imported ALB Target Group as the load balancer', () => {
417+
const stack = new cdk.Stack();
418+
419+
new codedeploy.ServerDeploymentGroup(stack, 'DeploymentGroup', {
420+
loadBalancer: codedeploy.LoadBalancer.application(
421+
lbv2.ApplicationTargetGroup.fromTargetGroupAttributes(stack, 'importedAlbTg', {
422+
targetGroupArn: 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myAlbTargetGroup/73e2d6bc24d8a067',
423+
}),
424+
),
425+
});
426+
427+
expect(stack).toHaveResourceLike('AWS::CodeDeploy::DeploymentGroup', {
428+
'LoadBalancerInfo': {
429+
'TargetGroupInfoList': [
430+
{
431+
'Name': 'myAlbTargetGroup',
432+
},
433+
],
434+
},
435+
'DeploymentStyle': {
436+
'DeploymentOption': 'WITH_TRAFFIC_CONTROL',
437+
},
438+
});
439+
});
440+
415441
});

packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/base-target-group.ts

+5
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ export interface TargetGroupImportProps extends TargetGroupAttributes {
376376
* A target group
377377
*/
378378
export interface ITargetGroup extends cdk.IConstruct {
379+
/**
380+
* The name of the target group
381+
*/
382+
readonly targetGroupName: string;
383+
379384
/**
380385
* ARN of the target group
381386
*/

packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/imported.ts

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export abstract class ImportedTargetGroupBase extends CoreConstruct implements I
1515
*/
1616
public readonly targetGroupArn: string;
1717

18+
/**
19+
* The name of the target group
20+
*/
21+
public readonly targetGroupName: string;
22+
1823
/**
1924
* A token representing a list of ARNs of the load balancers that route traffic to this target group
2025
*/
@@ -29,6 +34,7 @@ export abstract class ImportedTargetGroupBase extends CoreConstruct implements I
2934
super(scope, id);
3035

3136
this.targetGroupArn = props.targetGroupArn;
37+
this.targetGroupName = cdk.Stack.of(scope).splitArn(props.targetGroupArn, cdk.ArnFormat.SLASH_RESOURCE_SLASH_RESOURCE_NAME).resourceName!.split('/')[0];
3238
this.loadBalancerArns = props.loadBalancerArns || cdk.Aws.NO_VALUE;
3339
}
3440
}

packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
22
import { ApplicationProtocol, Protocol } from './enums';
33

4-
export type Attributes = {[key: string]: string | undefined};
4+
export type Attributes = { [key: string]: string | undefined };
55

66
/**
77
* Render an attribute dict to a list of { key, value } pairs

packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/target-group.test.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('tests', () => {
5555

5656
// WHEN
5757
const tg = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(stack, 'TG', {
58-
targetGroupArn: 'arn',
58+
targetGroupArn: 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myAlbTargetGroup/73e2d6bc24d8a067',
5959
});
6060
tg.addTarget(new FakeSelfRegisteringTarget(stack, 'Target', vpc));
6161
});
@@ -65,7 +65,7 @@ describe('tests', () => {
6565
const app = new cdk.App();
6666
const stack = new cdk.Stack(app, 'Stack');
6767
const tg = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(stack, 'TG', {
68-
targetGroupArn: 'arn',
68+
targetGroupArn: 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myAlbTargetGroup/73e2d6bc24d8a067',
6969
});
7070

7171
// WHEN
@@ -497,4 +497,18 @@ describe('tests', () => {
497497
app.synth();
498498
}).toThrow('Healthcheck interval 1 minute must be greater than the timeout 2 minutes');
499499
});
500+
501+
test('imported targetGroup has targetGroupName', () => {
502+
// GIVEN
503+
const app = new cdk.App();
504+
const stack = new cdk.Stack(app, 'Stack');
505+
506+
// WHEN
507+
const importedTg = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(stack, 'importedTg', {
508+
targetGroupArn: 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myAlbTargetGroup/73e2d6bc24d8a067',
509+
});
510+
511+
// THEN
512+
expect(importedTg.targetGroupName).toEqual('myAlbTargetGroup');
513+
});
500514
});

packages/@aws-cdk/aws-elasticloadbalancingv2/test/nlb/target-group.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -499,4 +499,18 @@ describe('tests', () => {
499499
expect(() => targetGroup.metricHealthyHostCount()).toThrow(/The TargetGroup needs to be attached to a LoadBalancer/);
500500
expect(() => targetGroup.metricUnHealthyHostCount()).toThrow(/The TargetGroup needs to be attached to a LoadBalancer/);
501501
});
502+
503+
test('imported targetGroup has targetGroupName', () => {
504+
// GIVEN
505+
const app = new cdk.App();
506+
const stack = new cdk.Stack(app, 'Stack');
507+
508+
// WHEN
509+
const importedTg = elbv2.NetworkTargetGroup.fromTargetGroupAttributes(stack, 'importedTg', {
510+
targetGroupArn: 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myNlbTargetGroup/73e2d6bc24d8a067',
511+
});
512+
513+
// THEN
514+
expect(importedTg.targetGroupName).toEqual('myNlbTargetGroup');
515+
});
502516
});

0 commit comments

Comments
 (0)