@@ -222,60 +222,89 @@ def __init__(
222222 cluster : ecs .Cluster ,
223223 props : ServiceProps ,
224224 load_balancer : elbv2 .ApplicationLoadBalancer ,
225- certificate_arn : str ,
225+ certificate_arn : str = None ,
226226 health_check_path : str = "/" ,
227227 health_check_interval : int = 1 , # max is 5
228+ enable_https : bool = False ,
228229 ** kwargs ,
229230 ) -> None :
230231 super ().__init__ (scope , construct_id , vpc , cluster , props , ** kwargs )
231232
232- # -------------------
233- # ACM Certificate for HTTPS
234- # -------------------
235- self .cert = acm .Certificate .from_certificate_arn (
236- self , "Cert" , certificate_arn = certificate_arn
237- )
233+ if enable_https and not certificate_arn :
234+ raise ValueError (
235+ "enable_https is True but certificate_arn is None. "
236+ "Please provide a valid certificate ARN to enable HTTPS."
237+ )
238+ if enable_https and certificate_arn :
239+ # -------------------
240+ # ACM Certificate for HTTPS
241+ # -------------------
242+ self .cert = acm .Certificate .from_certificate_arn (
243+ self , "Cert" , certificate_arn = certificate_arn
244+ )
238245
239- # -------------------------------
240- # Setup https
241- # -------------------------------
242- https_listener = elbv2 .ApplicationListener (
243- self ,
244- "HttpsListener" ,
245- load_balancer = load_balancer ,
246- port = ALB_HTTPS_LISTENER_PORT ,
247- open = True ,
248- protocol = elbv2 .ApplicationProtocol .HTTPS ,
249- certificates = [self .cert ],
250- )
246+ # -------------------------------
247+ # Setup https
248+ # -------------------------------
249+ https_listener = elbv2 .ApplicationListener (
250+ self ,
251+ "HttpsListener" ,
252+ load_balancer = load_balancer ,
253+ port = ALB_HTTPS_LISTENER_PORT ,
254+ open = True ,
255+ protocol = elbv2 .ApplicationProtocol .HTTPS ,
256+ certificates = [self .cert ],
257+ )
251258
252- https_listener .add_targets (
253- "HttpsTarget" ,
254- port = props .container_port ,
255- protocol = elbv2 .ApplicationProtocol .HTTP ,
256- targets = [self .service ],
257- health_check = elbv2 .HealthCheck (
258- path = health_check_path , interval = duration .minutes (health_check_interval )
259- ),
260- )
259+ https_listener .add_targets (
260+ "HttpsTarget" ,
261+ port = props .container_port ,
262+ protocol = elbv2 .ApplicationProtocol .HTTP ,
263+ targets = [self .service ],
264+ health_check = elbv2 .HealthCheck (
265+ path = health_check_path ,
266+ interval = duration .minutes (health_check_interval ),
267+ ),
268+ )
261269
262- # -------------------------------
263- # redirect http to https
264- # -------------------------------
265- http_listener = elbv2 .ApplicationListener (
266- self ,
267- "HttpListener" ,
268- load_balancer = load_balancer ,
269- port = ALB_HTTP_LISTENER_PORT ,
270- open = True ,
271- protocol = elbv2 .ApplicationProtocol .HTTP ,
272- )
270+ # -------------------------------
271+ # redirect http to https
272+ # -------------------------------
273+ http_listener = elbv2 .ApplicationListener (
274+ self ,
275+ "HttpListener" ,
276+ load_balancer = load_balancer ,
277+ port = ALB_HTTP_LISTENER_PORT ,
278+ open = True ,
279+ protocol = elbv2 .ApplicationProtocol .HTTP ,
280+ )
273281
274- http_listener .add_action (
275- "HttpRedirect" ,
276- action = elbv2 .ListenerAction .redirect (
277- port = str (ALB_HTTPS_LISTENER_PORT ),
278- protocol = (elbv2 .ApplicationProtocol .HTTPS ).value ,
279- permanent = True ,
280- ),
281- )
282+ http_listener .add_action (
283+ "HttpRedirect" ,
284+ action = elbv2 .ListenerAction .redirect (
285+ port = str (ALB_HTTPS_LISTENER_PORT ),
286+ protocol = (elbv2 .ApplicationProtocol .HTTPS ).value ,
287+ permanent = True ,
288+ ),
289+ )
290+ else :
291+ # Only HTTP listener, no HTTPS
292+ http_listener = elbv2 .ApplicationListener (
293+ self ,
294+ "HttpListener" ,
295+ load_balancer = load_balancer ,
296+ port = ALB_HTTP_LISTENER_PORT ,
297+ open = True ,
298+ protocol = elbv2 .ApplicationProtocol .HTTP ,
299+ )
300+
301+ http_listener .add_targets (
302+ "HttpTarget" ,
303+ port = props .container_port ,
304+ protocol = elbv2 .ApplicationProtocol .HTTP ,
305+ targets = [self .service ],
306+ health_check = elbv2 .HealthCheck (
307+ path = health_check_path ,
308+ interval = duration .minutes (health_check_interval ),
309+ ),
310+ )
0 commit comments