Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: include stage subdomain in cloudfront distribution #237

Merged
merged 3 commits into from
Oct 13, 2023
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
5 changes: 5 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
raster_api = RasterApiLambdaConstruct(
veda_stack,
"raster-api",
stage=veda_app_settings.stage_name(),
vpc=vpc.vpc,
database=database,
domain_name=domain.raster_domain_name,
Expand All @@ -66,6 +67,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
stac_api = StacApiLambdaConstruct(
veda_stack,
"stac-api",
stage=veda_app_settings.stage_name(),
vpc=vpc.vpc,
database=database,
raster_api=raster_api,
Expand All @@ -75,6 +77,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
veda_routes = CloudfrontDistributionConstruct(
veda_stack,
"routes",
stage=veda_app_settings.stage_name(),
raster_api_id=raster_api.raster_api.api_id,
stac_api_id=stac_api.stac_api.api_id,
region=veda_app_settings.cdk_default_region,
Expand All @@ -92,6 +95,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
alt_raster_api = RasterApiLambdaConstruct(
veda_stack,
"alt-raster-api",
stage=veda_app_settings.stage_name(),
vpc=vpc.vpc,
database=database,
domain_name=alt_domain.raster_domain_name,
Expand All @@ -100,6 +104,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
alt_stac_api = StacApiLambdaConstruct(
veda_stack,
"alt-stac-api",
stage=veda_app_settings.stage_name(),
vpc=vpc.vpc,
database=database,
raster_api=raster_api,
Expand Down
3 changes: 2 additions & 1 deletion raster_api/infrastructure/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(
self,
scope: Construct,
construct_id: str,
stage: str,
vpc,
database,
code_dir: str = "./",
Expand Down Expand Up @@ -89,7 +90,7 @@ def __init__(
] = aws_apigatewayv2_alpha.ParameterMapping().overwrite_header(
"host",
aws_apigatewayv2_alpha.MappingValue(
veda_raster_settings.domain_hosted_zone_name
f"{stage}.{veda_raster_settings.domain_hosted_zone_name}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment for future work: maybe we can add a method in the api config files to add a stage subdomain for all non-prod stages? We don't need that now so it's just something to think about.

),
)

Expand Down
31 changes: 27 additions & 4 deletions routes/infrastructure/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from aws_cdk import aws_certificatemanager as certificatemanager
from aws_cdk import aws_cloudfront as cf
from aws_cdk import aws_cloudfront_origins as origins
from aws_cdk import aws_route53, aws_route53_targets
from aws_cdk import aws_s3 as s3
from constructs import Construct

Expand All @@ -19,6 +20,7 @@ def __init__(
self,
scope: Construct,
construct_id: str,
stage: str,
raster_api_id: str,
stac_api_id: str,
region: Optional[str],
Expand Down Expand Up @@ -53,36 +55,57 @@ def __init__(
origin=origins.HttpOrigin(
s3Bucket.bucket_website_domain_name,
protocol_policy=cf.OriginProtocolPolicy.HTTP_ONLY,
origin_id="stac-browser",
),
cache_policy=cf.CachePolicy.CACHING_DISABLED,
),
certificate=domain_cert,
domain_names=[veda_route_settings.domain_hosted_zone_name]
domain_names=[f"{stage}.{veda_route_settings.domain_hosted_zone_name}"]
if veda_route_settings.domain_hosted_zone_name
else None,
additional_behaviors={
"/api/stac*": cf.BehaviorOptions(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this additional behaviors in the last PR, maybe we can drop a TODO here to make this configurable in the condition that a path prefex is used like f"{veda_route_settings.stac_path_prefix}*": cf.BehaviorOptions(.... I think we can work on that after using things for a while though, to see what our needs are

origin=origins.HttpOrigin(
f"{stac_api_id}.execute-api.{region}.amazonaws.com"
f"{stac_api_id}.execute-api.{region}.amazonaws.com",
origin_id="stac-api",
),
cache_policy=cf.CachePolicy.CACHING_DISABLED,
allowed_methods=cf.AllowedMethods.ALLOW_ALL,
),
"/api/raster*": cf.BehaviorOptions(
origin=origins.HttpOrigin(
f"{raster_api_id}.execute-api.{region}.amazonaws.com"
f"{raster_api_id}.execute-api.{region}.amazonaws.com",
origin_id="raster-api",
),
cache_policy=cf.CachePolicy.CACHING_DISABLED,
allowed_methods=cf.AllowedMethods.ALLOW_ALL,
),
"/api/ingest*": cf.BehaviorOptions(
origin=origins.HttpOrigin(
urlparse(veda_route_settings.ingest_url).hostname
urlparse(veda_route_settings.ingest_url).hostname,
origin_id="ingest-api",
),
cache_policy=cf.CachePolicy.CACHING_DISABLED,
allowed_methods=cf.AllowedMethods.ALLOW_ALL,
),
},
)

hosted_zone = aws_route53.HostedZone.from_hosted_zone_attributes(
self,
"hosted-zone",
hosted_zone_id=veda_route_settings.domain_hosted_zone_id,
zone_name=veda_route_settings.domain_hosted_zone_name,
)

aws_route53.ARecord(
self,
"cloudfront-dns-record",
zone=hosted_zone,
target=aws_route53.RecordTarget.from_alias(
aws_route53_targets.CloudFrontTarget(self.distribution)
),
record_name=stage,
)

CfnOutput(self, "Endpoint", value=self.distribution.domain_name)
3 changes: 2 additions & 1 deletion stac_api/infrastructure/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(
self,
scope: Construct,
construct_id: str,
stage: str,
vpc,
database,
raster_api, # TODO: typing!
Expand Down Expand Up @@ -81,7 +82,7 @@ def __init__(
] = aws_apigatewayv2_alpha.ParameterMapping().overwrite_header(
"host",
aws_apigatewayv2_alpha.MappingValue(
veda_stac_settings.domain_hosted_zone_name
f"{stage}.{veda_stac_settings.domain_hosted_zone_name}"
),
)

Expand Down