Tradhox_domain_settings/mainsite/mainsite_stack.py

168 lines
6.2 KiB
Python

from aws_cdk import (
Stack,
aws_s3 as s3,
aws_cloudfront as cloudfront,
aws_cloudfront_origins as origins,
aws_route53 as route53,
aws_route53_targets as targets,
aws_certificatemanager as acm,
RemovalPolicy,
)
from constructs import Construct
class MainsiteStack(Stack):
def __init__(
self, scope: Construct, construct_id: str,
hosted_zone: route53.IHostedZone,
**kwargs
) -> None:
super().__init__(scope, construct_id, **kwargs)
# Use the existing wildcard certificate in us-east-1 for CloudFront
us_east_1_cert = acm.Certificate.from_certificate_arn(
self, "TradhoxUsEast1Certificate",
"arn:aws:acm:us-east-1:764709663363:certificate/037281b1-e7ef-416e-af27-54dbc34cf6c3"
)
# 1. Main site S3 Bucket
main_bucket = s3.Bucket(
self, "TradhoxMainSiteBucket",
bucket_name="tradhoxmainsite",
block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
removal_policy=RemovalPolicy.RETAIN,
)
# 2. Main site CloudFront Distribution
self.main_distribution = cloudfront.Distribution(
self, "TradhoxMainSiteDistribution",
default_behavior=cloudfront.BehaviorOptions(
origin=origins.S3BucketOrigin.with_origin_access_control(main_bucket),
viewer_protocol_policy=cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
),
domain_names=["tradhox.com", "www.tradhox.com"],
certificate=us_east_1_cert,
default_root_object="index.html",
)
# 3. Beta site S3 Bucket
beta_bucket = s3.Bucket(
self, "TradhoxBetaSiteBucket",
bucket_name="tradhoxbetasite",
block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
removal_policy=RemovalPolicy.RETAIN,
)
# 4. Beta site CloudFront Distribution
self.beta_distribution = cloudfront.Distribution(
self, "TradhoxBetaSiteDistribution",
default_behavior=cloudfront.BehaviorOptions(
origin=origins.S3BucketOrigin.with_origin_access_control(beta_bucket),
viewer_protocol_policy=cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
),
domain_names=["beta.tradhox.com"],
certificate=us_east_1_cert,
default_root_object="index.html",
)
# 5. Route53 Records for Main Site
route53.ARecord(
self, "MainDomainApexRecord",
zone=hosted_zone,
record_name="",
target=route53.RecordTarget.from_alias(targets.CloudFrontTarget(self.main_distribution))
)
route53.ARecord(
self, "MainDomainWwwRecord",
zone=hosted_zone,
record_name="www",
target=route53.RecordTarget.from_alias(targets.CloudFrontTarget(self.main_distribution))
)
# 6. Route53 Records for Beta Site
route53.ARecord(
self, "BetaDomainRecord",
zone=hosted_zone,
record_name="beta",
target=route53.RecordTarget.from_alias(targets.CloudFrontTarget(self.beta_distribution))
)
# 7. CSA Website S3 Bucket
csa_bucket = s3.Bucket(
self, "CsaWebsiteBucket",
bucket_name="csawebsite",
block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
removal_policy=RemovalPolicy.RETAIN,
)
# 8. CSA Website CloudFront Distribution
self.csa_distribution = cloudfront.Distribution(
self, "CsaWebsiteDistribution",
default_behavior=cloudfront.BehaviorOptions(
origin=origins.S3BucketOrigin.with_origin_access_control(csa_bucket),
viewer_protocol_policy=cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
),
domain_names=["csa.tradhox.com"],
certificate=us_east_1_cert,
default_root_object="index.html",
error_responses=[
cloudfront.ErrorResponse(
http_status=403,
response_http_status=200,
response_page_path="/index.html",
),
cloudfront.ErrorResponse(
http_status=404,
response_http_status=200,
response_page_path="/index.html",
),
],
)
# 9. Beta CSA Site S3 Bucket
betacsa_bucket = s3.Bucket(
self, "BetaCsaSiteBucket",
bucket_name="betacsa",
block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
removal_policy=RemovalPolicy.RETAIN,
)
# 10. Beta CSA Site CloudFront Distribution
self.betacsa_distribution = cloudfront.Distribution(
self, "BetaCsaSiteDistribution",
default_behavior=cloudfront.BehaviorOptions(
origin=origins.S3BucketOrigin.with_origin_access_control(betacsa_bucket),
viewer_protocol_policy=cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
),
domain_names=["betacsa.tradhox.com"],
certificate=us_east_1_cert,
default_root_object="index.html",
error_responses=[
cloudfront.ErrorResponse(
http_status=403,
response_http_status=200,
response_page_path="/index.html",
),
cloudfront.ErrorResponse(
http_status=404,
response_http_status=200,
response_page_path="/index.html",
),
],
)
# 11. Route53 Records for CSA Site
route53.ARecord(
self, "CsaDomainRecord",
zone=hosted_zone,
record_name="csa",
target=route53.RecordTarget.from_alias(targets.CloudFrontTarget(self.csa_distribution))
)
# 12. Route53 Records for Beta CSA Site
route53.ARecord(
self, "BetaCsaDomainRecord",
zone=hosted_zone,
record_name="betacsa",
target=route53.RecordTarget.from_alias(targets.CloudFrontTarget(self.betacsa_distribution))
)