Skip to content

Commit

Permalink
Merge pull request #77 from hackforla/cb/example-dns-config
Browse files Browse the repository at this point in the history
update route 53 configuration to handle root and subdomain DNS records
  • Loading branch information
chelseybeck authored Jun 20, 2024
2 parents 855cff9 + deae389 commit cf5494b
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 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,5 @@ 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"
}

0 comments on commit cf5494b

Please sign in to comment.