@@ -132,13 +132,43 @@ resource "aws_cloudfront_distribution" "client_assets_distribution" {
132132 }
133133}
134134
135- # Subdomain to point at CF
136- resource "aws_route53_record" "client_assets" {
137- for_each = var. buckets
135+ locals {
136+ # Find buckets that are the domain apex. These need to have A ALIAS records.
137+ rootDomainBuckets = [
138+ for bucket in var . buckets :
139+ bucket if length (regexall (" \\ ." , bucket)) == 1
140+ ]
141+
142+ # Find buckets that are subdomains. These can have CNAME records.
143+ subDomainBuckets = [
144+ for bucket in var . buckets :
145+ bucket if length (regexall (" \\ ." , bucket)) > 1
146+ ]
147+
148+ }
149+
150+ # Root domains to point at CF
151+ resource "aws_route53_record" "client_assets_root" {
152+ count = length (local. rootDomainBuckets )
153+
154+ zone_id = var. route53_zone_id
155+ name = local. rootDomainBuckets [count . index ]
156+ type = " A"
157+
158+ alias {
159+ name = aws_cloudfront_distribution. client_assets_distribution [local . rootDomainBuckets [count . index ]]. domain_name
160+ zone_id = aws_cloudfront_distribution. client_assets_distribution [local . rootDomainBuckets [count . index ]]. hosted_zone_id
161+ evaluate_target_health = false
162+ }
163+ }
164+
165+ # Subdomains to point at CF
166+ resource "aws_route53_record" "client_assets_subdomain" {
167+ count = length (local. subDomainBuckets )
138168
139169 zone_id = var. route53_zone_id
140- name = each . value
170+ name = local . subDomainBuckets [ count . index ]
141171 type = " CNAME"
142172 ttl = " 120"
143- records = [aws_cloudfront_distribution . client_assets_distribution [each . value ]. domain_name ]
173+ records = [aws_cloudfront_distribution . client_assets_distribution [local . subDomainBuckets [ count . index ] ]. domain_name ]
144174}
0 commit comments