From 02c09e976902986c48979ac1e0a2d43f919eebb1 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Mon, 11 Mar 2024 12:24:27 -0600 Subject: [PATCH 1/2] fix(elasticloadbalancingv2): allow alb slow start duration of 0 seconds --- .../aws-elasticloadbalancingv2/README.md | 20 +++++++++++++ .../lib/alb/application-target-group.ts | 5 ++-- .../test/alb/target-group.test.ts | 28 ++++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/packages/aws-cdk-lib/aws-elasticloadbalancingv2/README.md b/packages/aws-cdk-lib/aws-elasticloadbalancingv2/README.md index cc7d200abdf83..1104be9f2533f 100644 --- a/packages/aws-cdk-lib/aws-elasticloadbalancingv2/README.md +++ b/packages/aws-cdk-lib/aws-elasticloadbalancingv2/README.md @@ -335,6 +335,26 @@ const tg2 = new elbv2.ApplicationTargetGroup(this, 'TG2', { }); ``` +### Slow start mode for your Application Load Balancer + +By default, a target starts to receive its full share of requests as soon as it is registered with a target group and passes an initial health check. Using slow start mode gives targets time to warm up before the load balancer sends them a full share of requests. + +After you enable slow start for a target group, its targets enter slow start mode when they are considered healthy by the target group. A target in slow start mode exits slow start mode when the configured slow start duration period elapses or the target becomes unhealthy. The load balancer linearly increases the number of requests that it can send to a target in slow start mode. After a healthy target exits slow start mode, the load balancer can send it a full share of requests. + +The allowed range is 30-900 seconds (15 minutes). The default is 0 seconds (disabled). + +```ts +declare const vpc: ec2.Vpc; + +// Target group with slow start mode enabled +const tg = new elbv2.ApplicationTargetGroup(this, 'TG', { + targetType: elbv2.TargetType.INSTANCE, + port: 80, + slowStart: cdk.Duration.seconds(60), + vpc, +}); +``` + For more information see: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/sticky-sessions.html#application-based-stickiness ### Setting the target group protocol version diff --git a/packages/aws-cdk-lib/aws-elasticloadbalancingv2/lib/alb/application-target-group.ts b/packages/aws-cdk-lib/aws-elasticloadbalancingv2/lib/alb/application-target-group.ts index f5ed22b92379f..4f5f25568cd3d 100644 --- a/packages/aws-cdk-lib/aws-elasticloadbalancingv2/lib/alb/application-target-group.ts +++ b/packages/aws-cdk-lib/aws-elasticloadbalancingv2/lib/alb/application-target-group.ts @@ -330,8 +330,9 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat if (props) { if (props.slowStart !== undefined) { - if (props.slowStart.toSeconds() < 30 || props.slowStart.toSeconds() > 900) { - throw new Error('Slow start duration value must be between 30 and 900 seconds.'); + // 0 is allowed and disables slow start + if ((props.slowStart.toSeconds() < 30 && props.slowStart.toSeconds() !== 0) || props.slowStart.toSeconds() > 900) { + throw new Error('Slow start duration value must be between 30 and 900 seconds, or 0 to disable slow start.'); } this.setAttribute('slow_start.duration_seconds', props.slowStart.toSeconds().toString()); } diff --git a/packages/aws-cdk-lib/aws-elasticloadbalancingv2/test/alb/target-group.test.ts b/packages/aws-cdk-lib/aws-elasticloadbalancingv2/test/alb/target-group.test.ts index 7e30c3db10ecb..f94e514fa6746 100644 --- a/packages/aws-cdk-lib/aws-elasticloadbalancingv2/test/alb/target-group.test.ts +++ b/packages/aws-cdk-lib/aws-elasticloadbalancingv2/test/alb/target-group.test.ts @@ -340,7 +340,33 @@ describe('tests', () => { slowStart: badDuration, vpc, }); - }).toThrow(/Slow start duration value must be between 30 and 900 seconds./); + }).toThrow(/Slow start duration value must be between 30 and 900 seconds, or 0 to disable slow start./); + }); + }); + + test('Disable slow start by setting to 0 seconds', () => { + 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', { + slowStart: cdk.Duration.seconds(0), + vpc, + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::TargetGroup', { + TargetGroupAttributes: [ + { + Key: 'slow_start.duration_seconds', + Value: '0', + }, + { + Key: 'stickiness.enabled', + Value: 'false', + }, + ], }); }); From 6c65e80da5552dade33b32b65905145e1680524a Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Mon, 11 Mar 2024 13:49:54 -0600 Subject: [PATCH 2/2] fix readme --- packages/aws-cdk-lib/aws-elasticloadbalancingv2/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-elasticloadbalancingv2/README.md b/packages/aws-cdk-lib/aws-elasticloadbalancingv2/README.md index 1104be9f2533f..49835ea017f18 100644 --- a/packages/aws-cdk-lib/aws-elasticloadbalancingv2/README.md +++ b/packages/aws-cdk-lib/aws-elasticloadbalancingv2/README.md @@ -349,8 +349,8 @@ declare const vpc: ec2.Vpc; // Target group with slow start mode enabled const tg = new elbv2.ApplicationTargetGroup(this, 'TG', { targetType: elbv2.TargetType.INSTANCE, + slowStart: Duration.seconds(60), port: 80, - slowStart: cdk.Duration.seconds(60), vpc, }); ```