Skip to content

Commit

Permalink
feat(aws-elbv2): ALB target group routing algorithms (aws#15622)
Browse files Browse the repository at this point in the history
Closes aws#15160
  • Loading branch information
nohack authored and hollanddd committed Aug 26, 2021
1 parent 45719b5 commit a670d4e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { BaseListener, BaseListenerLookupOptions } from '../shared/base-listener';
import { HealthCheck } from '../shared/base-target-group';
import { ApplicationProtocol, ApplicationProtocolVersion, IpAddressType, SslPolicy } from '../shared/enums';
import { ApplicationProtocol, ApplicationProtocolVersion, TargetGroupLoadBalancingAlgorithmType, IpAddressType, SslPolicy } from '../shared/enums';
import { IListenerCertificate, ListenerCertificate } from '../shared/listener-certificate';
import { determineProtocolAndPort } from '../shared/util';
import { ListenerAction } from './application-listener-action';
Expand Down Expand Up @@ -367,6 +367,7 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
slowStart: props.slowStart,
stickinessCookieDuration: props.stickinessCookieDuration,
stickinessCookieName: props.stickinessCookieName,
loadBalancingAlgorithmType: props.loadBalancingAlgorithmType,
targetGroupName: props.targetGroupName,
targets: props.targets,
vpc: this.loadBalancer.vpc,
Expand Down Expand Up @@ -887,6 +888,14 @@ export interface AddApplicationTargetsProps extends AddRuleProps {
* @default No health check
*/
readonly healthCheck?: HealthCheck;

/**
* The load balancing algorithm to select targets for routing requests.
*
* @default round_robin.
*/
readonly loadBalancingAlgorithmType?: TargetGroupLoadBalancingAlgorithmType;

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
BaseTargetGroupProps, ITargetGroup, loadBalancerNameFromListenerArn, LoadBalancerTargetProps,
TargetGroupAttributes, TargetGroupBase, TargetGroupImportProps,
} from '../shared/base-target-group';
import { ApplicationProtocol, ApplicationProtocolVersion, Protocol, TargetType } from '../shared/enums';
import { ApplicationProtocol, ApplicationProtocolVersion, Protocol, TargetType, TargetGroupLoadBalancingAlgorithmType } from '../shared/enums';
import { ImportedTargetGroupBase } from '../shared/imported';
import { determineProtocolAndPort } from '../shared/util';
import { IApplicationListener } from './application-listener';
Expand Down Expand Up @@ -78,6 +78,13 @@ export interface ApplicationTargetGroupProps extends BaseTargetGroupProps {
*/
readonly stickinessCookieName?: string;

/**
* The load balancing algorithm to select targets for routing requests.
*
* @default TargetGroupLoadBalancingAlgorithmType.ROUND_ROBIN
*/
readonly loadBalancingAlgorithmType?: TargetGroupLoadBalancingAlgorithmType;

/**
* The targets to add to this target group.
*
Expand Down Expand Up @@ -140,6 +147,9 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat
if (props.stickinessCookieDuration) {
this.enableCookieStickiness(props.stickinessCookieDuration, props.stickinessCookieName);
}
if (props.loadBalancingAlgorithmType) {
this.setAttribute('load_balancing.algorithm.type', props.loadBalancingAlgorithmType);
}
this.addTarget(...(props.targets || []));
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,18 @@ export enum TargetType {
*/
LAMBDA = 'lambda',
}

/**
* Load balancing algorithmm type for target groups
*/
export enum TargetGroupLoadBalancingAlgorithmType {
/**
* round_robin
*/
ROUND_ROBIN = 'round_robin',

/**
* least_outstanding_requests
*/
LEAST_OUTSTANDING_REQUESTS = 'least_outstanding_requests',
}
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,31 @@ describe('tests', () => {
});
});

test('Custom Load balancer algorithm type', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', { vpc });
const listener = lb.addListener('Listener', { port: 80 });

// WHEN
listener.addTargets('Group', {
port: 80,
targets: [new FakeSelfRegisteringTarget(stack, 'Target', vpc)],
loadBalancingAlgorithmType: elbv2.TargetGroupLoadBalancingAlgorithmType.LEAST_OUTSTANDING_REQUESTS,
});

// THEN
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
TargetGroupAttributes: [
{
Key: 'load_balancing.algorithm.type',
Value: 'least_outstanding_requests',
},
],
});
});

describe('Throws with bad fixed responses', () => {

test('status code', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,29 @@ describe('tests', () => {
});
});

test('Custom Load balancer algorithm type', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');
const vpc = new ec2.Vpc(stack, 'VPC', {});

// WHEN
new elbv2.ApplicationTargetGroup(stack, 'TargetGroup', {
loadBalancingAlgorithmType: elbv2.TargetGroupLoadBalancingAlgorithmType.LEAST_OUTSTANDING_REQUESTS,
vpc,
});

// THEN
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
TargetGroupAttributes: [
{
Key: 'load_balancing.algorithm.type',
Value: 'least_outstanding_requests',
},
],
});
});

test('Can set a protocol version', () => {
// GIVEN
const app = new cdk.App();
Expand Down

0 comments on commit a670d4e

Please sign in to comment.