diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-target-group.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-target-group.ts index dc381c4529cb2..025d655aef181 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-target-group.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-target-group.ts @@ -145,7 +145,11 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat this.setAttribute('slow_start.duration_seconds', props.slowStart.toSeconds().toString()); } - this.enableCookieStickiness(props.stickinessCookieDuration, props.stickinessCookieName); + if (props.stickinessCookieDuration) { + this.enableCookieStickiness(props.stickinessCookieDuration, props.stickinessCookieName); + } else { + this.setAttribute('stickiness.enabled', 'false'); + } if (props.loadBalancingAlgorithmType) { this.setAttribute('load_balancing.algorithm.type', props.loadBalancingAlgorithmType); @@ -172,12 +176,7 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat * * @see https://docs.aws.amazon.com/elasticloadbalancing/latest/application/sticky-sessions.html */ - public enableCookieStickiness(duration?: Duration, cookieName?: string) { - if (typeof duration === 'undefined') { - this.setAttribute('stickiness.enabled', 'false'); - return; - } - + public enableCookieStickiness(duration: Duration, cookieName?: string) { if (duration.toSeconds() < 1 || duration.toSeconds() > 604800) { throw new Error('Stickiness cookie duration value must be between 1 second and 7 days (604800 seconds).'); }