Skip to content

Commit

Permalink
update route 53 configuration to correctly handle root and subdomain …
Browse files Browse the repository at this point in the history
…DNS records
  • Loading branch information
chelseybeck committed Jun 3, 2024
1 parent 855cff9 commit 55193c9
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions terraform-modules/service/r53.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@ data "aws_route53_zone" "selected" {
private_zone = false
}

resource "aws_route53_record" "www" {
for_each = var.aws_managed_dns ? toset(var.host_names) : []
# Resource for root domain A record with alias
resource "aws_route53_record" "root" {
count = var.aws_managed_dns && length(regexall("[^.]*?.[^.]*?$", var.host_names[0])) > 0 ? 1 : 0

zone_id = data.aws_route53_zone.selected[0].zone_id
name = regexall("[^.]*?.[^.]*?$", var.host_names[0])[0]
type = "A"

alias {
name = var.alb_external_dns
zone_id = data.aws_route53_zone.selected[0].zone_id
evaluate_target_health = false
}
}

# Resource for subdomain CNAME records
resource "aws_route53_record" "subdomain" {
for_each = var.aws_managed_dns ? { for v in var.host_names : v => v if v != regexall("[^.]*?.[^.]*?$", var.host_names[0])[0] } : {}

zone_id = data.aws_route53_zone.selected[0].zone_id
name = each.value
Expand All @@ -21,5 +37,10 @@ variable "alb_external_dns" {

variable "aws_managed_dns" {
type = bool
description = "flag to either create record if domain is managed in AWS or output ALB DNS for user to manually create"
description = "Flag to either create record if domain is managed in AWS or output ALB DNS for user to manually create"
}

variable "host_names" {
description = "List of host names including the root domain and subdomains"
type = list(string)
}

0 comments on commit 55193c9

Please sign in to comment.