From 272ab0b03f7d33c2eb2d84574740a25ec0286209 Mon Sep 17 00:00:00 2001 From: Zachary Hill Date: Thu, 7 Mar 2024 09:26:24 -0700 Subject: [PATCH] improved route53/zone module scalability (#44) * improved route53/zone module scalability * Continuous Integration - terraform fmt and terraform-docs * simplified the api for zone * Continuous Integration - terraform fmt and terraform-docs * updated outputs to match for_each changes * Continuous Integration - terraform fmt and terraform-docs * renaming the output * Continuous Integration - terraform fmt and terraform-docs * added new documentation --------- Co-authored-by: zachreborn --- modules/aws/route53/zone/README.md | 51 ++++++++++++++++++++++----- modules/aws/route53/zone/main.tf | 7 ++-- modules/aws/route53/zone/outputs.tf | 12 +++++-- modules/aws/route53/zone/variables.tf | 39 ++++++++++---------- 4 files changed, 73 insertions(+), 36 deletions(-) diff --git a/modules/aws/route53/zone/README.md b/modules/aws/route53/zone/README.md index fb6f591f..d3643f3d 100644 --- a/modules/aws/route53/zone/README.md +++ b/modules/aws/route53/zone/README.md @@ -62,13 +62,20 @@ ## Usage - +### Simple Usage +This example configures two hosted zones with a comment and tags for each zone. ``` module "route53_zone" { source = "github.com/zachreborn/terraform-modules//modules/aws/route53/zone" - comment = "ThinkStack primary domain" - name = "thinkstack.co" + zones = { + "example.com" = { + comment = "example.com" + } + "example.net" = { + comment = "example.net" + } + } tags = { terraform = "yes" @@ -79,6 +86,35 @@ module "route53_zone" { } ``` +### Advanced Usage +This example shows how to use the module with a variable and a map of objects. This allows for easier readability and maintainability of the code. +``` +module "route53_zones" { + source = "github.com/zachreborn/terraform-modules//modules/aws/route53/zone" + + zones = var.zones + tags = { + created_by = "Zachary Hill" + role = "external dns" + } +} + +variable "zones" { + type = map(object({ + comment = optional(string) + delegation_set_id = optional(string) + })) + default = { + "example.com" = { + comment = "example.com" + } + "example.net" = { + comment = "Not in use" + } + } +} +``` + _For more examples, please refer to the [Documentation](https://github.com/zachreborn/terraform-modules)_

(back to top)

@@ -113,18 +149,15 @@ No modules. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| [comment](#input\_comment) | (Optional) A comment for the hosted zone. Defaults to 'Managed by Terraform'. | `string` | `"Managed by Terraform"` | no | -| [delegation\_set\_id](#input\_delegation\_set\_id) | (Optional) The ID of the reusable delegation set whose NS records you want to assign to the hosted zone. Conflicts with vpc as delegation sets can only be used for public zones. | `string` | `null` | no | -| [name](#input\_name) | (Required) This is the name of the hosted zone. | `string` | n/a | yes | | [tags](#input\_tags) | (Optional) A map of tags to assign to the zone. | `map(any)` |
{
"terraform": true
}
| no | -| [vpc](#input\_vpc) | (Optional) Configuration block(s) specifying VPC(s) to associate with a private hosted zone. Conflicts with the delegation\_set\_id argument in this resource and any aws\_route53\_zone\_association resource specifying the same zone ID. Detailed below. | `string` | `null` | no | +| [zones](#input\_zones) | (Required) A map of hosted zone objects. The key is the name of the hosted zone. Values are the zone configuration settings. |
map(object({
comment = optional(string) # (Optional) A comment for the hosted zone. Defaults to 'Managed by Terraform'.
delegation_set_id = optional(string) # (Optional) The ID of the reusable delegation set whose NS records you want to assign to the hosted zone. Conflicts with vpc as delegation sets can only be used for public zones.
}))
| n/a | yes | ## Outputs | Name | Description | |------|-------------| -| [name\_servers](#output\_name\_servers) | n/a | -| [zone\_id](#output\_zone\_id) | n/a | +| [name\_servers](#output\_name\_servers) | A map of zones and their list of name servers. | +| [zone\_ids](#output\_zone\_ids) | A map of zones and their zone IDs. | diff --git a/modules/aws/route53/zone/main.tf b/modules/aws/route53/zone/main.tf index 3fb36c73..1205d960 100644 --- a/modules/aws/route53/zone/main.tf +++ b/modules/aws/route53/zone/main.tf @@ -9,8 +9,9 @@ terraform { } resource "aws_route53_zone" "zone" { - comment = var.comment - delegation_set_id = var.delegation_set_id - name = var.name + for_each = var.zones + comment = each.value.comment + delegation_set_id = each.value.delegation_set_id + name = each.key tags = var.tags } diff --git a/modules/aws/route53/zone/outputs.tf b/modules/aws/route53/zone/outputs.tf index a91a1b67..6a8a0a5f 100644 --- a/modules/aws/route53/zone/outputs.tf +++ b/modules/aws/route53/zone/outputs.tf @@ -1,7 +1,13 @@ output "name_servers" { - value = aws_route53_zone.zone.name_servers + description = "A map of zones and their list of name servers." + value = { + for zone in aws_route53_zone.zone : zone.name => zone.name_servers + } } -output "zone_id" { - value = aws_route53_zone.zone.zone_id +output "zone_ids" { + description = "A map of zones and their zone IDs." + value = { + for zone in aws_route53_zone.zone : zone.name => zone.zone_id + } } diff --git a/modules/aws/route53/zone/variables.tf b/modules/aws/route53/zone/variables.tf index a2eb2bd1..879a03da 100644 --- a/modules/aws/route53/zone/variables.tf +++ b/modules/aws/route53/zone/variables.tf @@ -1,20 +1,3 @@ -variable "comment" { - type = string - description = "(Optional) A comment for the hosted zone. Defaults to 'Managed by Terraform'." - default = "Managed by Terraform" -} - -variable "delegation_set_id" { - type = string - description = "(Optional) The ID of the reusable delegation set whose NS records you want to assign to the hosted zone. Conflicts with vpc as delegation sets can only be used for public zones." - default = null -} - -variable "name" { - type = string - description = "(Required) This is the name of the hosted zone." -} - variable "tags" { type = map(any) description = "(Optional) A map of tags to assign to the zone." @@ -23,8 +6,22 @@ variable "tags" { } } -variable "vpc" { - type = string - description = "(Optional) Configuration block(s) specifying VPC(s) to associate with a private hosted zone. Conflicts with the delegation_set_id argument in this resource and any aws_route53_zone_association resource specifying the same zone ID. Detailed below." - default = null +variable "zones" { + type = map(object({ + comment = optional(string) # (Optional) A comment for the hosted zone. Defaults to 'Managed by Terraform'. + delegation_set_id = optional(string) # (Optional) The ID of the reusable delegation set whose NS records you want to assign to the hosted zone. Conflicts with vpc as delegation sets can only be used for public zones. + })) + description = "(Required) A map of hosted zone objects. The key is the name of the hosted zone. Values are the zone configuration settings." + # Example: + # zones = { + # "example.com" = { + # comment = "example.com" + # delegation_set_id = null + # }, + # "example.net" = { + # comment = "example.net" + # }, + # "example.org" = { + # } + # } }