Skip to content

Commit

Permalink
improved route53/zone module scalability (#44)
Browse files Browse the repository at this point in the history
* 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 <zachreborn@users.noreply.github.com>
  • Loading branch information
zachreborn and zachreborn committed Mar 7, 2024
1 parent f2e60ff commit 272ab0b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 36 deletions.
51 changes: 42 additions & 9 deletions modules/aws/route53/zone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,20 @@

<!-- USAGE EXAMPLES -->
## 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"
Expand All @@ -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)_

<p align="right">(<a href="#readme-top">back to top</a>)</p>
Expand Down Expand Up @@ -113,18 +149,15 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_comment"></a> [comment](#input\_comment) | (Optional) A comment for the hosted zone. Defaults to 'Managed by Terraform'. | `string` | `"Managed by Terraform"` | no |
| <a name="input_delegation_set_id"></a> [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 |
| <a name="input_name"></a> [name](#input\_name) | (Required) This is the name of the hosted zone. | `string` | n/a | yes |
| <a name="input_tags"></a> [tags](#input\_tags) | (Optional) A map of tags to assign to the zone. | `map(any)` | <pre>{<br> "terraform": true<br>}</pre> | no |
| <a name="input_vpc"></a> [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 |
| <a name="input_zones"></a> [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. | <pre>map(object({<br> comment = optional(string) # (Optional) A comment for the hosted zone. Defaults to 'Managed by Terraform'.<br> 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.<br> }))</pre> | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_name_servers"></a> [name\_servers](#output\_name\_servers) | n/a |
| <a name="output_zone_id"></a> [zone\_id](#output\_zone\_id) | n/a |
| <a name="output_name_servers"></a> [name\_servers](#output\_name\_servers) | A map of zones and their list of name servers. |
| <a name="output_zone_ids"></a> [zone\_ids](#output\_zone\_ids) | A map of zones and their zone IDs. |
<!-- END_TF_DOCS -->

<!-- LICENSE -->
Expand Down
7 changes: 4 additions & 3 deletions modules/aws/route53/zone/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 9 additions & 3 deletions modules/aws/route53/zone/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
39 changes: 18 additions & 21 deletions modules/aws/route53/zone/variables.tf
Original file line number Diff line number Diff line change
@@ -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."
Expand All @@ -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" = {
# }
# }
}

0 comments on commit 272ab0b

Please sign in to comment.