Skip to content

Commit 25de639

Browse files
committed
fix bakcwards compatibility issue with imported lb
1 parent 527c8d5 commit 25de639

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

packages/aws-cdk-lib/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -532,24 +532,22 @@ export abstract class ApplicationLoadBalancedServiceBase extends Construct {
532532
}
533533

534534
if (props.redirectHTTP) {
535-
// Check if the load balancer was created by this construct
535+
this.redirectListener = loadBalancer.addListener('PublicRedirectListener', {
536+
protocol: ApplicationProtocol.HTTP,
537+
port: 80,
538+
open: props.openListener ?? true,
539+
defaultAction: ListenerAction.redirect({
540+
protocol: ApplicationProtocol.HTTPS,
541+
port: props.listenerPort?.toString() || '443',
542+
permanent: true,
543+
}),
544+
});
545+
546+
// Ensure the redirect listener is created after the main listener
547+
this.redirectListener.node.addDependency(this.listener);
548+
549+
// Validate that there are no conflicting port 80 listeners during synth for owned load balancers.
536550
if (loadBalancer instanceof ApplicationLoadBalancer) {
537-
// Create a new HTTP listener on port 80 that redirects to HTTPS
538-
this.redirectListener = loadBalancer.addListener('PublicRedirectListener', {
539-
protocol: ApplicationProtocol.HTTP,
540-
port: 80,
541-
open: props.openListener ?? true,
542-
defaultAction: ListenerAction.redirect({
543-
protocol: ApplicationProtocol.HTTPS,
544-
port: props.listenerPort?.toString() || '443',
545-
permanent: true,
546-
}),
547-
});
548-
549-
// Ensure the redirect listener is created after the main listener
550-
this.redirectListener.node.addDependency(this.listener);
551-
552-
// Validate that there are no conflicting port 80 listeners during synth
553551
loadBalancer.node.addValidation({
554552
validate: () => {
555553
const port80Listeners = loadBalancer.listeners.filter(listener =>
@@ -565,7 +563,7 @@ export abstract class ApplicationLoadBalancedServiceBase extends Construct {
565563
},
566564
});
567565
} else {
568-
// If the ALB was imported then we cannot reliably add the redirect action as we can't find the port 80 listener.
566+
// If the ALB was imported then we cannot reliably add or validate the redirect action as we can't access the existing listeners.
569567
Annotations.of(this).addWarning(
570568
'Cannot automatically configure port 80 HTTP redirect with redirectHTTP: ' +
571569
'The construct cannot reliably determine if a port 80 listener already exists. ' +

packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ describe('ApplicationLoadBalancedFargateService', () => {
12201220
}).toThrow('Validation failed with the following errors:\n [Default/ALB] Cannot automatically configure redirectHTTP: A listener already exists on port 80.');
12211221
});
12221222

1223-
test('adds warning for imported load balancers', () => {
1223+
test('adds warning for imported load balancers with redirectHTTP and creates the redirect listener', () => {
12241224
// GIVEN
12251225
const stack = new cdk.Stack();
12261226
const vpc = new ec2.Vpc(stack, 'VPC');
@@ -1253,6 +1253,31 @@ describe('ApplicationLoadBalancedFargateService', () => {
12531253
// Verify that a warning is added
12541254
const annotations = Annotations.fromStack(stack);
12551255
annotations.hasWarning('/Default/Service', 'Cannot automatically configure port 80 HTTP redirect with redirectHTTP: The construct cannot reliably determine if a port 80 listener already exists. Please configure the redirect manually on the port 80 listener.');
1256+
1257+
// Verify that we have two listeners - one for HTTPS and one for HTTP redirect
1258+
Template.fromStack(stack).resourceCountIs('AWS::ElasticLoadBalancingV2::Listener', 2);
1259+
1260+
// Verify the HTTPS listener is on port 8443
1261+
Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::Listener', {
1262+
Port: 443,
1263+
Protocol: 'HTTPS',
1264+
});
1265+
1266+
// Verify the HTTP redirect listener is on port 80
1267+
Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::Listener', {
1268+
Port: 80,
1269+
Protocol: 'HTTP',
1270+
DefaultActions: [
1271+
Match.objectLike({
1272+
Type: 'redirect',
1273+
RedirectConfig: {
1274+
Protocol: 'HTTPS',
1275+
Port: '443',
1276+
StatusCode: 'HTTP_301',
1277+
},
1278+
}),
1279+
],
1280+
});
12561281
});
12571282

12581283
test('errors when setting HTTPS protocol but not domain name', () => {

0 commit comments

Comments
 (0)