Skip to content

Commit

Permalink
feat(codedeploy): loadbalancer support for imported Target Groups (#1…
Browse files Browse the repository at this point in the history
…7848)

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*
  • Loading branch information
eastNine committed Dec 20, 2021
1 parent 65da9e1 commit 32f1c80
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-codedeploy/lib/server/load-balancer.ts
Expand Up @@ -41,7 +41,7 @@ export abstract class LoadBalancer {
*
* @param albTargetGroup an ALB Target Group
*/
public static application(albTargetGroup: elbv2.ApplicationTargetGroup): LoadBalancer {
public static application(albTargetGroup: elbv2.IApplicationTargetGroup): LoadBalancer {
class AlbLoadBalancer extends LoadBalancer {
public readonly generation = LoadBalancerGeneration.SECOND;
public readonly name = albTargetGroup.targetGroupName;
Expand All @@ -55,7 +55,7 @@ export abstract class LoadBalancer {
*
* @param nlbTargetGroup an NLB Target Group
*/
public static network(nlbTargetGroup: elbv2.NetworkTargetGroup): LoadBalancer {
public static network(nlbTargetGroup: elbv2.INetworkTargetGroup): LoadBalancer {
class NlbLoadBalancer extends LoadBalancer {
public readonly generation = LoadBalancerGeneration.SECOND;
public readonly name = nlbTargetGroup.targetGroupName;
Expand Down
Expand Up @@ -412,4 +412,30 @@ describe('CodeDeploy Server Deployment Group', () => {

expect(() => SynthUtils.toCloudFormation(stack)).toThrow(/deploymentInAlarm/);
});

test('can be used with an imported ALB Target Group as the load balancer', () => {
const stack = new cdk.Stack();

new codedeploy.ServerDeploymentGroup(stack, 'DeploymentGroup', {
loadBalancer: codedeploy.LoadBalancer.application(
lbv2.ApplicationTargetGroup.fromTargetGroupAttributes(stack, 'importedAlbTg', {
targetGroupArn: 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myAlbTargetGroup/73e2d6bc24d8a067',
}),
),
});

expect(stack).toHaveResourceLike('AWS::CodeDeploy::DeploymentGroup', {
'LoadBalancerInfo': {
'TargetGroupInfoList': [
{
'Name': 'myAlbTargetGroup',
},
],
},
'DeploymentStyle': {
'DeploymentOption': 'WITH_TRAFFIC_CONTROL',
},
});
});

});
Expand Up @@ -376,6 +376,11 @@ export interface TargetGroupImportProps extends TargetGroupAttributes {
* A target group
*/
export interface ITargetGroup extends cdk.IConstruct {
/**
* The name of the target group
*/
readonly targetGroupName: string;

/**
* ARN of the target group
*/
Expand Down
Expand Up @@ -15,6 +15,11 @@ export abstract class ImportedTargetGroupBase extends CoreConstruct implements I
*/
public readonly targetGroupArn: string;

/**
* The name of the target group
*/
public readonly targetGroupName: string;

/**
* A token representing a list of ARNs of the load balancers that route traffic to this target group
*/
Expand All @@ -29,6 +34,7 @@ export abstract class ImportedTargetGroupBase extends CoreConstruct implements I
super(scope, id);

this.targetGroupArn = props.targetGroupArn;
this.targetGroupName = cdk.Stack.of(scope).splitArn(props.targetGroupArn, cdk.ArnFormat.SLASH_RESOURCE_SLASH_RESOURCE_NAME).resourceName!.split('/')[0];
this.loadBalancerArns = props.loadBalancerArns || cdk.Aws.NO_VALUE;
}
}
@@ -1,7 +1,7 @@
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { ApplicationProtocol, Protocol } from './enums';

export type Attributes = {[key: string]: string | undefined};
export type Attributes = { [key: string]: string | undefined };

/**
* Render an attribute dict to a list of { key, value } pairs
Expand Down
Expand Up @@ -55,7 +55,7 @@ describe('tests', () => {

// WHEN
const tg = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(stack, 'TG', {
targetGroupArn: 'arn',
targetGroupArn: 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myAlbTargetGroup/73e2d6bc24d8a067',
});
tg.addTarget(new FakeSelfRegisteringTarget(stack, 'Target', vpc));
});
Expand All @@ -65,7 +65,7 @@ describe('tests', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');
const tg = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(stack, 'TG', {
targetGroupArn: 'arn',
targetGroupArn: 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myAlbTargetGroup/73e2d6bc24d8a067',
});

// WHEN
Expand Down Expand Up @@ -497,4 +497,18 @@ describe('tests', () => {
app.synth();
}).toThrow('Healthcheck interval 1 minute must be greater than the timeout 2 minutes');
});

test('imported targetGroup has targetGroupName', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');

// WHEN
const importedTg = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(stack, 'importedTg', {
targetGroupArn: 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myAlbTargetGroup/73e2d6bc24d8a067',
});

// THEN
expect(importedTg.targetGroupName).toEqual('myAlbTargetGroup');
});
});
Expand Up @@ -499,4 +499,18 @@ describe('tests', () => {
expect(() => targetGroup.metricHealthyHostCount()).toThrow(/The TargetGroup needs to be attached to a LoadBalancer/);
expect(() => targetGroup.metricUnHealthyHostCount()).toThrow(/The TargetGroup needs to be attached to a LoadBalancer/);
});

test('imported targetGroup has targetGroupName', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');

// WHEN
const importedTg = elbv2.NetworkTargetGroup.fromTargetGroupAttributes(stack, 'importedTg', {
targetGroupArn: 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myNlbTargetGroup/73e2d6bc24d8a067',
});

// THEN
expect(importedTg.targetGroupName).toEqual('myNlbTargetGroup');
});
});

0 comments on commit 32f1c80

Please sign in to comment.