Skip to content

Commit

Permalink
feat: feature weighted and other record policies
Browse files Browse the repository at this point in the history
  • Loading branch information
anmolnagpal committed Sep 20, 2023
1 parent 9917261 commit 9625565
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 82 deletions.
25 changes: 25 additions & 0 deletions _example/complete/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions _example/complete/example.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
provider "aws" {
region = "eu-west-1"
}

module "route53" {
source = "../../"

name = "route53"
environment = "test"
label_order = ["environment", "name"]
public_enabled = true
record_enabled = true

domain_name = "clouddrove.com"

records = [
{
name = ""
type = "A"
ttl = 3600
records = [
"10.10.10.10",
]
},
{
name = "geo"
type = "CNAME"
ttl = 5
records = ["europe.test.clouddrove.com."]
set_identifier = "europe"
geolocation_routing_policy = {
continent = "EU"
}
},
{
name = "test"
type = "CNAME"
ttl = 5
records = ["test.clouddrove.com."]
set_identifier = "test-primary"
weighted_routing_policy = {
weight = 90
}
},
{
name = "test"
type = "CNAME"
ttl = 5
records = ["test.clouddrove.com."]
set_identifier = "test-secondary"
weighted_routing_policy = {
weight = 10
}
}
]
}
9 changes: 9 additions & 0 deletions _example/complete/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "id" {
value = module.route53.*.zone_id
description = "The ID of the Hostzone."
}

output "tags" {
value = module.route53.tags
description = "A mapping of tags to assign to the resource."
}
10 changes: 10 additions & 0 deletions _example/complete/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.5.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.48.0"
}
}
}
2 changes: 1 addition & 1 deletion _example/vpc-association/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ provider "aws" {
module "route53" {
source = "../../"

enabled = true
name = "route53"
environment = "test"
label_order = ["environment", "name"]
private_enabled = true
enabled = true

domain_name = "clouddrove.com"
vpc_id = "vpc-xxxxxxxxxxxxxx"
Expand Down
97 changes: 68 additions & 29 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
## Managed By : CloudDrove
## Copyright @ CloudDrove. All Right Reserved.

locals {
# Terragrunt users have to provide `records_jsonencoded` as jsonencode()'d string.
# See details: https://github.com/gruntwork-io/terragrunt/issues/1211
records = concat(var.records, try(jsondecode(var.records_jsonencoded), []))

# Convert `records` from list to map with unique keys
recordsets = { for rs in local.records : try(rs.key, join(" ", compact(["${rs.name} ${rs.type}", try(rs.set_identifier, "")]))) => rs }
zone_id = var.zone_id != "" ? var.zone_id : (var.private_enabled ? aws_route53_zone.private.*.zone_id[0] : aws_route53_zone.public.*.zone_id[0])
}

#Module : label
#Description : This terraform module is designed to generate consistent label names and tags
# for resources. You can use terraform-labels to implement a strict naming
Expand Down Expand Up @@ -47,42 +57,71 @@ resource "aws_route53_zone" "public" {

# Module : Route53 Record Set
# Description : Terraform module to create Route53 record sets resource on AWS.
resource "aws_route53_record" "default" {
count = var.record_enabled && length(var.ttls) > 0 ? length(var.ttls) : 0
zone_id = var.zone_id != "" ? var.zone_id : (var.private_enabled ? aws_route53_zone.private.*.zone_id[0] : aws_route53_zone.public.*.zone_id[0])
name = element(var.names, count.index)
type = element(var.types, count.index)
ttl = element(var.ttls, count.index)
records = split(",", element(var.values, count.index))
set_identifier = length(var.set_identifiers) > 0 ? element(var.set_identifiers, count.index) : ""
health_check_id = length(var.health_check_ids) > 0 ? element(var.health_check_ids, count.index) : ""
multivalue_answer_routing_policy = length(var.multivalue_answer_routing_policies) > 0 ? element(var.multivalue_answer_routing_policies, count.index) : null
allow_overwrite = length(var.allow_overwrites) > 0 ? element(var.allow_overwrites, count.index) : false
}

# Module : Route53 Record Set
# Description : Terraform module to create Route53 record sets resource on AWS.
resource "aws_route53_record" "alias" {
count = var.record_enabled && length(var.alias) > 0 && length(var.alias["names"]) > 0 ? length(var.alias["names"]) : 0
zone_id = var.zone_id
name = element(var.names, count.index)
type = element(var.types, count.index)
set_identifier = length(var.set_identifiers) > 0 ? element(var.set_identifiers, count.index) : ""
health_check_id = length(var.health_check_ids) > 0 ? element(var.health_check_ids, count.index) : ""
multivalue_answer_routing_policy = length(var.multivalue_answer_routing_policies) > 0 ? element(var.multivalue_answer_routing_policies, count.index) : null
allow_overwrite = length(var.allow_overwrites) > 0 ? element(var.allow_overwrites, count.index) : false
alias {
name = length(var.alias) > 0 ? element(var.alias["names"], count.index) : ""
zone_id = length(var.alias) > 0 ? element(var.alias["zone_ids"], count.index) : ""
evaluate_target_health = length(var.alias) > 0 ? element(var.alias["evaluate_target_healths"], count.index) : false
resource "aws_route53_record" "this" {
for_each = { for k, v in local.recordsets : k => v if var.record_enabled && (local.zone_id != null || var.domain_name != null) }

zone_id = local.zone_id

name = each.value.name != "" ? (lookup(each.value, "full_name_override", false) ? each.value.name : "${each.value.name}.${var.domain_name}") : var.domain_name
type = each.value.type
ttl = lookup(each.value, "ttl", null)
records = try(each.value.records, null)
set_identifier = lookup(each.value, "set_identifier", null)
health_check_id = lookup(each.value, "health_check_id", null)
multivalue_answer_routing_policy = lookup(each.value, "multivalue_answer_routing_policy", null)
allow_overwrite = lookup(each.value, "allow_overwrite", false)

dynamic "alias" {
for_each = length(keys(lookup(each.value, "alias", {}))) == 0 ? [] : [true]

content {
name = each.value.alias.name
zone_id = try(each.value.alias.zone_id, local.zone_id)
evaluate_target_health = lookup(each.value.alias, "evaluate_target_health", false)
}
}

dynamic "failover_routing_policy" {
for_each = length(keys(lookup(each.value, "failover_routing_policy", {}))) == 0 ? [] : [true]

content {
type = each.value.failover_routing_policy.type
}
}

dynamic "latency_routing_policy" {
for_each = length(keys(lookup(each.value, "latency_routing_policy", {}))) == 0 ? [] : [true]

content {
region = each.value.latency_routing_policy.region
}
}

dynamic "weighted_routing_policy" {
for_each = length(keys(lookup(each.value, "weighted_routing_policy", {}))) == 0 ? [] : [true]

content {
weight = each.value.weighted_routing_policy.weight
}
}

dynamic "geolocation_routing_policy" {
for_each = length(keys(lookup(each.value, "geolocation_routing_policy", {}))) == 0 ? [] : [true]

content {
continent = lookup(each.value.geolocation_routing_policy, "continent", null)
country = lookup(each.value.geolocation_routing_policy, "country", null)
subdivision = lookup(each.value.geolocation_routing_policy, "subdivision", null)
}
}
}

# Module : Route53
# Description : Terraform module to create Route53 record sets resource on AWS for Weighted
# Routing Policy.
resource "aws_route53_zone_association" "default" {
count = var.enabled ? 1 : 0
zone_id = var.private_enabled ? aws_route53_zone.private.*.zone_id[0] : aws_route53_zone.public.*.zone_id[0]
count = var.enabled && var.private_enabled ? 1 : 0
zone_id = aws_route53_zone.private.*.zone_id[0]
vpc_id = var.secondary_vpc_id
}
56 changes: 4 additions & 52 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ variable "repository" {
}
}


variable "label_order" {
type = list(any)
default = []
Expand Down Expand Up @@ -63,11 +62,6 @@ variable "tags" {

# Module : Route53
# Description : Terraform Route53 module variables.
variable "private_enabled" {
type = bool
default = false
description = "Whether to create private Route53 zone."
}

variable "record_enabled" {
type = bool
Expand All @@ -87,28 +81,10 @@ variable "record_set_enabled" {
description = "Whether to create seperate Route53 record set."
}

variable "failover_enabled" {
type = bool
default = false
description = "Whether to create Route53 record set."
}

variable "latency_enabled" {
type = bool
default = false
description = "Whether to create Route53 record set."
}

variable "geolocation_enabled" {
type = bool
default = false
description = "Whether to create Route53 record set."
}

variable "weighted_enabled" {
type = bool
default = false
description = "Whether to create Route53 record set."
variable "records" {
description = "List of objects of DNS records"
type = any
default = []
}

variable "domain_name" {
Expand Down Expand Up @@ -146,42 +122,18 @@ variable "types" {
description = "The record type. Valid values are A, AAAA, CAA, CNAME, MX, NAPTR, NS, PTR, SOA, SPF, SRV and TXT. "
}

variable "ttls" {
type = list(any)
default = []
description = "(Required for non-alias records) The TTL of the record."
}

variable "names" {
type = list(any)
default = []
description = "The name of the record."
}

variable "values" {
type = list(any)
default = []
description = "(Required for non-alias records) A string list of records. To specify a single record value longer than 255 characters such as a TXT record for DKIM, add \"\" inside the Terraform configuration string (e.g. \"first255characters\"\"morecharacters\")."
}

variable "set_identifiers" {
type = list(any)
default = []
description = "Unique identifier to differentiate records with routing policies from one another. Required if using failover, geolocation, latency, or weighted routing policies documented below."
}

variable "health_check_ids" {
type = list(any)
default = []
description = "The health check the record should be associated with."
}

variable "alias" {
type = map(any)
default = { "names" = [], "zone_ids" = [], "evaluate_target_healths" = [] }
description = "An alias block. Conflicts with ttl & records. Alias record documented below."
}

variable "failover_routing_policies" {
default = null
description = "A block indicating the routing behavior when associated health check fails. Conflicts with any other routing policy. Documented below."
Expand Down

0 comments on commit 9625565

Please sign in to comment.