Skip to content

Commit d9dc763

Browse files
committed
Enable route 53 alias records to support weighted routing policy
1 parent 672b546 commit d9dc763

6 files changed

Lines changed: 109 additions & 6 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,27 @@ module "ingress" {
176176
}
177177
```
178178

179+
To create weighted Route 53 aliases, set `alias_weighted_routing`. This applies
180+
to every alias created by the module instance, and is intended for coordinating
181+
peer weighted records created from separate Terraform states or applies.
182+
183+
``` terraform
184+
module "ingress" {
185+
source = "..."
186+
187+
primary_domain_name = "www.example.com"
188+
hosted_zone_name = "example.com"
189+
190+
alias_weighted_routing = {
191+
weight = 10
192+
set_identifier = "primary"
193+
}
194+
}
195+
```
196+
197+
For a given Route 53 weighted record set, `set_identifier` must be unique for
198+
the combination of hosted zone, record name, and record type.
199+
179200
<!-- BEGIN_TF_DOCS -->
180201
## Requirements
181202

@@ -216,6 +237,7 @@ module "ingress" {
216237
| <a name="input_alarm_evaluation_minutes"></a> [alarm\_evaluation\_minutes](#input\_alarm\_evaluation\_minutes) | Number of minutes of alarm state until triggering an alarm | `number` | `2` | no |
217238
| <a name="input_allow_overwrite"></a> [allow\_overwrite](#input\_allow\_overwrite) | Allow overwriting of existing DNS records | `bool` | `false` | no |
218239
| <a name="input_attach_certificate_domains"></a> [attach\_certificate\_domains](#input\_attach\_certificate\_domains) | Additional existing certificates which should be attached | `list(string)` | `[]` | no |
240+
| <a name="input_alias_weighted_routing"></a> [alias\_weighted\_routing](#input\_alias\_weighted\_routing) | Optional weighted routing configuration for Route 53 aliases | <pre>object({<br> weight = number<br> set_identifier = string<br> })</pre> | `null` | no |
219241
| <a name="input_certificate_types"></a> [certificate\_types](#input\_certificate\_types) | Types of certificates to look for (default: AMAZON\_ISSUED) | `list(string)` | <pre>[<br> "AMAZON_ISSUED"<br>]</pre> | no |
220242
| <a name="input_create_domain_aliases"></a> [create\_domain\_aliases](#input\_create\_domain\_aliases) | List of domains for which alias records should be created | `list(string)` | n/a | yes |
221243
| <a name="input_description"></a> [description](#input\_description) | Human description for this load balancer | `string` | n/a | yes |

main.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ module "alias" {
6161
providers = { aws = aws.route53 }
6262
source = "./modules/alb-route53-alias"
6363

64-
alb_dns_name = module.alb.dns_name
65-
alb_zone_id = module.alb.zone_id
66-
allow_overwrite = var.allow_overwrite
67-
hosted_zone_name = var.hosted_zone_name
68-
name = each.value
64+
alb_dns_name = module.alb.dns_name
65+
alb_zone_id = module.alb.zone_id
66+
alias_weighted_routing = var.alias_weighted_routing
67+
allow_overwrite = var.allow_overwrite
68+
hosted_zone_name = var.hosted_zone_name
69+
name = each.value
6970
}
7071

7172
module "target_group" {

modules/alb-route53-alias/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ No modules.
2121
| Name | Type |
2222
|------|------|
2323
| [aws_route53_record.load_balancer](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
24+
| [aws_route53_record.weighted_load_balancer](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
2425
| [aws_route53_zone.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
2526

2627
## Inputs
@@ -30,6 +31,7 @@ No modules.
3031
| <a name="input_alb_dns_name"></a> [alb\_dns\_name](#input\_alb\_dns\_name) | DNS name for the ALB for which an alias should be created | `string` | n/a | yes |
3132
| <a name="input_alb_zone_id"></a> [alb\_zone\_id](#input\_alb\_zone\_id) | Route53 zone for the ALB for which an alias should be created | `string` | n/a | yes |
3233
| <a name="input_allow_overwrite"></a> [allow\_overwrite](#input\_allow\_overwrite) | Allow overwriting of existing DNS records | `bool` | `false` | no |
34+
| <a name="input_alias_weighted_routing"></a> [alias\_weighted\_routing](#input\_alias\_weighted\_routing) | Optional weighted routing configuration for the Route 53 alias | <pre>object({<br> weight = number<br> set_identifier = string<br> })</pre> | `null` | no |
3335
| <a name="input_hosted_zone_name"></a> [hosted\_zone\_name](#input\_hosted\_zone\_name) | Hosted zone for AWS Route53 | `string` | `null` | no |
3436
| <a name="input_name"></a> [name](#input\_name) | Name of the Route 53 alias (example: www) | `string` | n/a | yes |
3537

modules/alb-route53-alias/main.tf

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
resource "aws_route53_record" "load_balancer" {
2-
for_each = toset(var.hosted_zone_name == null ? [] : [var.hosted_zone_name])
2+
for_each = toset(
3+
var.hosted_zone_name == null || var.alias_weighted_routing != null ?
4+
[] :
5+
[var.hosted_zone_name]
6+
)
37

48
allow_overwrite = var.allow_overwrite
59
name = var.name
@@ -13,6 +17,30 @@ resource "aws_route53_record" "load_balancer" {
1317
}
1418
}
1519

20+
resource "aws_route53_record" "weighted_load_balancer" {
21+
for_each = (
22+
var.hosted_zone_name == null || var.alias_weighted_routing == null ?
23+
{} :
24+
{ (var.hosted_zone_name) = var.alias_weighted_routing }
25+
)
26+
27+
allow_overwrite = var.allow_overwrite
28+
name = var.name
29+
type = "A"
30+
zone_id = data.aws_route53_zone.this[each.key].zone_id
31+
set_identifier = each.value.set_identifier
32+
33+
weighted_routing_policy {
34+
weight = each.value.weight
35+
}
36+
37+
alias {
38+
evaluate_target_health = true
39+
name = var.alb_dns_name
40+
zone_id = var.alb_zone_id
41+
}
42+
}
43+
1644
data "aws_route53_zone" "this" {
1745
for_each = toset(var.hosted_zone_name == null ? [] : [var.hosted_zone_name])
1846

modules/alb-route53-alias/variables.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,31 @@ variable "allow_overwrite" {
1414
description = "Allow overwriting of existing DNS records"
1515
}
1616

17+
variable "alias_weighted_routing" {
18+
description = "Optional weighted routing configuration for the Route 53 alias"
19+
type = object({
20+
weight = number
21+
set_identifier = string
22+
})
23+
default = null
24+
25+
validation {
26+
condition = (
27+
var.alias_weighted_routing == null ||
28+
var.alias_weighted_routing.weight >= 0
29+
)
30+
error_message = "alias_weighted_routing.weight must be greater than or equal to 0."
31+
}
32+
33+
validation {
34+
condition = (
35+
var.alias_weighted_routing == null ||
36+
trimspace(var.alias_weighted_routing.set_identifier) != ""
37+
)
38+
error_message = "alias_weighted_routing.set_identifier must not be empty."
39+
}
40+
}
41+
1742
variable "name" {
1843
description = "Name of the Route 53 alias (example: www)"
1944
type = string

variables.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ variable "allow_overwrite" {
1616
description = "Allow overwriting of existing DNS records"
1717
}
1818

19+
variable "alias_weighted_routing" {
20+
description = "Optional weighted routing configuration for Route 53 aliases"
21+
type = object({
22+
weight = number
23+
set_identifier = string
24+
})
25+
default = null
26+
27+
validation {
28+
condition = (
29+
var.alias_weighted_routing == null ||
30+
var.alias_weighted_routing.weight >= 0
31+
)
32+
error_message = "alias_weighted_routing.weight must be greater than or equal to 0."
33+
}
34+
35+
validation {
36+
condition = (
37+
var.alias_weighted_routing == null ||
38+
trimspace(var.alias_weighted_routing.set_identifier) != ""
39+
)
40+
error_message = "alias_weighted_routing.set_identifier must not be empty."
41+
}
42+
}
43+
1944
variable "attach_certificate_domains" {
2045
description = "Additional existing certificates which should be attached"
2146
type = list(string)

0 commit comments

Comments
 (0)