|
7 | 7 | RemovalPolicy, |
8 | 8 | Stack, |
9 | 9 | aws_certificatemanager, |
| 10 | + aws_cloudfront, |
| 11 | + aws_cloudfront_origins, |
10 | 12 | aws_ec2, |
11 | 13 | aws_iam, |
12 | 14 | aws_lambda, |
13 | 15 | aws_rds, |
| 16 | + aws_route53, |
| 17 | + aws_route53_targets, |
14 | 18 | aws_s3, |
15 | 19 | ) |
16 | 20 | from aws_cdk.aws_apigateway import DomainNameOptions |
@@ -352,29 +356,90 @@ def __init__( |
352 | 356 | ) |
353 | 357 |
|
354 | 358 | if app_config.stac_browser_version: |
| 359 | + if not ( |
| 360 | + app_config.hosted_zone_id |
| 361 | + and app_config.hosted_zone_name |
| 362 | + and app_config.stac_browser_custom_domain |
| 363 | + and app_config.stac_browser_certificate_arn |
| 364 | + ): |
| 365 | + raise ValueError( |
| 366 | + "to deploy STAC browser you must provide config parameters for hosted_zone_id and stac_browser_custom_domain and stac_browser_certificate_arn" |
| 367 | + ) |
| 368 | + |
355 | 369 | stac_browser_bucket = aws_s3.Bucket( |
356 | 370 | self, |
357 | 371 | "stac-browser-bucket", |
358 | 372 | bucket_name=app_config.build_service_name("stac-browser"), |
359 | 373 | removal_policy=RemovalPolicy.DESTROY, |
360 | 374 | auto_delete_objects=True, |
361 | | - website_index_document="index.html", |
362 | | - public_read_access=True, |
363 | | - block_public_access=aws_s3.BlockPublicAccess( |
364 | | - block_public_acls=False, |
365 | | - block_public_policy=False, |
366 | | - ignore_public_acls=False, |
367 | | - restrict_public_buckets=False, |
| 375 | + block_public_access=aws_s3.BlockPublicAccess.BLOCK_ALL, |
| 376 | + enforce_ssl=True, |
| 377 | + ) |
| 378 | + |
| 379 | + distribution = aws_cloudfront.Distribution( |
| 380 | + self, |
| 381 | + "stac-browser-distribution", |
| 382 | + default_behavior=aws_cloudfront.BehaviorOptions( |
| 383 | + origin=aws_cloudfront_origins.S3Origin(stac_browser_bucket), |
| 384 | + viewer_protocol_policy=aws_cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS, |
| 385 | + allowed_methods=aws_cloudfront.AllowedMethods.ALLOW_GET_HEAD, |
| 386 | + cached_methods=aws_cloudfront.CachedMethods.CACHE_GET_HEAD, |
368 | 387 | ), |
369 | | - object_ownership=aws_s3.ObjectOwnership.OBJECT_WRITER, |
| 388 | + default_root_object="index.html", |
| 389 | + error_responses=[ |
| 390 | + aws_cloudfront.ErrorResponse( |
| 391 | + http_status=404, |
| 392 | + response_http_status=200, |
| 393 | + response_page_path="/index.html", |
| 394 | + ) |
| 395 | + ], |
| 396 | + certificate=aws_certificatemanager.Certificate.from_certificate_arn( |
| 397 | + self, |
| 398 | + "stac-browser-certificate", |
| 399 | + app_config.stac_browser_certificate_arn, |
| 400 | + ), |
| 401 | + domain_names=[app_config.stac_browser_custom_domain], |
370 | 402 | ) |
| 403 | + |
| 404 | + account_id = Stack.of(self).account |
| 405 | + distribution_arn = f"arn:aws:cloudfront::${account_id}:distribution/${distribution.distribution_id}" |
| 406 | + |
| 407 | + stac_browser_bucket.add_to_resource_policy( |
| 408 | + aws_iam.PolicyStatement( |
| 409 | + actions=["s3:GetObject"], |
| 410 | + resources=[stac_browser_bucket.arn_for_objects("*")], |
| 411 | + principals=[aws_iam.ServicePrincipal("cloudfront.amazonaws.com")], |
| 412 | + conditions={"StringEquals": {"AWS:SourceArn": distribution_arn}}, |
| 413 | + ) |
| 414 | + ) |
| 415 | + |
| 416 | + hosted_zone = aws_route53.HostedZone.from_hosted_zone_attributes( |
| 417 | + self, |
| 418 | + "stac-browser-hosted-zone", |
| 419 | + hosted_zone_id=app_config.hosted_zone_id, |
| 420 | + zone_name=app_config.hosted_zone_name, |
| 421 | + ) |
| 422 | + |
| 423 | + aws_route53.ARecord( |
| 424 | + self, |
| 425 | + "stac-browser-alias", |
| 426 | + zone=hosted_zone, |
| 427 | + target=aws_route53.RecordTarget.from_alias( |
| 428 | + aws_route53_targets.CloudFrontTarget(distribution) |
| 429 | + ), |
| 430 | + record_name=app_config.stac_browser_custom_domain, |
| 431 | + ) |
| 432 | + |
371 | 433 | StacBrowser( |
372 | 434 | self, |
373 | 435 | "stac-browser", |
374 | 436 | github_repo_tag=app_config.stac_browser_version, |
375 | 437 | stac_catalog_url=f"https://{app_config.stac_api_custom_domain}", |
376 | 438 | website_index_document="index.html", |
377 | 439 | bucket_arn=stac_browser_bucket.bucket_arn, |
| 440 | + config_file_path=os.path.join( |
| 441 | + os.path.abspath(context_dir), "browser_config.js" |
| 442 | + ), |
378 | 443 | ) |
379 | 444 |
|
380 | 445 | def _create_data_access_role(self) -> aws_iam.Role: |
|
0 commit comments