from typing import Optional from aws_cdk import ( Duration, Stack, aws_route53 as route53, aws_route53_targets as targets, aws_cloudfront as cloudfront, ) from constructs import Construct class TradhoxdomainStack(Stack): def __init__( self, scope: Construct, construct_id: str, partner_distribution: Optional[cloudfront.IDistribution] = None, partner_distribution_domain_name: Optional[str] = None, shopping_distribution: Optional[cloudfront.IDistribution] = None, shopping_distribution_domain_name: Optional[str] = None, main_distribution: Optional[cloudfront.IDistribution] = None, main_distribution_domain_name: Optional[str] = None, **kwargs ) -> None: super().__init__(scope, construct_id, **kwargs) # Create a Hosted Zone for tradhox.com hosted_zone = route53.PublicHostedZone( self, "TradhoxHostedZone", zone_name="tradhox.com" ) self.hosted_zone = hosted_zone # Backend API / service subdomains pointing to target EC2 IP subdomains = [ "partners-api", "api", "mailservices", "whatsapp", "payments", "socialmedia", "betapartners", "betapartner-api", "beta-api", "beta-mailservices", "betawhatsapp", "betapayments", ] # Target IP address provided by the user target_ip = "16.113.57.127" for subdomain in subdomains: # Create an A Record for each subdomain route53.ARecord( self, f"{subdomain.replace('-', '').capitalize()}Record", zone=hosted_zone, record_name=subdomain, target=route53.RecordTarget.from_ip_addresses(target_ip) ) # CI/CD pipeline server (Forgejo on cisystem) ci_system_ip = "16.113.106.152" route53.ARecord( self, "PipelineRecord", zone=hosted_zone, record_name="pipeline", target=route53.RecordTarget.from_ip_addresses(ci_system_ip) ) # Route 'partners' frontend to PartnerProductionSiteBucket CloudFront distribution if partner_distribution: route53.ARecord( self, "PartnersRecord", zone=hosted_zone, record_name="partners", target=route53.RecordTarget.from_alias(targets.CloudFrontTarget(partner_distribution)) ) elif partner_distribution_domain_name: route53.CnameRecord( self, "PartnersRecord", zone=hosted_zone, record_name="partners", domain_name=partner_distribution_domain_name, ttl=Duration.minutes(5), ) # Route main domain ('tradhox.com' and 'www.tradhox.com') to Shopping / Main CloudFront distribution target_main_distribution = shopping_distribution or main_distribution target_main_distribution_domain_name = shopping_distribution_domain_name or main_distribution_domain_name if target_main_distribution: # Apex domain A Record (tradhox.com) route53.ARecord( self, "MainDomainApexRecord", zone=hosted_zone, record_name="", target=route53.RecordTarget.from_alias(targets.CloudFrontTarget(target_main_distribution)) ) # www subdomain A Record (www.tradhox.com) route53.ARecord( self, "MainDomainWwwRecord", zone=hosted_zone, record_name="www", target=route53.RecordTarget.from_alias(targets.CloudFrontTarget(target_main_distribution)) ) elif target_main_distribution_domain_name: route53.CnameRecord( self, "MainDomainWwwRecord", zone=hosted_zone, record_name="www", domain_name=target_main_distribution_domain_name, ttl=Duration.minutes(5), ) # MX records for Hostinger mail services route53.MxRecord( self, "TradhoxMxRecord", zone=hosted_zone, values=[ route53.MxRecordValue( host_name="mx1.hostinger.com", priority=5, ), route53.MxRecordValue( host_name="mx2.hostinger.com", priority=10, ), ], ttl=Duration.seconds(14400), ) # SPF TXT record for Hostinger mail services route53.TxtRecord( self, "TradhoxSpfTxtRecord", zone=hosted_zone, values=[ "v=spf1 include:_spf.mail.hostinger.com ~all" ], ttl=Duration.seconds(3600), ) # DKIM CNAME records for Hostinger mail services for key in ["a", "b", "c"]: route53.CnameRecord( self, f"HostingerDkimRecord{key.upper()}", zone=hosted_zone, record_name=f"hostingermail-{key}._domainkey", domain_name=f"hostingermail-{key}.dkim.mail.hostinger.com", ttl=Duration.seconds(300), ) # DMARC TXT record route53.TxtRecord( self, "TradhoxDmarcTxtRecord", zone=hosted_zone, record_name="_dmarc", values=[ "v=DMARC1; p=none" ], ttl=Duration.seconds(3600), )