Skip to content

Commit

Permalink
Support WEIGHTED_RANDOM load balancing algorithm (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
posquit0 authored Jul 15, 2024
1 parent 3e2041f commit 48c0b22
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 40 deletions.
12 changes: 7 additions & 5 deletions modules/alb-instance-target-group/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ This module creates following resources.

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.71 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.6 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.38 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.19.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.58.0 |

## Modules

Expand All @@ -40,15 +40,16 @@ This module creates following resources.
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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` or `LEAST_OUTSTANDING_REQUESTS`. Defaults to `ROUND_ROBIN`. | `string` | `"ROUND_ROBIN"` | no |
| <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 |
| <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 |
| <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 |
| <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 |
| <a name="input_resource_group_enabled"></a> [resource\_group\_enabled](#input\_resource\_group\_enabled) | (Optional) Whether to create Resource Group to find and group AWS resources which are created by this module. | `bool` | `true` | no |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | (Optional) The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. | `string` | `""` | no |
| <a name="input_slow_start_duration"></a> [slow\_start\_duration](#input\_slow\_start\_duration) | (Optional) The amount time for a newly registered targets to warm up before the load balancer sends them a full share of requests. During this period, targets receives an increasing share of requests until it reaches its fair share. Requires `30` to `900` seconds to enable, or `0` seconds to disable. This attribute cannot be combined with the Least outstanding requests algorithm. | `number` | `0` | no |
| <a name="input_slow_start_duration"></a> [slow\_start\_duration](#input\_slow\_start\_duration) | (Optional) The amount time for a newly registered targets to warm up before the load balancer sends them a full share of requests. During this period, targets receives an increasing share of requests until it reaches its fair share. Requires `30` to `900` seconds to enable, or `0` seconds to disable. Not compatible with the Least outstanding requests and Weighted random routing algorithms. | `number` | `0` | no |
| <a name="input_stickiness_cookie"></a> [stickiness\_cookie](#input\_stickiness\_cookie) | (Optional) The name of the application based cookie. `AWSALB`, `AWSALBAPP`, and `AWSALBTG` prefixes are reserved and cannot be used. Only needed when `stickiness_type` is `APP_COOKIE`. | `string` | `null` | no |
| <a name="input_stickiness_duration"></a> [stickiness\_duration](#input\_stickiness\_duration) | (Optional) The time period, in seconds, during which requests from a client should be routed to the same target. After this time period expires, the load balancer-generated cookie is considered stale. Valid values are from `1` to `604800` (1 week). Defaults to `86400` (1 day). | `number` | `86400` | no |
| <a name="input_stickiness_enabled"></a> [stickiness\_enabled](#input\_stickiness\_enabled) | (Optional) Whether to enable the type of stickiness associated with this target group. If enabled, the load balancer binds a client’s session to a specific instance within the target group. Defaults to `false`. | `bool` | `false` | no |
Expand All @@ -65,6 +66,7 @@ This module creates following resources.
| <a name="output_attributes"></a> [attributes](#output\_attributes) | Attributes of the Instance target group of network load balancer. |
| <a name="output_health_check"></a> [health\_check](#output\_health\_check) | Health Check configuration of the target group. |
| <a name="output_id"></a> [id](#output\_id) | The ID of the target group. |
| <a name="output_load_balancers"></a> [load\_balancers](#output\_load\_balancers) | The ARNs (Amazon Resource Name) of the load balancers associated with the target group. |
| <a name="output_name"></a> [name](#output\_name) | The name of the target group. |
| <a name="output_port"></a> [port](#output\_port) | The port number on which the target receive trrafic. |
| <a name="output_protocol"></a> [protocol](#output\_protocol) | The protocol to use to connect with the target. |
Expand Down
6 changes: 5 additions & 1 deletion modules/alb-instance-target-group/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ resource "aws_lb_target_group" "this" {
## Attributes
deregistration_delay = var.deregistration_delay
load_balancing_algorithm_type = lower(var.load_balancing_algorithm)
slow_start = var.slow_start_duration
load_balancing_anomaly_mitigation = (var.load_balancing_algorithm == "WEIGHTED_RANDOM"
? var.anomaly_mitigation_enabled ? "on" : "off"
: null
)
slow_start = var.slow_start_duration

stickiness {
enabled = var.stickiness_enabled
Expand Down
4 changes: 4 additions & 0 deletions modules/alb-instance-target-group/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ output "targets" {
output "attributes" {
description = "Attributes of the Instance target group of network load balancer."
value = {
anomaly_mitigation_enabled = (var.load_balancing_algorithm == "WEIGHTED_RANDOM"
? var.anomaly_mitigation_enabled
: null
)
deregistration_delay = aws_lb_target_group.this.deregistration_delay
load_balancing_algorithm = upper(aws_lb_target_group.this.load_balancing_algorithm_type)
slow_start_duration = aws_lb_target_group.this.slow_start
Expand Down
17 changes: 12 additions & 5 deletions modules/alb-instance-target-group/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,26 @@ variable "deregistration_delay" {
}

variable "load_balancing_algorithm" {
description = "(Optional) Determines how the load balancer selects targets when routing requests. Valid values are `ROUND_ROBIN` or `LEAST_OUTSTANDING_REQUESTS`. Defaults to `ROUND_ROBIN`."
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`."
type = string
default = "ROUND_ROBIN"
nullable = false

validation {
condition = contains(["ROUND_ROBIN", "LEAST_OUTSTANDING_REQUESTS"], var.load_balancing_algorithm)
error_message = "Valid values are `ROUND_ROBIN` and `LEAST_OUTSTANDING_REQUESTS`."
condition = contains(["ROUND_ROBIN", "LEAST_OUTSTANDING_REQUESTS", "WEIGHTED_RANDOM"], var.load_balancing_algorithm)
error_message = "Valid values are `ROUND_ROBIN`, `LEAST_OUTSTANDING_REQUESTS` and `WEIGHTED_RANDOM`."
}
}

variable "anomaly_mitigation_enabled" {
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`."
type = bool
default = false
nullable = false
}

variable "slow_start_duration" {
description = "(Optional) The amount time for a newly registered targets to warm up before the load balancer sends them a full share of requests. During this period, targets receives an increasing share of requests until it reaches its fair share. Requires `30` to `900` seconds to enable, or `0` seconds to disable. This attribute cannot be combined with the Least outstanding requests algorithm."
description = "(Optional) The amount time for a newly registered targets to warm up before the load balancer sends them a full share of requests. During this period, targets receives an increasing share of requests until it reaches its fair share. Requires `30` to `900` seconds to enable, or `0` seconds to disable. Not compatible with the Least outstanding requests and Weighted random routing algorithms."
type = number
default = 0
nullable = false
Expand All @@ -100,7 +107,7 @@ variable "slow_start_duration" {
var.slow_start_duration == 0,
var.slow_start_duration <= 900 && var.slow_start_duration >= 30
])
error_message = "Requires `30` to `900` seconds to enable, or `0` seconds to disable. This attribute cannot be combined with the Least outstanding requests algorithm."
error_message = "Requires `30` to `900` seconds to enable, or `0` seconds to disable. Not compatible with the Least outstanding requests and Weighted random routing algorithms."
}
}

Expand Down
Loading

0 comments on commit 48c0b22

Please sign in to comment.