Skip to content

Commit

Permalink
resource/aws_lb_target_group: Add load_balancing_algorithm_type arg…
Browse files Browse the repository at this point in the history
…ument (support Least Outstanding Requests algorithm for Application Load Balancers) (#11141)

Output from acceptance testing:

```
--- PASS: TestAccDataSourceAWSALBTargetGroup_basic (200.51s)

--- PASS: TestAccAWSALBTargetGroup_basic (21.51s)
--- PASS: TestAccAWSALBTargetGroup_changeNameForceNew (47.75s)
--- PASS: TestAccAWSALBTargetGroup_changePortForceNew (51.25s)
--- PASS: TestAccAWSALBTargetGroup_changeProtocolForceNew (49.02s)
--- PASS: TestAccAWSALBTargetGroup_changeVpcForceNew (43.55s)
--- PASS: TestAccAWSALBTargetGroup_generatedName (36.73s)
--- PASS: TestAccAWSALBTargetGroup_lambda (17.06s)
--- PASS: TestAccAWSALBTargetGroup_lambdaMultiValueHeadersEnabled (50.30s)
--- PASS: TestAccAWSALBTargetGroup_missingPortProtocolVpc (38.43s)
--- PASS: TestAccAWSALBTargetGroup_namePrefix (38.05s)
--- PASS: TestAccAWSALBTargetGroup_setAndUpdateSlowStart (60.30s)
--- PASS: TestAccAWSALBTargetGroup_tags (39.07s)
--- PASS: TestAccAWSALBTargetGroup_updateHealthCheck (41.31s)
--- PASS: TestAccAWSALBTargetGroup_updateLoadBalancingAlgorithmType (75.02s)
--- PASS: TestAccAWSALBTargetGroup_updateSticknessEnabled (75.67s)

--- PASS: TestAccAWSLBTargetGroup_basic (17.41s)
--- PASS: TestAccAWSLBTargetGroup_basicUdp (16.94s)
--- PASS: TestAccAWSLBTargetGroup_changeNameForceNew (44.95s)
--- PASS: TestAccAWSLBTargetGroup_changePortForceNew (52.23s)
--- PASS: TestAccAWSLBTargetGroup_changeProtocolForceNew (62.13s)
--- PASS: TestAccAWSLBTargetGroup_changeVpcForceNew (51.71s)
--- PASS: TestAccAWSLBTargetGroup_defaults_application (24.82s)
--- PASS: TestAccAWSLBTargetGroup_defaults_network (32.18s)
--- PASS: TestAccAWSLBTargetGroup_enableHealthCheck (19.99s)
--- PASS: TestAccAWSLBTargetGroup_generatedName (19.61s)
--- PASS: TestAccAWSLBTargetGroup_namePrefix (27.16s)
--- PASS: TestAccAWSLBTargetGroup_networkLB_TargetGroup (64.78s)
--- PASS: TestAccAWSLBTargetGroup_networkLB_TargetGroupWithProxy (52.06s)
--- PASS: TestAccAWSLBTargetGroup_Protocol_Tls (25.47s)
--- PASS: TestAccAWSLBTargetGroup_stickinessWithTCPDisabled (36.05s)
--- PASS: TestAccAWSLBTargetGroup_stickinessWithTCPEnabledShouldError (30.68s)
--- PASS: TestAccAWSLBTargetGroup_tags (56.23s)
--- PASS: TestAccAWSLBTargetGroup_TCP_HTTPHealthCheck (53.13s)
--- PASS: TestAccAWSLBTargetGroup_updateHealthCheck (45.20s)
--- PASS: TestAccAWSLBTargetGroup_updateSticknessEnabled (41.85s)
--- PASS: TestAccAWSLBTargetGroup_withoutHealthcheck (13.44s)
```
  • Loading branch information
dhoeric authored Mar 3, 2020
1 parent bfbc65f commit 378a269
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
71 changes: 70 additions & 1 deletion aws/resource_aws_alb_target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,47 @@ func TestAccAWSALBTargetGroup_setAndUpdateSlowStart(t *testing.T) {
})
}

func TestAccAWSALBTargetGroup_updateLoadBalancingAlgorithmType(t *testing.T) {
var conf elbv2.TargetGroup
targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_alb_target_group.test",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSALBTargetGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSALBTargetGroupConfig_loadBalancingAlgorithm(targetGroupName, false, ""),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "load_balancing_algorithm_type", "round_robin"),
),
},
{
Config: testAccAWSALBTargetGroupConfig_loadBalancingAlgorithm(targetGroupName, true, "round_robin"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "load_balancing_algorithm_type", "round_robin"),
),
},
{
Config: testAccAWSALBTargetGroupConfig_loadBalancingAlgorithm(targetGroupName, true, "least_outstanding_requests"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "load_balancing_algorithm_type", "least_outstanding_requests"),
),
},
},
})
}

func testAccCheckAWSALBTargetGroupExists(n string, res *elbv2.TargetGroup) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -530,6 +571,7 @@ func TestAccAWSALBTargetGroup_lambda(t *testing.T) {
"deregistration_delay",
"proxy_protocol_v2",
"slow_start",
"load_balancing_algorithm_type",
},
},
},
Expand Down Expand Up @@ -562,6 +604,7 @@ func TestAccAWSALBTargetGroup_lambdaMultiValueHeadersEnabled(t *testing.T) {
"deregistration_delay",
"proxy_protocol_v2",
"slow_start",
"load_balancing_algorithm_type",
},
},
{
Expand Down Expand Up @@ -889,6 +932,32 @@ resource "aws_vpc" "test" {
}`, targetGroupName, stickinessBlock)
}

func testAccAWSALBTargetGroupConfig_loadBalancingAlgorithm(targetGroupName string, nonDefault bool, algoType string) string {
var algoTypeParam string

if nonDefault {
algoTypeParam = fmt.Sprintf(`load_balancing_algorithm_type = "%s"`, algoType)
}

return fmt.Sprintf(`resource "aws_alb_target_group" "test" {
name = "%s"
port = 443
protocol = "HTTPS"
vpc_id = "${aws_vpc.test.id}"
%s
}
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "terraform-testacc-alb-target-group-load-balancing-algo"
}
}`, targetGroupName, algoTypeParam)
}

func testAccAWSALBTargetGroupConfig_updateSlowStart(targetGroupName string, slowStartDuration int) string {
return fmt.Sprintf(`resource "aws_alb_target_group" "test" {
name = "%s"
Expand Down Expand Up @@ -950,7 +1019,7 @@ resource "aws_alb_target_group" "test" {
port = 80
protocol = "HTTP"
vpc_id = "${aws_vpc.test.id}"
health_check {
path = "/health"
interval = 60
Expand Down
20 changes: 20 additions & 0 deletions aws/resource_aws_lb_target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ func resourceAwsLbTargetGroup() *schema.Resource {
}, false),
},

"load_balancing_algorithm_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"round_robin",
"least_outstanding_requests",
}, false),
},

"stickiness": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -451,6 +461,13 @@ func resourceAwsLbTargetGroupUpdate(d *schema.ResourceData, meta interface{}) er
})
}
}

if d.HasChange("load_balancing_algorithm_type") {
attrs = append(attrs, &elbv2.TargetGroupAttribute{
Key: aws.String("load_balancing.algorithm.type"),
Value: aws.String(d.Get("load_balancing_algorithm_type").(string)),
})
}
case elbv2.TargetTypeEnumLambda:
if d.HasChange("lambda_multi_value_headers_enabled") {
attrs = append(attrs, &elbv2.TargetGroupAttribute{
Expand Down Expand Up @@ -608,6 +625,9 @@ func flattenAwsLbTargetGroupResource(d *schema.ResourceData, meta interface{}, t
return fmt.Errorf("Error converting slow_start.duration_seconds to int: %s", aws.StringValue(attr.Value))
}
d.Set("slow_start", slowStart)
case "load_balancing.algorithm.type":
loadBalancingAlgorithm := aws.StringValue(attr.Value)
d.Set("load_balancing_algorithm_type", loadBalancingAlgorithm)
}
}

Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/lb_target_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ The following arguments are supported:
* `vpc_id` - (Optional, Forces new resource) The identifier of the VPC in which to create the target group. Required when `target_type` is `instance` or `ip`. Does not apply when `target_type` is `lambda`.
* `deregistration_delay` - (Optional) The amount time for Elastic Load Balancing to wait before changing the state of a deregistering target from draining to unused. The range is 0-3600 seconds. The default value is 300 seconds.
* `slow_start` - (Optional) The amount time for targets to warm up before the load balancer sends them a full share of requests. The range is 30-900 seconds or 0 to disable. The default value is 0 seconds.
* `load_balancing_algorithm_type` - (Optional) Determines how the load balancer selects targets when routing requests. Only applicable for Application Load Balancer Target Groups. The value is `round_robin` or `least_outstanding_requests`. The default is `round_robin`.
* `lambda_multi_value_headers_enabled` - (Optional) Boolean whether the request and response headers exchanged between the load balancer and the Lambda function include arrays of values or strings. Only applies when `target_type` is `lambda`.
* `proxy_protocol_v2` - (Optional) Boolean to enable / disable support for proxy protocol v2 on Network Load Balancers. See [doc](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-target-groups.html#proxy-protocol) for more information.
* `stickiness` - (Optional) A Stickiness block. Stickiness blocks are documented below. `stickiness` is only valid if used with Load Balancers of type `Application`
Expand Down Expand Up @@ -104,7 +105,7 @@ The underlying function is invoked when `target_type` is set to `lambda`.
* `timeout` - (Optional) The amount of time, in seconds, during which no response means a failed health check. For Application Load Balancers, the range is 2 to 120 seconds, and the default is 5 seconds for the `instance` target type and 30 seconds for the `lambda` target type. For Network Load Balancers, you cannot set a custom value, and the default is 10 seconds for TCP and HTTPS health checks and 6 seconds for HTTP health checks.
* `healthy_threshold` - (Optional) The number of consecutive health checks successes required before considering an unhealthy target healthy. Defaults to 3.
* `unhealthy_threshold` - (Optional) The number of consecutive health check failures required before considering the target unhealthy . For Network Load Balancers, this value must be the same as the `healthy_threshold`. Defaults to 3.
* `matcher` (Required for HTTP/HTTPS ALB) 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"). Applies to Application Load Balancers only (HTTP/HTTPS), not Network Load Balancers (TCP).
* `matcher` (Required for HTTP/HTTPS ALB) 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"). Applies to Application Load Balancers only (HTTP/HTTPS), not Network Load Balancers (TCP).

## Attributes Reference

Expand Down

0 comments on commit 378a269

Please sign in to comment.