diff --git a/lb-domain/main.tf b/lb-domain/main.tf index 36ceda3..6a4acd5 100644 --- a/lb-domain/main.tf +++ b/lb-domain/main.tf @@ -5,13 +5,17 @@ data "aws_lb" "ingress" { } } -module "records" { - source = "terraform-aws-modules/route53/aws//modules/records" - version = "~> 2.11" - - zone_name = var.route53_zone_name +data "aws_lb" "custom_domains_ingress" { + count = var.create_custom_domain_record ? 1 : 0 + tags = { + Namespace = var.namespace + Stage = var.stage + custom_domains= "true" + } +} - records = [ +locals { + main_dns_records = [ { name = "*" type = "CNAME" @@ -21,4 +25,28 @@ module "records" { ] } ] + + custom_domain_dns_record = var.create_custom_domain_record ? { + name = var.custom_domain_record_value + type = "CNAME" + ttl = 300 + records = [ + data.aws_lb.custom_domains_ingress.0.dns_name + ] + } : null + + dns_records = concat( + local.main_dns_records, + var.create_custom_domain_record ? [ local.custom_domain_dns_record ] : [] + ) +} + + +module "records" { + source = "terraform-aws-modules/route53/aws//modules/records" + version = "~> 2.11" + + zone_name = var.route53_zone_name + + records = local.dns_records } diff --git a/lb-domain/variables.tf b/lb-domain/variables.tf index 3b5d40e..964919f 100644 --- a/lb-domain/variables.tf +++ b/lb-domain/variables.tf @@ -13,6 +13,18 @@ variable "route53_zone_name" { description = "The name of the Route53 zone" } +variable "create_custom_domain_record" { + type = bool + description = "Define if a custom domain record should be created" + default = false +} + +variable "custom_domain_record_value" { + type = string + description = "The value of the custom domain record" + default = "custom-loadbalancer" +} + variable "tags" { type = map(string) description = "A map of tags to add to all resources"