refactor: update CloudFront origins and configure Route53 DNS records for subdomains, mail services, and partner distribution

This commit is contained in:
vickytechkey 2026-09-09 21:59:53 +05:30
parent 67d2d3fb62
commit de80dc9a93
4 changed files with 128 additions and 28 deletions

47
app.py
View file

@ -10,29 +10,34 @@ from shopping.shopping_stack import ShoppingStack
app = cdk.App()
# Existing domain stack
TradhoxdomainStack(app, "TradhoxdomainStack",
# If you don't specify 'env', this stack will be environment-agnostic.
# Account/Region-dependent features and context lookups will not work,
# but a single synthesized template can be deployed anywhere.
# Uncomment the next line to specialize this stack for the AWS Account
# and Region that are implied by the current CLI configuration.
#env=cdk.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'), region=os.getenv('CDK_DEFAULT_REGION')),
# Uncomment the next line if you know exactly what Account and Region you
# want to deploy the stack to. */
#env=cdk.Environment(account='123456789012', region='us-east-1'),
# For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
)
# Partner stack in ap-south-2
PartnerStack(app, "PartnerStack", env=cdk.Environment(region="ap-south-2"))
partner_stack = PartnerStack(
app, "PartnerStack",
env=cdk.Environment(
account=os.getenv("CDK_DEFAULT_ACCOUNT"),
region="ap-south-2",
),
cross_region_references=True,
)
# Shopping stack in ap-south-2
ShoppingStack(app, "ShoppingStack", env=cdk.Environment(region="ap-south-2"))
ShoppingStack(
app, "ShoppingStack",
env=cdk.Environment(
account=os.getenv("CDK_DEFAULT_ACCOUNT"),
region="ap-south-2",
),
)
# Domain stack in ap-south-2
TradhoxdomainStack(
app, "TradhoxdomainStack",
env=cdk.Environment(
account=os.getenv("CDK_DEFAULT_ACCOUNT"),
region="ap-south-2",
),
cross_region_references=True,
partner_distribution=partner_stack.distribution,
)
app.synth()

View file

@ -21,11 +21,22 @@ class PartnerStack(Stack):
)
# 2. Create the CloudFront Distribution with OAC (Origin Access Control)
# S3Origin automatically configures OAC for the bucket
distribution = cloudfront.Distribution(
self.distribution = cloudfront.Distribution(
self, "PartnerSiteDistribution",
default_behavior=cloudfront.BehaviorOptions(
origin=origins.S3Origin(partner_bucket)
origin=origins.S3BucketOrigin.with_origin_access_control(partner_bucket)
),
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",
),
],
)

View file

@ -24,7 +24,7 @@ class ShoppingStack(Stack):
distribution = cloudfront.Distribution(
self, "ShoppingSiteDistribution",
default_behavior=cloudfront.BehaviorOptions(
origin=origins.S3Origin(shopping_bucket)
origin=origins.S3BucketOrigin.with_origin_access_control(shopping_bucket)
),
default_root_object="index.html",
)

View file

@ -1,12 +1,24 @@
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, **kwargs) -> None:
def __init__(
self,
scope: Construct,
construct_id: str,
partner_distribution: Optional[cloudfront.IDistribution] = None,
partner_distribution_domain_name: Optional[str] = None,
**kwargs
) -> None:
super().__init__(scope, construct_id, **kwargs)
# Create a Hosted Zone for tradhox.com
@ -14,15 +26,22 @@ class TradhoxdomainStack(Stack):
self, "TradhoxHostedZone",
zone_name="tradhox.com"
)
self.hosted_zone = hosted_zone
# Backend API / service subdomains pointing to target EC2 IP
subdomains = [
"partners",
"partners-api",
"api",
"mailservices",
"whatsapp",
"payments",
"socialmedia"
"socialmedia",
"betapartners",
"betapartner-api",
"beta-api",
"beta-mailservices",
"betawhatsapp",
"betapayments",
]
# Target IP address provided by the user
@ -36,3 +55,68 @@ class TradhoxdomainStack(Stack):
record_name=subdomain,
target=route53.RecordTarget.from_ip_addresses(target_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),
)
# 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),
)