Skip to content

Commit

Permalink
alias and non alias must be seperated before compute
Browse files Browse the repository at this point in the history
The count value depends on resource attributes that cannot be determined
Therefore it must be clear which are alias records and which are not.
  • Loading branch information
etwillbefine committed Oct 6, 2019
1 parent 75bfe59 commit 5aab864
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module "zone" {
|-------------------------|-----------------------------------------------------------|---------|
| hosted_zone | Name of the existing hosted zone | - |
| is_private_zone | Whether the hosted zone is private or public | false |
| records | Object of ttl, name, values, overwrite and type to describe a record | - |
| records | Object of ttl, name, values, overwrite and type to describe a record | [] |
| alias_records | Object of name, overwrite and alias to describe a record | [] |

You can find an example [here](terraform.tfvars.example)
29 changes: 12 additions & 17 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,26 @@ data "terraform_remote_state" "alias" {
}
}

locals {
records = [for record in var.records : record if lookup(record, "alias", "") == ""]
alias_records = [for record in var.records : record if lookup(record, "alias", "") != ""]
}

resource "aws_route53_record" "record" {
count = length(local.records)
count = length(var.records)
zone_id = data.aws_route53_zone.zone.zone_id
name = format("%s.%s", lookup(local.records[count.index], "name"), var.hosted_zone)
type = lookup(local.records[count.index], "type", "A")
ttl = lookup(local.records[count.index], "ttl", 600)
records = lookup(local.records[count.index], "values", [])
allow_overwrite = lookup(local.records[count.index], "overwrite", true)
name = format("%s.%s", lookup(var.records[count.index], "name"), var.hosted_zone)
type = lookup(var.records[count.index], "type", "A")
ttl = lookup(var.records[count.index], "ttl", 600)
records = lookup(var.records[count.index], "values", [])
allow_overwrite = lookup(var.records[count.index], "overwrite", true)
}

resource "aws_route53_record" "alias_record" {
count = length(local.alias_records)
count = length(var.alias_records)
zone_id = data.aws_route53_zone.zone.zone_id
name = format("%s.%s", lookup(local.alias_records[count.index], "name"), var.hosted_zone)
type = lookup(local.alias_records[count.index], "type", "A")
allow_overwrite = lookup(local.alias_records[count.index], "overwrite", true)
name = format("%s.%s", lookup(var.alias_records[count.index], "name"), var.hosted_zone)
type = lookup(var.alias_records[count.index], "type", "A")
allow_overwrite = lookup(var.alias_records[count.index], "overwrite", true)

alias {
name = lookup(local.alias_records[count.index], "alias", join("", data.terraform_remote_state.alias.*.outputs.dns_name))
zone_id = lookup(local.alias_records[count.index], "alias_zone", join("", data.terraform_remote_state.alias.*.outputs.zone_id))
name = lookup(var.alias_records[count.index], "alias", join("", data.terraform_remote_state.alias.*.outputs.dns_name))
zone_id = lookup(var.alias_records[count.index], "alias_zone", join("", data.terraform_remote_state.alias.*.outputs.zone_id))
evaluate_target_health = true
}
}
Expand Down
5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ variable "records" {
description = "Objects of records to add to the hosted zone"
}

variable "alias_records" {
type = list
description = "Objects of alias records to add to the hosted zone"
}

variable "alias_module_state" {
type = string
default = ""
Expand Down

0 comments on commit 5aab864

Please sign in to comment.