-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add configuration for deploying a cloudfront distribution for t…
…he veda-backend (#229)
- Loading branch information
Showing
9 changed files
with
228 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Settings for Cloudfront distribution - any environment variables starting with | ||
`VEDA_` will overwrite the values of variables in this file | ||
""" | ||
from typing import Optional | ||
|
||
from pydantic import BaseSettings, Field | ||
|
||
|
||
class vedaRouteSettings(BaseSettings): | ||
"""Veda Route settings""" | ||
|
||
cloudfront: Optional[bool] = Field( | ||
False, | ||
description="Boolean if Cloudfront Distribution should be deployed", | ||
) | ||
|
||
# STAC S#3 browser bucket name | ||
stac_browser_bucket: Optional[str] = Field( | ||
"", description="STAC browser S3 bucket name" | ||
) | ||
|
||
# API Gateway URLs | ||
ingest_url: Optional[str] = Field( | ||
"", | ||
description="URL of ingest API", | ||
) | ||
|
||
domain_hosted_zone_name: Optional[str] = Field( | ||
None, | ||
description="Domain name for the cloudfront distribution", | ||
) | ||
|
||
domain_hosted_zone_id: Optional[str] = Field( | ||
None, description="Domain ID for the cloudfront distribution" | ||
) | ||
|
||
cert_arn: Optional[str] = Field( | ||
None, | ||
description="Certificate’s ARN", | ||
) | ||
|
||
class Config: | ||
"""model config""" | ||
|
||
env_prefix = "VEDA_" | ||
case_sentive = False | ||
env_file = ".env" | ||
|
||
|
||
veda_route_settings = vedaRouteSettings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"""CDK Construct for a Cloudfront Distribution.""" | ||
from typing import Optional | ||
from urllib.parse import urlparse | ||
|
||
from aws_cdk import CfnOutput, Stack | ||
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_s3 as s3 | ||
from constructs import Construct | ||
|
||
from .config import veda_route_settings | ||
|
||
|
||
class CloudfrontDistributionConstruct(Construct): | ||
"""CDK Construct for a Cloudfront Distribution.""" | ||
|
||
def __init__( | ||
self, | ||
scope: Construct, | ||
construct_id: str, | ||
raster_api_id: str, | ||
stac_api_id: str, | ||
region: Optional[str], | ||
**kwargs, | ||
) -> None: | ||
""".""" | ||
super().__init__(scope, construct_id) | ||
|
||
stack_name = Stack.of(self).stack_name | ||
|
||
if veda_route_settings.cloudfront: | ||
s3Bucket = s3.Bucket.from_bucket_name( | ||
self, | ||
"stac-browser-bucket", | ||
bucket_name=veda_route_settings.stac_browser_bucket, | ||
) | ||
|
||
# Certificate must be in zone us-east-1 | ||
domain_cert = ( | ||
certificatemanager.Certificate.from_certificate_arn( | ||
self, "domainCert", veda_route_settings.cert_arn | ||
) | ||
if veda_route_settings.cert_arn | ||
else None | ||
) | ||
|
||
self.distribution = cf.Distribution( | ||
self, | ||
stack_name, | ||
comment=stack_name, | ||
default_behavior=cf.BehaviorOptions( | ||
origin=origins.HttpOrigin( | ||
s3Bucket.bucket_website_domain_name, | ||
protocol_policy=cf.OriginProtocolPolicy.HTTP_ONLY, | ||
), | ||
cache_policy=cf.CachePolicy.CACHING_DISABLED, | ||
), | ||
certificate=domain_cert, | ||
domain_names=[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" | ||
), | ||
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" | ||
), | ||
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 | ||
), | ||
cache_policy=cf.CachePolicy.CACHING_DISABLED, | ||
allowed_methods=cf.AllowedMethods.ALLOW_ALL, | ||
), | ||
}, | ||
) | ||
|
||
CfnOutput(self, "Endpoint", value=self.distribution.domain_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters