Skip to content

Commit 7bfdddf

Browse files
authored
Support cross_zone strategy for target groups (#109)
1 parent 48c0b22 commit 7bfdddf

File tree

20 files changed

+194
-62
lines changed

20 files changed

+194
-62
lines changed

modules/alb-instance-target-group/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ This module creates following resources.
4040
| <a name="input_port"></a> [port](#input\_port) | (Required) The number of port on which targets receive traffic, unless overridden when registering a specific target. Valid values are either ports 1-65535. | `number` | n/a | yes |
4141
| <a name="input_protocol"></a> [protocol](#input\_protocol) | (Required) The protocol to use for routing traffic to the targets. Valid values are `HTTP` and `HTTPS`. Defaults to `HTTP`. | `string` | n/a | yes |
4242
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | (Required) The ID of the VPC which the target group belongs to. | `string` | n/a | yes |
43-
| <a name="input_anomaly_mitigation_enabled"></a> [anomaly\_mitigation\_enabled](#input\_anomaly\_mitigation\_enabled) | (Optional) Whether to enable target anomaly mitigation. When a target is determined to be anomalous, traffic is automatically routed away so the target has an opportunity to recover. Target anomaly mitigation is only supported by the `WEIGHTED_RANDOM` load balancing algorithm type. Not compatible with the `slow_start_duration` attribute. Defaults to `false`. | `bool` | `false` | no |
4443
| <a name="input_deregistration_delay"></a> [deregistration\_delay](#input\_deregistration\_delay) | (Optional) The time to wait for in-flight requests to complete while deregistering a target. During this time, the state of the target is draining. | `number` | `300` | no |
4544
| <a name="input_health_check"></a> [health\_check](#input\_health\_check) | (Optional) Health Check configuration block. The associated load balancer periodically sends requests to the registered targets to test their status. `health_check` block as defined below.<br> (Optional) `protocol` - Protocol to use to connect with the target. The possible values are `HTTP` and `HTTPS`. Defaults to `HTTP`.<br> (Optional) `port` - The port the load balancer uses when performing health checks on targets. The default is the port on which each target receives traffic from the load balancer. Valid values are either ports 1-65535.<br> (Optional) `port_override` - Whether to override the port on which each target receives trafficfrom the load balancer to a different port. Defaults to `false`.<br> (Optional) `path` - Use the default path of `/` to ping the root, or specify a custom path if preferred.<br> (Optional) `success_codes` - The HTTP codes to use when checking for a successful response from a target. You can specify multiple values (for example, `200,202`) or a range of values (for example, `200-299`).<br> (Optional) `healthy_threshold` - The number of consecutive health checks successes required before considering an unhealthy target healthy. Valid value range is 2 - 10. Defaults to `5`.<br> (Optional) `unhealthy_threshold` - The number of consecutive health check failures required before considering a target unhealthy. Valid value range is 2 - 10. Defaults to `2`.<br> (Optional) `interval` - Approximate amount of time, in seconds, between health checks of an individual target. Valid value range is 5 - 300. Defaults to `30`.<br> (Optional) `timeout` - The amount of time, in seconds, during which no response means a failed health check. Valid value range is 2 - 120. Defaults to `5`. | <pre>object({<br> protocol = optional(string, "HTTP")<br> port = optional(number, null)<br> port_override = optional(bool, false)<br> path = optional(string, null)<br> success_codes = optional(string, null)<br><br> healthy_threshold = optional(number, 5)<br> unhealthy_threshold = optional(number, 2)<br> interval = optional(number, 30)<br> timeout = optional(number, 5)<br> })</pre> | `{}` | no |
46-
| <a name="input_load_balancing_algorithm"></a> [load\_balancing\_algorithm](#input\_load\_balancing\_algorithm) | (Optional) Determines how the load balancer selects targets when routing requests. Valid values are `ROUND_ROBIN`, `LEAST_OUTSTANDING_REQUESTS` or `WEIGHTED_RANDOM`. Defaults to `ROUND_ROBIN`. | `string` | `"ROUND_ROBIN"` | no |
45+
| <a name="input_load_balancing"></a> [load\_balancing](#input\_load\_balancing) | (Optional) A load balancing configuration of the target group. `load_balancing` block as defined below.<br> (Optional) `algorithm` - Determines how the load balancer selects targets when routing requests. Valid values are `ROUND_ROBIN`, `LEAST_OUTSTANDING_REQUESTS` or `WEIGHTED_RANDOM`. Defaults to `ROUND_ROBIN`.<br> (Optional) `anomaly_mitigation_enabled` - Whether to enable target anomaly mitigation. When a target is determined to be anomalous, traffic is automatically routed away so the target has an opportunity to recover. Target anomaly mitigation is only supported by the `WEIGHTED_RANDOM` load balancing algorithm type. Not compatible with the `slow_start_duration` attribute. Defaults to `false`.<br> (Optional) `cross_zone_strategy` - Determines how the load balancer routes requests across the Availability Zones. Valid values are `ENABLED`, `DISABLED` or `INHERIT`. Defaults to `INHERIT` (Use load balancer configuration). | <pre>object({<br> algorithm = optional(string, "ROUND_ROBIN")<br> anomaly_mitigation_enabled = optional(bool, false)<br> cross_zone_strategy = optional(string, "INHERIT")<br> })</pre> | `{}` | no |
4746
| <a name="input_module_tags_enabled"></a> [module\_tags\_enabled](#input\_module\_tags\_enabled) | (Optional) Whether to create AWS Resource Tags for the module informations. | `bool` | `true` | no |
4847
| <a name="input_protocol_version"></a> [protocol\_version](#input\_protocol\_version) | (Optional) Use `HTTP1` to send requests to targets using HTTP/1.1. Supported when the request protocol is HTTP/1.1 or HTTP/2. Use `HTTP2` to send requests to targets using HTTP/2. Supported when the request protocol is HTTP/2 or gRPC, but gRPC-specific features are not available. Use `GRPC` to send requests to targets using gRPC. Supported when the request protocol is gRPC. Defaults to `HTTP1`. | `string` | `"HTTP1"` | no |
4948
| <a name="input_resource_group_description"></a> [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no |

modules/alb-instance-target-group/main.tf

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ locals {
1414
} : {}
1515
}
1616

17+
locals {
18+
cross_zone_strategy = {
19+
"ENABLED" = "true"
20+
"DISABLED" = "false"
21+
"INHERIT" = "use_load_balancer_configuration"
22+
}
23+
}
24+
25+
26+
###################################################
27+
# ALB Instance Target Group
28+
###################################################
1729

1830
# INFO: Not supported attributes
1931
# - `connection_termination`
@@ -33,12 +45,13 @@ resource "aws_lb_target_group" "this" {
3345

3446
## Attributes
3547
deregistration_delay = var.deregistration_delay
36-
load_balancing_algorithm_type = lower(var.load_balancing_algorithm)
37-
load_balancing_anomaly_mitigation = (var.load_balancing_algorithm == "WEIGHTED_RANDOM"
38-
? var.anomaly_mitigation_enabled ? "on" : "off"
48+
load_balancing_algorithm_type = lower(var.load_balancing.algorithm)
49+
load_balancing_anomaly_mitigation = (var.load_balancing.algorithm == "WEIGHTED_RANDOM"
50+
? var.load_balancing.anomaly_mitigation_enabled ? "on" : "off"
3951
: null
4052
)
41-
slow_start = var.slow_start_duration
53+
load_balancing_cross_zone_enabled = local.cross_zone_strategy[var.load_balancing.cross_zone_strategy]
54+
slow_start = var.slow_start_duration
4255

4356
stickiness {
4457
enabled = var.stickiness_enabled

modules/alb-instance-target-group/outputs.tf

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,16 @@ output "targets" {
6161
output "attributes" {
6262
description = "Attributes of the Instance target group of network load balancer."
6363
value = {
64-
anomaly_mitigation_enabled = (var.load_balancing_algorithm == "WEIGHTED_RANDOM"
65-
? var.anomaly_mitigation_enabled
66-
: null
67-
)
68-
deregistration_delay = aws_lb_target_group.this.deregistration_delay
69-
load_balancing_algorithm = upper(aws_lb_target_group.this.load_balancing_algorithm_type)
70-
slow_start_duration = aws_lb_target_group.this.slow_start
64+
deregistration_delay = aws_lb_target_group.this.deregistration_delay
65+
load_balancing = {
66+
algorithm = upper(aws_lb_target_group.this.load_balancing_algorithm_type)
67+
anomaly_mitigation_enabled = (var.load_balancing.algorithm == "WEIGHTED_RANDOM"
68+
? var.load_balancing.anomaly_mitigation_enabled
69+
: null
70+
)
71+
cross_zone_strategy = var.load_balancing.cross_zone_strategy
72+
}
73+
slow_start_duration = aws_lb_target_group.this.slow_start
7174
stickiness = {
7275
enabled = aws_lb_target_group.this.stickiness[0].enabled
7376
type = upper(aws_lb_target_group.this.stickiness[0].type)

modules/alb-instance-target-group/variables.tf

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,30 @@ variable "deregistration_delay" {
7777
}
7878
}
7979

80-
variable "load_balancing_algorithm" {
81-
description = "(Optional) Determines how the load balancer selects targets when routing requests. Valid values are `ROUND_ROBIN`, `LEAST_OUTSTANDING_REQUESTS` or `WEIGHTED_RANDOM`. Defaults to `ROUND_ROBIN`."
82-
type = string
83-
default = "ROUND_ROBIN"
84-
nullable = false
80+
variable "load_balancing" {
81+
description = <<EOF
82+
(Optional) A load balancing configuration of the target group. `load_balancing` block as defined below.
83+
(Optional) `algorithm` - Determines how the load balancer selects targets when routing requests. Valid values are `ROUND_ROBIN`, `LEAST_OUTSTANDING_REQUESTS` or `WEIGHTED_RANDOM`. Defaults to `ROUND_ROBIN`.
84+
(Optional) `anomaly_mitigation_enabled` - Whether to enable target anomaly mitigation. When a target is determined to be anomalous, traffic is automatically routed away so the target has an opportunity to recover. Target anomaly mitigation is only supported by the `WEIGHTED_RANDOM` load balancing algorithm type. Not compatible with the `slow_start_duration` attribute. Defaults to `false`.
85+
(Optional) `cross_zone_strategy` - Determines how the load balancer routes requests across the Availability Zones. Valid values are `ENABLED`, `DISABLED` or `INHERIT`. Defaults to `INHERIT` (Use load balancer configuration).
86+
EOF
87+
type = object({
88+
algorithm = optional(string, "ROUND_ROBIN")
89+
anomaly_mitigation_enabled = optional(bool, false)
90+
cross_zone_strategy = optional(string, "INHERIT")
91+
})
92+
default = {}
93+
nullable = false
8594

8695
validation {
87-
condition = contains(["ROUND_ROBIN", "LEAST_OUTSTANDING_REQUESTS", "WEIGHTED_RANDOM"], var.load_balancing_algorithm)
96+
condition = contains(["ROUND_ROBIN", "LEAST_OUTSTANDING_REQUESTS", "WEIGHTED_RANDOM"], var.load_balancing.algorithm)
8897
error_message = "Valid values are `ROUND_ROBIN`, `LEAST_OUTSTANDING_REQUESTS` and `WEIGHTED_RANDOM`."
8998
}
90-
}
9199

92-
variable "anomaly_mitigation_enabled" {
93-
description = "(Optional) Whether to enable target anomaly mitigation. When a target is determined to be anomalous, traffic is automatically routed away so the target has an opportunity to recover. Target anomaly mitigation is only supported by the `WEIGHTED_RANDOM` load balancing algorithm type. Not compatible with the `slow_start_duration` attribute. Defaults to `false`."
94-
type = bool
95-
default = false
96-
nullable = false
100+
validation {
101+
condition = contains(["ENABLED", "DISABLED", "INHERIT"], var.load_balancing.cross_zone_strategy)
102+
error_message = "Valid values are `ENABLED`, `DISABLED` and `INHERIT`."
103+
}
97104
}
98105

99106
variable "slow_start_duration" {

modules/alb-ip-target-group/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ This module creates following resources.
4141
| <a name="input_port"></a> [port](#input\_port) | (Required) The number of port on which targets receive traffic, unless overridden when registering a specific target. Valid values are either ports 1-65535. | `number` | n/a | yes |
4242
| <a name="input_protocol"></a> [protocol](#input\_protocol) | (Required) The protocol to use for routing traffic to the targets. Valid values are `HTTP` and `HTTPS`. Defaults to `HTTP`. | `string` | n/a | yes |
4343
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | (Required) The ID of the VPC which the target group belongs to. | `string` | n/a | yes |
44-
| <a name="input_anomaly_mitigation_enabled"></a> [anomaly\_mitigation\_enabled](#input\_anomaly\_mitigation\_enabled) | (Optional) Whether to enable target anomaly mitigation. When a target is determined to be anomalous, traffic is automatically routed away so the target has an opportunity to recover. Target anomaly mitigation is only supported by the `WEIGHTED_RANDOM` load balancing algorithm type. Not compatible with the `slow_start_duration` attribute. Defaults to `false`. | `bool` | `false` | no |
4544
| <a name="input_deregistration_delay"></a> [deregistration\_delay](#input\_deregistration\_delay) | (Optional) The time to wait for in-flight requests to complete while deregistering a target. During this time, the state of the target is draining. | `number` | `300` | no |
4645
| <a name="input_health_check"></a> [health\_check](#input\_health\_check) | (Optional) Health Check configuration block. The associated load balancer periodically sends requests to the registered targets to test their status. `health_check` block as defined below.<br> (Optional) `protocol` - Protocol to use to connect with the target. The possible values are `HTTP` and `HTTPS`. Defaults to `HTTP`.<br> (Optional) `port` - The port the load balancer uses when performing health checks on targets. The default is the port on which each target receives traffic from the load balancer. Valid values are either ports 1-65535.<br> (Optional) `port_override` - Whether to override the port on which each target receives trafficfrom the load balancer to a different port. Defaults to `false`.<br> (Optional) `path` - Use the default path of `/` to ping the root, or specify a custom path if preferred.<br> (Optional) `success_codes` - The HTTP codes to use when checking for a successful response from a target. You can specify multiple values (for example, `200,202`) or a range of values (for example, `200-299`).<br> (Optional) `healthy_threshold` - The number of consecutive health checks successes required before considering an unhealthy target healthy. Valid value range is 2 - 10. Defaults to `5`.<br> (Optional) `unhealthy_threshold` - The number of consecutive health check failures required before considering a target unhealthy. Valid value range is 2 - 10. Defaults to `2`.<br> (Optional) `interval` - Approximate amount of time, in seconds, between health checks of an individual target. Valid value range is 5 - 300. Defaults to `30`.<br> (Optional) `timeout` - The amount of time, in seconds, during which no response means a failed health check. Valid value range is 2 - 120. Defaults to `5`. | <pre>object({<br> protocol = optional(string, "HTTP")<br> port = optional(number, null)<br> port_override = optional(bool, false)<br> path = optional(string, null)<br> success_codes = optional(string, null)<br><br> healthy_threshold = optional(number, 5)<br> unhealthy_threshold = optional(number, 2)<br> interval = optional(number, 30)<br> timeout = optional(number, 5)<br> })</pre> | `{}` | no |
4746
| <a name="input_ip_address_type"></a> [ip\_address\_type](#input\_ip\_address\_type) | (Required) The type of IP addresses used by the target group. Valid values are `IPV4` or `IPV6`. | `string` | `"IPV4"` | no |
48-
| <a name="input_load_balancing_algorithm"></a> [load\_balancing\_algorithm](#input\_load\_balancing\_algorithm) | (Optional) Determines how the load balancer selects targets when routing requests. Valid values are `ROUND_ROBIN`, `LEAST_OUTSTANDING_REQUESTS` or `WEIGHTED_RANDOM`. Defaults to `ROUND_ROBIN`. | `string` | `"ROUND_ROBIN"` | no |
47+
| <a name="input_load_balancing"></a> [load\_balancing](#input\_load\_balancing) | (Optional) A load balancing configuration of the target group. `load_balancing` block as defined below.<br> (Optional) `algorithm` - Determines how the load balancer selects targets when routing requests. Valid values are `ROUND_ROBIN`, `LEAST_OUTSTANDING_REQUESTS` or `WEIGHTED_RANDOM`. Defaults to `ROUND_ROBIN`.<br> (Optional) `anomaly_mitigation_enabled` - Whether to enable target anomaly mitigation. When a target is determined to be anomalous, traffic is automatically routed away so the target has an opportunity to recover. Target anomaly mitigation is only supported by the `WEIGHTED_RANDOM` load balancing algorithm type. Not compatible with the `slow_start_duration` attribute. Defaults to `false`.<br> (Optional) `cross_zone_strategy` - Determines how the load balancer routes requests across the Availability Zones. Valid values are `ENABLED`, `DISABLED` or `INHERIT`. Defaults to `INHERIT` (Use load balancer configuration). | <pre>object({<br> algorithm = optional(string, "ROUND_ROBIN")<br> anomaly_mitigation_enabled = optional(bool, false)<br> cross_zone_strategy = optional(string, "INHERIT")<br> })</pre> | `{}` | no |
4948
| <a name="input_module_tags_enabled"></a> [module\_tags\_enabled](#input\_module\_tags\_enabled) | (Optional) Whether to create AWS Resource Tags for the module informations. | `bool` | `true` | no |
5049
| <a name="input_protocol_version"></a> [protocol\_version](#input\_protocol\_version) | (Optional) Use `HTTP1` to send requests to targets using HTTP/1.1. Supported when the request protocol is HTTP/1.1 or HTTP/2. Use `HTTP2` to send requests to targets using HTTP/2. Supported when the request protocol is HTTP/2 or gRPC, but gRPC-specific features are not available. Use `GRPC` to send requests to targets using gRPC. Supported when the request protocol is gRPC. Defaults to `HTTP1`. | `string` | `"HTTP1"` | no |
5150
| <a name="input_resource_group_description"></a> [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no |

modules/alb-ip-target-group/main.tf

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ data "aws_vpc" "this" {
1919
}
2020

2121
locals {
22+
cross_zone_strategy = {
23+
"ENABLED" = "true"
24+
"DISABLED" = "false"
25+
"INHERIT" = "use_load_balancer_configuration"
26+
}
27+
2228
ipv4_regex = "^(\\d+).(\\d+).(\\d+).(\\d+)$"
2329

2430
ipv4_vpc_cidrs = data.aws_vpc.this.cidr_block_associations[*].cidr_block
@@ -36,6 +42,11 @@ locals {
3642
]
3743
}
3844

45+
46+
###################################################
47+
# ALB IP Target Group
48+
###################################################
49+
3950
# INFO: Not supported attributes
4051
# - `connection_termination`
4152
# - `lambda_multi_value_headers_enabled`
@@ -54,12 +65,13 @@ resource "aws_lb_target_group" "this" {
5465

5566
## Attributes
5667
deregistration_delay = var.deregistration_delay
57-
load_balancing_algorithm_type = lower(var.load_balancing_algorithm)
58-
load_balancing_anomaly_mitigation = (var.load_balancing_algorithm == "WEIGHTED_RANDOM"
59-
? var.anomaly_mitigation_enabled ? "on" : "off"
68+
load_balancing_algorithm_type = lower(var.load_balancing.algorithm)
69+
load_balancing_anomaly_mitigation = (var.load_balancing.algorithm == "WEIGHTED_RANDOM"
70+
? var.load_balancing.anomaly_mitigation_enabled ? "on" : "off"
6071
: null
6172
)
62-
slow_start = var.slow_start_duration
73+
load_balancing_cross_zone_enabled = local.cross_zone_strategy[var.load_balancing.cross_zone_strategy]
74+
slow_start = var.slow_start_duration
6375

6476
stickiness {
6577
enabled = var.stickiness_enabled

0 commit comments

Comments
 (0)