Skip to content

Commit 7072ca2

Browse files
authored
fix(apis)!: overwrite api host when provided in config (#245)
# What - When a custom host is configured, overwrite the host in the api integration (bring back the parameter mapping method introduced in #229) - Include origin_path in cloudfront ingest api behavior. Note: this path will not work until the root_path is changed to `api/ingest` in the ingest api - Domain construct now only generates custom subdomains for endpoints when configured (existing stacks must provide new configuration variable). _Note: indentation changes that wrap all subdomain constructs make the change appear larger than they really are. The change removes an unused shared subdomain construct and only generates api-specific subdomains when create subdomains configuration is true._ ## Environment variable changes ### New `VEDA_CUSTOM_DOMAIN` optional configuration to provide a complete url for host overrides in lambda APIs (i.e. `dev.somedomain.com`) ### Removed `VEDA_DOMAIN_SHARED_SUBDOMAIN` is no longer used (logic is managed with custom subdomains config below) ### New+Breaking `VEDA_DOMAIN_CREATE_CUSTOM_SUBDOMAINS` boolean defaults false. Existing deployments with custom subdomains for each raster and stac api endpoints need to set this variable to TRUE.
2 parents d3bf6ff + 6f73459 commit 7072ca2

File tree

8 files changed

+91
-82
lines changed

8 files changed

+91
-82
lines changed

.example.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ STAC_BROWSER_BUCKET=
2828
STAC_URL=
2929
CERT_ARN=
3030
VEDA_CLOUDFRONT=
31+
VEDA_CUSTOM_HOST=

domain/infrastructure/config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ class vedaDomainSettings(BaseSettings):
1414
hosted_zone_name: Optional[str] = Field(
1515
None, description="Custom domain name, i.e. veda-backend.xyz"
1616
)
17-
shared_subdomain: bool = Field(
17+
create_custom_subdomains: bool = Field(
1818
False,
19-
description="Create a subdomain for app stage to used for all endpoints, i.e. <stage>.veda-backend.com/api/raster",
19+
description=(
20+
"When true and hosted zone config is provided, create a unique subdomain for stac and raster apis. "
21+
"For example <stage>-stac.<hosted_zone_name> and <stage>-raster.<hosted_zone_name>"
22+
),
2023
)
2124
api_prefix: Optional[str] = Field(
2225
None,

domain/infrastructure/construct.py

Lines changed: 59 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ def __init__(
3030
self.stac_domain_name = None
3131
self.raster_domain_name = None
3232

33-
if (
34-
veda_domain_settings.hosted_zone_id
35-
and veda_domain_settings.hosted_zone_name
36-
):
33+
if veda_domain_settings.create_custom_subdomains:
34+
3735
# If alternative custom domain provided, use it instead of the default
3836
if alt_domain is True:
3937
hosted_zone_name = veda_domain_settings.alt_hosted_zone_name
@@ -57,84 +55,65 @@ def __init__(
5755
),
5856
)
5957

60-
# Either create one subdomain to share for all endpoints or create a unique subdomain for each
61-
62-
if not veda_domain_settings.shared_subdomain:
63-
# Use custom api prefix if provided or deployment stage if not
64-
if veda_domain_settings.api_prefix:
65-
raster_url_prefix = (
66-
f"{veda_domain_settings.api_prefix.lower()}-raster"
67-
)
68-
stac_url_prefix = f"{veda_domain_settings.api_prefix.lower()}-stac"
69-
else:
70-
raster_url_prefix = f"{stage.lower()}-raster"
71-
stac_url_prefix = f"{stage.lower()}-stac"
72-
raster_domain_name = f"{raster_url_prefix}.{hosted_zone_name}"
73-
stac_domain_name = f"{stac_url_prefix}.{hosted_zone_name}"
74-
75-
self.raster_domain_name = aws_apigatewayv2_alpha.DomainName(
76-
self,
77-
"rasterApiCustomDomain",
78-
domain_name=raster_domain_name,
79-
certificate=certificate,
80-
)
81-
82-
aws_route53.ARecord(
83-
self,
84-
"raster-api-dns-record",
85-
zone=hosted_zone,
86-
target=aws_route53.RecordTarget.from_alias(
87-
aws_route53_targets.ApiGatewayv2DomainProperties(
88-
regional_domain_name=self.raster_domain_name.regional_domain_name,
89-
regional_hosted_zone_id=self.raster_domain_name.regional_hosted_zone_id,
90-
)
91-
),
92-
# Note: CDK will append the hosted zone name (eg: `veda-backend.xyz` to this record name)
93-
record_name=raster_url_prefix,
94-
)
58+
# Use custom api prefix if provided or deployment stage if not
59+
if veda_domain_settings.api_prefix:
60+
raster_url_prefix = f"{veda_domain_settings.api_prefix.lower()}-raster"
61+
stac_url_prefix = f"{veda_domain_settings.api_prefix.lower()}-stac"
62+
else:
63+
raster_url_prefix = f"{stage.lower()}-raster"
64+
stac_url_prefix = f"{stage.lower()}-stac"
65+
raster_domain_name = f"{raster_url_prefix}.{hosted_zone_name}"
66+
stac_domain_name = f"{stac_url_prefix}.{hosted_zone_name}"
9567

96-
self.stac_domain_name = aws_apigatewayv2_alpha.DomainName(
97-
self,
98-
"stacApiCustomDomain",
99-
domain_name=stac_domain_name,
100-
certificate=certificate,
101-
)
68+
self.raster_domain_name = aws_apigatewayv2_alpha.DomainName(
69+
self,
70+
"rasterApiCustomDomain",
71+
domain_name=raster_domain_name,
72+
certificate=certificate,
73+
)
10274

103-
aws_route53.ARecord(
104-
self,
105-
"stac-api-dns-record",
106-
zone=hosted_zone,
107-
target=aws_route53.RecordTarget.from_alias(
108-
aws_route53_targets.ApiGatewayv2DomainProperties(
109-
regional_domain_name=self.stac_domain_name.regional_domain_name,
110-
regional_hosted_zone_id=self.stac_domain_name.regional_hosted_zone_id,
111-
)
112-
),
113-
# Note: CDK will append the hosted zone name (eg: `veda-backend.xyz` to this record name)
114-
record_name=stac_url_prefix,
115-
)
75+
aws_route53.ARecord(
76+
self,
77+
"raster-api-dns-record",
78+
zone=hosted_zone,
79+
target=aws_route53.RecordTarget.from_alias(
80+
aws_route53_targets.ApiGatewayv2DomainProperties(
81+
regional_domain_name=self.raster_domain_name.regional_domain_name,
82+
regional_hosted_zone_id=self.raster_domain_name.regional_hosted_zone_id,
83+
)
84+
),
85+
# Note: CDK will append the hosted zone name (eg: `veda-backend.xyz` to this record name)
86+
record_name=raster_url_prefix,
87+
)
11688

117-
CfnOutput(
118-
self,
119-
"raster-api",
120-
value=f"https://{raster_url_prefix}.{hosted_zone_name}/docs",
121-
)
122-
CfnOutput(
123-
self,
124-
"stac-api",
125-
value=f"https://{stac_url_prefix}.{hosted_zone_name}/",
126-
)
89+
self.stac_domain_name = aws_apigatewayv2_alpha.DomainName(
90+
self,
91+
"stacApiCustomDomain",
92+
domain_name=stac_domain_name,
93+
certificate=certificate,
94+
)
12795

128-
if veda_domain_settings.shared_subdomain:
129-
self.shared_sub_domain_name = aws_apigatewayv2_alpha.DomainName(
130-
self,
131-
"cfSharedApiCustomDomain",
132-
domain_name=f"{stage}.{hosted_zone_name}",
133-
certificate=certificate,
134-
)
96+
aws_route53.ARecord(
97+
self,
98+
"stac-api-dns-record",
99+
zone=hosted_zone,
100+
target=aws_route53.RecordTarget.from_alias(
101+
aws_route53_targets.ApiGatewayv2DomainProperties(
102+
regional_domain_name=self.stac_domain_name.regional_domain_name,
103+
regional_hosted_zone_id=self.stac_domain_name.regional_hosted_zone_id,
104+
)
105+
),
106+
# Note: CDK will append the hosted zone name (eg: `veda-backend.xyz` to this record name)
107+
record_name=stac_url_prefix,
108+
)
135109

136-
CfnOutput(
137-
self,
138-
"shared-sub-domain-name",
139-
value=f"https://{stage}.{hosted_zone_name}/",
140-
)
110+
CfnOutput(
111+
self,
112+
"raster-api",
113+
value=f"https://{raster_url_prefix}.{hosted_zone_name}/docs",
114+
)
115+
CfnOutput(
116+
self,
117+
"stac-api",
118+
value=f"https://{stac_url_prefix}.{hosted_zone_name}/",
119+
)

raster_api/infrastructure/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class vedaRasterSettings(BaseSettings):
7474
description="Optional root path for all api endpoints",
7575
)
7676

77+
custom_host: str = Field(
78+
None,
79+
description="Complete url of custom host including subdomain. When provided, override host in api integration",
80+
)
81+
7782
class Config:
7883
"""model config"""
7984

raster_api/infrastructure/construct.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ def __init__(
8787
)
8888

8989
integration_kwargs = dict(handler=veda_raster_function)
90+
if veda_raster_settings.custom_host:
91+
integration_kwargs[
92+
"parameter_mapping"
93+
] = aws_apigatewayv2_alpha.ParameterMapping().overwrite_header(
94+
"host",
95+
aws_apigatewayv2_alpha.MappingValue(veda_raster_settings.custom_host),
96+
)
97+
9098
raster_api_integration = (
9199
aws_apigatewayv2_integrations_alpha.HttpLambdaIntegration(
92100
construct_id,

routes/infrastructure/construct.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def __init__(
8686
origin=origins.HttpOrigin(
8787
urlparse(veda_route_settings.ingest_url).hostname,
8888
origin_id="ingest-api",
89+
origin_path=f"/{stage}",
8990
),
9091
cache_policy=cf.CachePolicy.CACHING_DISABLED,
9192
allowed_methods=cf.AllowedMethods.ALLOW_ALL,

stac_api/infrastructure/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class vedaSTACSettings(BaseSettings):
2323
description="Optional path prefix to add to all api endpoints",
2424
)
2525

26+
custom_host: str = Field(
27+
None,
28+
description="Complete url of custom host including subdomain. When provided, override host in api integration",
29+
)
30+
2631
class Config:
2732
"""model config"""
2833

stac_api/infrastructure/construct.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ def __init__(
8181
)
8282

8383
integration_kwargs = dict(handler=lambda_function)
84+
if veda_stac_settings.custom_host:
85+
integration_kwargs[
86+
"parameter_mapping"
87+
] = aws_apigatewayv2_alpha.ParameterMapping().overwrite_header(
88+
"host",
89+
aws_apigatewayv2_alpha.MappingValue(veda_stac_settings.custom_host),
90+
)
8491
stac_api_integration = (
8592
aws_apigatewayv2_integrations_alpha.HttpLambdaIntegration(
8693
construct_id,

0 commit comments

Comments
 (0)