Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
environment_variables = {
"VPC_CIDR": "10.254.174.0/24",
"FQDN": "prod.mydomain.io",
"CERTIFICATE_ARN": "arn:aws:acm:us-east-1:XXXXXXXXX:certificate/69b3ba97-b382-4648-8f94-a250b77b4994",
"TAGS": {"CostCenter": "NO PROGRAM / 000000"},
}
case "stage":
Expand Down Expand Up @@ -93,7 +92,6 @@
cluster=ecs_stack.cluster,
props=app_props,
load_balancer=load_balancer_stack.alb,
certificate_arn=environment_variables["CERTIFICATE_ARN"],
)
app_stack.add_dependency(app_stack)

Expand Down
123 changes: 76 additions & 47 deletions src/service_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,60 +222,89 @@ def __init__(
cluster: ecs.Cluster,
props: ServiceProps,
load_balancer: elbv2.ApplicationLoadBalancer,
certificate_arn: str,
certificate_arn: str = None,
health_check_path: str = "/",
health_check_interval: int = 1, # max is 5
enable_https: bool = False,
**kwargs,
) -> None:
super().__init__(scope, construct_id, vpc, cluster, props, **kwargs)

# -------------------
# ACM Certificate for HTTPS
# -------------------
self.cert = acm.Certificate.from_certificate_arn(
self, "Cert", certificate_arn=certificate_arn
)
if enable_https and not certificate_arn:
raise ValueError(
"enable_https is True but certificate_arn is None. "
"Please provide a valid certificate ARN to enable HTTPS."
)
if enable_https and certificate_arn:
# -------------------
# ACM Certificate for HTTPS
# -------------------
self.cert = acm.Certificate.from_certificate_arn(
self, "Cert", certificate_arn=certificate_arn
)

# -------------------------------
# Setup https
# -------------------------------
https_listener = elbv2.ApplicationListener(
self,
"HttpsListener",
load_balancer=load_balancer,
port=ALB_HTTPS_LISTENER_PORT,
open=True,
protocol=elbv2.ApplicationProtocol.HTTPS,
certificates=[self.cert],
)
# -------------------------------
# Setup https
# -------------------------------
https_listener = elbv2.ApplicationListener(
self,
"HttpsListener",
load_balancer=load_balancer,
port=ALB_HTTPS_LISTENER_PORT,
open=True,
protocol=elbv2.ApplicationProtocol.HTTPS,
certificates=[self.cert],
)

https_listener.add_targets(
"HttpsTarget",
port=props.container_port,
protocol=elbv2.ApplicationProtocol.HTTP,
targets=[self.service],
health_check=elbv2.HealthCheck(
path=health_check_path, interval=duration.minutes(health_check_interval)
),
)
https_listener.add_targets(
"HttpsTarget",
port=props.container_port,
protocol=elbv2.ApplicationProtocol.HTTP,
targets=[self.service],
health_check=elbv2.HealthCheck(
path=health_check_path,
interval=duration.minutes(health_check_interval),
),
)

# -------------------------------
# redirect http to https
# -------------------------------
http_listener = elbv2.ApplicationListener(
self,
"HttpListener",
load_balancer=load_balancer,
port=ALB_HTTP_LISTENER_PORT,
open=True,
protocol=elbv2.ApplicationProtocol.HTTP,
)
# -------------------------------
# redirect http to https
# -------------------------------
http_listener = elbv2.ApplicationListener(
self,
"HttpListener",
load_balancer=load_balancer,
port=ALB_HTTP_LISTENER_PORT,
open=True,
protocol=elbv2.ApplicationProtocol.HTTP,
)

http_listener.add_action(
"HttpRedirect",
action=elbv2.ListenerAction.redirect(
port=str(ALB_HTTPS_LISTENER_PORT),
protocol=(elbv2.ApplicationProtocol.HTTPS).value,
permanent=True,
),
)
http_listener.add_action(
"HttpRedirect",
action=elbv2.ListenerAction.redirect(
port=str(ALB_HTTPS_LISTENER_PORT),
protocol=(elbv2.ApplicationProtocol.HTTPS).value,
permanent=True,
),
)
else:
# Only HTTP listener, no HTTPS
http_listener = elbv2.ApplicationListener(
self,
"HttpListener",
load_balancer=load_balancer,
port=ALB_HTTP_LISTENER_PORT,
open=True,
protocol=elbv2.ApplicationProtocol.HTTP,
)

http_listener.add_targets(
"HttpTarget",
port=props.container_port,
protocol=elbv2.ApplicationProtocol.HTTP,
targets=[self.service],
health_check=elbv2.HealthCheck(
path=health_check_path,
interval=duration.minutes(health_check_interval),
),
)