Skip to content

Commit

Permalink
feat(aws-elbv2): add support for enabling anomaly mitigation for targ…
Browse files Browse the repository at this point in the history
…et groups
  • Loading branch information
pag-tractive authored and gillesbergerp committed Apr 26, 2024
1 parent 66de1e1 commit 0064829
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
Expand Up @@ -932,6 +932,13 @@ export interface AddApplicationTargetsProps extends AddRuleProps {
*/
readonly loadBalancingAlgorithmType?: TargetGroupLoadBalancingAlgorithmType;

/**
* Indicates whether anomaly mitigation is enabled.
*
* @default false
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticloadbalancingv2-targetgroup-targetgroupattribute.html
*/
readonly loadBalancingAlgorithmAnomalyDetection?: boolean;
}

/**
Expand Down
Expand Up @@ -85,6 +85,14 @@ export interface ApplicationTargetGroupProps extends BaseTargetGroupProps {
*/
readonly loadBalancingAlgorithmType?: TargetGroupLoadBalancingAlgorithmType;

/**
* Indicates whether anomaly mitigation is enabled.
*
* @default false
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticloadbalancingv2-targetgroup-targetgroupattribute.html
*/
readonly loadBalancingAlgorithmAnomalyDetection?: boolean;

/**
* The targets to add to this target group.
*
Expand Down Expand Up @@ -346,6 +354,15 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat
if (props.loadBalancingAlgorithmType) {
this.setAttribute('load_balancing.algorithm.type', props.loadBalancingAlgorithmType);
}

if (props.loadBalancingAlgorithmAnomalyDetection) {
if (props.loadBalancingAlgorithmType != TargetGroupLoadBalancingAlgorithmType.WEIGHTED_RANDOM) {
throw new Error('Anomaly mitigation is only supported for the weighted_random load balancing algorithm.');
}

this.setAttribute('load_balancing.algorithm.anomaly_mitigation', 'on');
}

this.addTarget(...(props.targets || []));
}
}
Expand Down
Expand Up @@ -1088,6 +1088,54 @@ describe('tests', () => {
});
});

test('Enable anomaly mitigation', () => {
// 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)],
loadBalancingAlgorithmAnomalyDetection: true,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::TargetGroup', {
TargetGroupAttributes: [
{
Key: 'stickiness.enabled',
Value: 'false',
},
{
Key: 'load_balancing.algorithm.anomaly_detection',
Value: 'on',
},
],
});
});

test('Throw when trying to enable anomaly mitigation with an unsupported load balancing algorithm', () => {
// 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 });

[elbv2.TargetGroupLoadBalancingAlgorithmType.LEAST_OUTSTANDING_REQUESTS, elbv2.TargetGroupLoadBalancingAlgorithmType.ROUND_ROBIN].forEach((loadBalancingAlgorithmType) => {
// THEN
expect(() => {
listener.addTargets('Group', {
port: 80,
targets: [new FakeSelfRegisteringTarget(stack, 'Target', vpc)],
loadBalancingAlgorithmAnomalyDetection: true,
});
}).toThrow(/Anomaly mitigation is only supported for the weighted_random load balancing algorithm./);
});
});

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

test('status code', () => {
Expand Down
Expand Up @@ -268,6 +268,51 @@ describe('tests', () => {
});
});

test('Enable anomaly mitigation', () => {
// 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', {
loadBalancingAlgorithmAnomalyDetection: true,
vpc,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::TargetGroup', {
TargetGroupAttributes: [
{
Key: 'stickiness.enabled',
Value: 'false',
},
{
Key: 'load_balancing.algorithm.anomaly_mitigation',
Value: 'on',
},
],
});
});

test('Throw when trying to enable anomaly mitigation with an unsupported load balancing algorithm', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');
const vpc = new ec2.Vpc(stack, 'VPC', {});

[elbv2.TargetGroupLoadBalancingAlgorithmType.LEAST_OUTSTANDING_REQUESTS, elbv2.TargetGroupLoadBalancingAlgorithmType.ROUND_ROBIN].forEach((loadBalancingAlgorithmType) => {
// THEN
expect(() => {
new elbv2.ApplicationTargetGroup(stack, 'TargetGroup', {
loadBalancingAlgorithmAnomalyDetection: true,
loadBalancingAlgorithmType,
vpc,
});
}).toThrow(/Anomaly mitigation is only supported for the weighted_random load balancing algorithm./);
});
});

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

0 comments on commit 0064829

Please sign in to comment.