Skip to content

Commit

Permalink
ci: Simplify subdomain hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
topher-lo committed Apr 23, 2024
1 parent 75a9fa0 commit 6af2019
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions aws/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
AWS_SECRET__ARN = os.environ["AWS_SECRET__ARN"]
AWS_ROUTE53__HOSTED_ZONE_ID = os.environ["AWS_ROUTE53__HOSTED_ZONE_ID"]
AWS_ROUTE53__HOSTED_ZONE_NAME = os.environ["AWS_ROUTE53__HOSTED_ZONE_NAME"]
PREFIXED_AWS_ROUTE53__HOSTED_ZONE_NAME = (
f"staging.{AWS_ROUTE53__HOSTED_ZONE_NAME}"
if TRACECAT__APP_ENV == "staging"
else AWS_ROUTE53__HOSTED_ZONE_NAME
)
AWS_ACM__CERTIFICATE_ARN = os.environ["AWS_ACM__CERTIFICATE_ARN"]
AWS_ACM__API_CERTIFICATE_ARN = os.environ["AWS_ACM__API_CERTIFICATE_ARN"]
AWS_ACM__RUNNER_CERTIFICATE_ARN = os.environ["AWS_ACM__RUNNER_CERTIFICATE_ARN"]
Expand Down Expand Up @@ -564,8 +559,6 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
),
)

### Amazon MQ (RabbitMQ) Service

### Load balancer
alb = elbv2.ApplicationLoadBalancer(
self,
Expand All @@ -588,6 +581,11 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
),
)

if TRACECAT__APP_ENV == "staging":
host = f"staging.{AWS_ROUTE53__HOSTED_ZONE_NAME}"
else:
host = AWS_ROUTE53__HOSTED_ZONE_NAME

# Main HTTPS listener
listener = alb.add_listener(
"DefaultHttpsListener",
Expand All @@ -600,7 +598,7 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
priority=30,
conditions=[elbv2.ListenerCondition.path_patterns(["/"])],
action=elbv2.ListenerAction.redirect(
host=f"api.{PREFIXED_AWS_ROUTE53__HOSTED_ZONE_NAME}", # Redirect to the API subdomain
host=f"api.{host}", # Redirect to the API subdomain
protocol="HTTPS",
port="443",
path="/",
Expand All @@ -612,37 +610,29 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
listener.add_action(
"ApiTarget",
priority=10,
conditions=[
elbv2.ListenerCondition.host_headers(
[f"api.{PREFIXED_AWS_ROUTE53__HOSTED_ZONE_NAME}"]
)
],
conditions=[elbv2.ListenerCondition.host_headers([f"api.{host}"])],
action=elbv2.ListenerAction.forward(target_groups=[api_target_group]),
)
listener.add_action(
"RunnerTarget",
priority=20,
conditions=[
elbv2.ListenerCondition.host_headers(
[f"runner.{PREFIXED_AWS_ROUTE53__HOSTED_ZONE_NAME}"]
)
],
conditions=[elbv2.ListenerCondition.host_headers([f"runner.{host}"])],
action=elbv2.ListenerAction.forward(target_groups=[runner_target_group]),
)

# Create A record to point the hosted zone domain to the ALB
route53.ARecord(
self,
"AliasRecord",
record_name=PREFIXED_AWS_ROUTE53__HOSTED_ZONE_NAME,
record_name=host,
target=route53.RecordTarget.from_alias(LoadBalancerTarget(alb)),
zone=hosted_zone,
)
# Create A record for api.domain.com pointing to the ALB
route53.ARecord(
self,
"ApiAliasRecord",
record_name=f"api.{PREFIXED_AWS_ROUTE53__HOSTED_ZONE_NAME}",
record_name=f"api.{host}",
target=route53.RecordTarget.from_alias(LoadBalancerTarget(alb)),
zone=hosted_zone,
)
Expand All @@ -651,7 +641,7 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
route53.ARecord(
self,
"RunnerAliasRecord",
record_name=f"runner.{PREFIXED_AWS_ROUTE53__HOSTED_ZONE_NAME}",
record_name=f"runner.{host}",
target=route53.RecordTarget.from_alias(LoadBalancerTarget(alb)),
zone=hosted_zone,
)

0 comments on commit 6af2019

Please sign in to comment.