@@ -114,29 +114,31 @@ export class BackendStack extends cdk.Stack {
114114 ) ;
115115
116116 // 3. HTTP 80 Listener
117- alb . addListener ( `${ appName } HttpListener-${ props . environment } ` , {
117+ const httpListener = alb . addListener ( `${ appName } HttpListener-${ props . environment } ` , {
118118 port : 80 ,
119119 protocol : elbv2 . ApplicationProtocol . HTTP ,
120- defaultAction : elbv2 . ListenerAction . redirect ( {
121- protocol : elbv2 . ApplicationProtocol . HTTPS ,
122- } ) ,
120+ defaultAction : elbv2 . ListenerAction . forward ( [ targetGroup ] ) ,
123121 } ) ;
124122
125- // 4. Create Fargate Service
123+ // 4. Create a security group for the Fargate service
124+ const serviceSecurityGroup = new ec2 . SecurityGroup ( this , `${ appName } ServiceSG-${ props . environment } ` , {
125+ vpc,
126+ allowAllOutbound : true ,
127+ } ) ;
128+
129+ // 5. Create the Fargate service WITHOUT registering it with the target group yet
126130 const fargateService = new ecs . FargateService ( this , `${ appName } Service-${ props . environment } ` , {
127131 cluster,
128132 taskDefinition,
129133 desiredCount : isProd ? 2 : 1 ,
130134 assignPublicIp : false ,
131- securityGroups : [
132- new ec2 . SecurityGroup ( this , `${ appName } ServiceSG-${ props . environment } ` , {
133- vpc,
134- allowAllOutbound : true ,
135- } ) ,
136- ] ,
135+ securityGroups : [ serviceSecurityGroup ] ,
137136 } ) ;
138137
139- // 5. Register the service with the Target Group
138+ // 6. Add explicit dependency to ensure the listener exists before the service
139+ fargateService . node . addDependency ( httpListener ) ;
140+
141+ // 7. Now register the service with the target group
140142 targetGroup . addTarget ( fargateService ) ;
141143
142144 // Create a Cognito User Pool Client for the ALB
0 commit comments