feat: add MainsiteStack to manage main and beta site distributions
This commit is contained in:
parent
41548a17cd
commit
7bede4a357
5 changed files with 119 additions and 4 deletions
16
app.py
16
app.py
|
|
@ -31,7 +31,7 @@ shopping_stack = ShoppingStack(
|
|||
)
|
||||
|
||||
# Domain stack in ap-south-2
|
||||
TradhoxdomainStack(
|
||||
domain_stack = TradhoxdomainStack(
|
||||
app, "TradhoxdomainStack",
|
||||
env=cdk.Environment(
|
||||
account=os.getenv("CDK_DEFAULT_ACCOUNT"),
|
||||
|
|
@ -39,7 +39,19 @@ TradhoxdomainStack(
|
|||
),
|
||||
cross_region_references=True,
|
||||
partner_distribution=partner_stack.distribution,
|
||||
shopping_distribution=shopping_stack.distribution,
|
||||
)
|
||||
|
||||
from mainsite.mainsite_stack import MainsiteStack
|
||||
|
||||
# Main site stack in ap-south-2
|
||||
mainsite_stack = MainsiteStack(
|
||||
app, "MainsiteStack",
|
||||
env=cdk.Environment(
|
||||
account=os.getenv("CDK_DEFAULT_ACCOUNT"),
|
||||
region="ap-south-2",
|
||||
),
|
||||
cross_region_references=True,
|
||||
hosted_zone=domain_stack.hosted_zone,
|
||||
)
|
||||
|
||||
app.synth()
|
||||
|
|
|
|||
0
mainsite/__init__.py
Normal file
0
mainsite/__init__.py
Normal file
88
mainsite/mainsite_stack.py
Normal file
88
mainsite/mainsite_stack.py
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
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))
|
||||
)
|
||||
|
|
@ -34,8 +34,9 @@ class ShoppingStack(Stack):
|
|||
origin=origins.S3BucketOrigin.with_origin_access_control(shopping_bucket),
|
||||
viewer_protocol_policy=cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
|
||||
),
|
||||
domain_names=["tradhox.com", "www.tradhox.com"],
|
||||
certificate=certificate,
|
||||
# REMOVED to free up CNAMEs for MainsiteStack
|
||||
# domain_names=["tradhox.com", "www.tradhox.com"],
|
||||
# certificate=certificate,
|
||||
default_root_object="index.html",
|
||||
error_responses=[
|
||||
cloudfront.ErrorResponse(
|
||||
|
|
|
|||
|
|
@ -143,6 +143,20 @@ class TradhoxdomainStack(Stack):
|
|||
validation=acm.CertificateValidation.from_dns(hosted_zone),
|
||||
)
|
||||
|
||||
# SSL Certificate for tradhox.com with Route53 DNS validation
|
||||
self.tradhox_certificate = acm.Certificate(
|
||||
self, "TradhoxCertificate",
|
||||
domain_name="tradhox.com",
|
||||
validation=acm.CertificateValidation.from_dns(hosted_zone),
|
||||
)
|
||||
|
||||
# SSL Certificate for beta.tradhox.com with Route53 DNS validation
|
||||
self.beta_tradhox_certificate = acm.Certificate(
|
||||
self, "BetaTradhoxCertificate",
|
||||
domain_name="beta.tradhox.com",
|
||||
validation=acm.CertificateValidation.from_dns(hosted_zone),
|
||||
)
|
||||
|
||||
# Route 'partners' frontend to PartnerProductionSiteBucket CloudFront distribution
|
||||
if partner_distribution:
|
||||
route53.ARecord(
|
||||
|
|
|
|||
Loading…
Reference in a new issue