Skip to content

Commit bd13056

Browse files
committed
Set up HTTP ALB listener for ACM gateway
1 parent da026f9 commit bd13056

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

docs/docs/concepts/gateways.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ If you disable [public IP](#public-ip) (e.g. to make the gateway private) or if
119119
`dstack` supports the following certificate types:
120120

121121
* `lets-encrypt` (default) — Automatic certificates via [Let's Encrypt](https://letsencrypt.org/). Requires a [public IP](#public-ip).
122-
* `acm` — Certificates managed by [AWS Certificate Manager](https://aws.amazon.com/certificate-manager/). AWS-only. TLS is terminated at the load balancer, not at the gateway.
122+
* `acm` — Certificates managed by [AWS Certificate Manager](https://aws.amazon.com/certificate-manager/). AWS-only. TLS is terminated at the load balancer, not at the gateway, and HTTP requests are redirected to HTTPS by the ALB.
123123
Requires a VPC with at least two subnets in different availability zones to provision a load balancer. If `public_ip: False`, subnets must be private and have a route to NAT gateway.
124124
* `null` — No certificate. Services will use HTTP.
125125

src/dstack/_internal/core/backends/aws/compute.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class AWSGatewayBackendData(CoreModel):
8989
lb_arn: str
9090
tg_arn: str
9191
listener_arn: str
92+
http_listener_arn: Optional[str] = None # None for old gateways
9293

9394

9495
class AWSVolumeBackendData(CoreModel):
@@ -594,7 +595,7 @@ def create_gateway(
594595
)
595596
logger.debug("Registered ALB target for gateway %s", configuration.instance_name)
596597

597-
logger.debug("Creating ALB Listener for gateway %s...", configuration.instance_name)
598+
logger.debug("Creating HTTPS ALB listener for gateway %s...", configuration.instance_name)
598599
response = elb_client.create_listener(
599600
LoadBalancerArn=lb_arn,
600601
Protocol="HTTPS",
@@ -611,7 +612,26 @@ def create_gateway(
611612
],
612613
)
613614
listener_arn = response["Listeners"][0]["ListenerArn"]
614-
logger.debug("Created ALB Listener for gateway %s", configuration.instance_name)
615+
logger.debug("Created HTTPS ALB listener for gateway %s", configuration.instance_name)
616+
617+
logger.debug("Creating HTTP ALB listener for gateway %s...", configuration.instance_name)
618+
response = elb_client.create_listener(
619+
LoadBalancerArn=lb_arn,
620+
Protocol="HTTP",
621+
Port=80,
622+
DefaultActions=[
623+
{
624+
"Type": "redirect",
625+
"RedirectConfig": {
626+
"Protocol": "HTTPS",
627+
"Port": "443",
628+
"StatusCode": "HTTP_301",
629+
},
630+
}
631+
],
632+
)
633+
http_listener_arn = response["Listeners"][0]["ListenerArn"]
634+
logger.debug("Created HTTP ALB listener for gateway %s", configuration.instance_name)
615635

616636
ip_address = _get_instance_ip(instance, configuration.public_ip)
617637
return GatewayProvisioningData(
@@ -623,6 +643,7 @@ def create_gateway(
623643
lb_arn=lb_arn,
624644
tg_arn=tg_arn,
625645
listener_arn=listener_arn,
646+
http_listener_arn=http_listener_arn,
626647
).json(),
627648
)
628649

@@ -659,6 +680,8 @@ def terminate_gateway(
659680
elb_client = self.session.client("elbv2", region_name=configuration.region)
660681

661682
logger.debug("Deleting ALB resources for gateway %s...", configuration.instance_name)
683+
if backend_data_parsed.http_listener_arn is not None:
684+
elb_client.delete_listener(ListenerArn=backend_data_parsed.http_listener_arn)
662685
elb_client.delete_listener(ListenerArn=backend_data_parsed.listener_arn)
663686
elb_client.delete_target_group(TargetGroupArn=backend_data_parsed.tg_arn)
664687
elb_client.delete_load_balancer(LoadBalancerArn=backend_data_parsed.lb_arn)

0 commit comments

Comments
 (0)