From f32f152014b2d18ca7b6ab44e7fc7a243d558cef Mon Sep 17 00:00:00 2001 From: smohiudd Date: Thu, 12 Oct 2023 17:30:02 -0600 Subject: [PATCH 1/3] fix cloudfront subdomain --- app.py | 3 +++ raster_api/infrastructure/construct.py | 3 ++- routes/infrastructure/construct.py | 32 ++++++++++++++++++++++---- stac_api/infrastructure/construct.py | 3 ++- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index b2716754..5a27b587 100644 --- a/app.py +++ b/app.py @@ -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, @@ -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, @@ -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, diff --git a/raster_api/infrastructure/construct.py b/raster_api/infrastructure/construct.py index cc155208..dbfef904 100644 --- a/raster_api/infrastructure/construct.py +++ b/raster_api/infrastructure/construct.py @@ -24,6 +24,7 @@ def __init__( self, scope: Construct, construct_id: str, + stage: str, vpc, database, code_dir: str = "./", @@ -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}" ), ) diff --git a/routes/infrastructure/construct.py b/routes/infrastructure/construct.py index 32d7b7f9..68e082fb 100755 --- a/routes/infrastructure/construct.py +++ b/routes/infrastructure/construct.py @@ -6,7 +6,9 @@ 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 from .config import veda_route_settings @@ -19,6 +21,7 @@ def __init__( self, scope: Construct, construct_id: str, + stage: str, raster_api_id: str, stac_api_id: str, region: Optional[str], @@ -53,31 +56,35 @@ 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( 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, @@ -85,4 +92,21 @@ def __init__( }, ) + 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) diff --git a/stac_api/infrastructure/construct.py b/stac_api/infrastructure/construct.py index a85e0d5e..2185ce5b 100644 --- a/stac_api/infrastructure/construct.py +++ b/stac_api/infrastructure/construct.py @@ -23,6 +23,7 @@ def __init__( self, scope: Construct, construct_id: str, + stage: str, vpc, database, raster_api, # TODO: typing! @@ -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}" ), ) From f6f71389bfa22f3ee4f6d170fb13ca79e2d3c55a Mon Sep 17 00:00:00 2001 From: smohiudd Date: Thu, 12 Oct 2023 17:36:45 -0600 Subject: [PATCH 2/3] incude missing argument --- app.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app.py b/app.py index 5a27b587..1cd76597 100644 --- a/app.py +++ b/app.py @@ -95,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, @@ -103,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, From 31ce1b4ab05006cb304d70206f51e36a740f834d Mon Sep 17 00:00:00 2001 From: smohiudd Date: Thu, 12 Oct 2023 17:41:57 -0600 Subject: [PATCH 3/3] formatting --- routes/infrastructure/construct.py | 1 - 1 file changed, 1 deletion(-) diff --git a/routes/infrastructure/construct.py b/routes/infrastructure/construct.py index 68e082fb..300c290e 100755 --- a/routes/infrastructure/construct.py +++ b/routes/infrastructure/construct.py @@ -8,7 +8,6 @@ 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 from .config import veda_route_settings