I am trying to setup multiple websites in cloudfront terraform configurations with one base domain and multiple subdomains:
I am trying to create individual distribution for each website with alternate names like this:
First step is to create ACM certificate so I created a module acm_certificate/main.tf
data "aws_route53_zone" "public" {
name = var.domain
}
resource "aws_acm_certificate" "this" {
domain_name = "www.${var.domain}"
subject_alternative_names = ["www.${var.domain}", "${var.domain}"]
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "cert_validation" {
allow_overwrite = true
for_each = {
for dvo in aws_acm_certificate.this.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
zone_id = data.aws_route53_zone.public.id
}
}
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = each.value.zone_id
}
resource "aws_acm_certificate_validation" "this" {
certificate_arn = aws_acm_certificate.this.arn
validation_record_fqdns = [for record in aws_route53_record.cert_validation : record.fqdn]
}
And this is my child module that should create ACM cert for base domain and subdomains sites/route53.tf
data "aws_route53_zone" "public" {
name = var.domain # base domain route53
}
locals {
subdomainsList = {
for pv in var.project_version : pv.project => {
subdomain_name = "${pv.project}.${var.domain}"
#target_distribution = aws_cloudfront_distribution.engagement_hub_subdomains[pv.project]
}
}
}
module "acm_certificate_domain" {
source = "../../modules/acm_certificate" # for base domain tst.example.com
domain = var.domain
env = var.env
region = var.region
dns_name = var.dns_name
providers = {
aws = aws
aws.global = aws.global
}
}
module "acm_certificate_subdomain" {
env = var.env
region = var.region
dns_name = var.dns_name
for_each = local.subdomainsList
source = "../../modules/acm_certificate" # for sub domains
domain = each.value.subdomain_name
providers = {
aws = aws
aws.global = aws.global
}
}
where var.domain = example.com in TF cloud workspace variable var.project_version =[{"project": "project1", "version": "v123.1"}]
error
Error: no matching Route53Zone found
with module.static_site.module.acm_certificate_subdomain["project1"].data.aws_route53_zone.public
on modules/acm_certificate/main.tf line 1, in data "aws_route53_zone" "public":
data "aws_route53_zone" "public" {