Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding support for connection termination in aws_lb_target_group #21130

Merged
3 changes: 3 additions & 0 deletions .changelog/21130.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_lb_target_group: Add support for `connection_termination` argument for NLBs
```
25 changes: 25 additions & 0 deletions internal/service/elbv2/target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ func ResourceTargetGroup() *schema.Resource {
Optional: true,
Default: false,
},
"connection_termination": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"slow_start": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -437,6 +442,13 @@ func resourceTargetGroupCreate(d *schema.ResourceData, meta interface{}) error {
})
}

if v, ok := d.GetOk("connection_termination"); ok {
attrs = append(attrs, &elbv2.TargetGroupAttribute{
Key: aws.String("deregistration_delay.connection_termination.enabled"),
Value: aws.String(strconv.FormatBool(v.(bool))),
})
}

if v, ok := d.GetOk("slow_start"); ok {
attrs = append(attrs, &elbv2.TargetGroupAttribute{
Key: aws.String("slow_start.duration_seconds"),
Expand Down Expand Up @@ -671,6 +683,13 @@ func resourceTargetGroupUpdate(d *schema.ResourceData, meta interface{}) error {
})
}

if d.HasChange("connection_termination") {
attrs = append(attrs, &elbv2.TargetGroupAttribute{
Key: aws.String("deregistration_delay.connection_termination.enabled"),
Value: aws.String(strconv.FormatBool(d.Get("connection_termination").(bool))),
})
}

if d.HasChange("preserve_client_ip") {
attrs = append(attrs, &elbv2.TargetGroupAttribute{
Key: aws.String("preserve_client_ip.enabled"),
Expand Down Expand Up @@ -902,6 +921,12 @@ func flattenTargetGroupResource(d *schema.ResourceData, meta interface{}, target
return fmt.Errorf("error converting proxy_protocol_v2.enabled to bool: %s", aws.StringValue(attr.Value))
}
d.Set("proxy_protocol_v2", enabled)
case "deregistration_delay.connection_termination.enabled":
enabled, err := strconv.ParseBool(aws.StringValue(attr.Value))
if err != nil {
return fmt.Errorf("error converting deregistration_delay.connection_termination.enabled to bool: %s", aws.StringValue(attr.Value))
}
d.Set("connection_termination", enabled)
case "slow_start.duration_seconds":
slowStart, err := strconv.Atoi(aws.StringValue(attr.Value))
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/service/elbv2/target_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func DataSourceTargetGroup() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"connection_termination": {
Type: schema.TypeBool,
Computed: true,
},
"deregistration_delay": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -209,6 +213,12 @@ func dataSourceTargetGroupRead(d *schema.ResourceData, meta interface{}) error {

for _, attr := range attrResp.Attributes {
switch aws.StringValue(attr.Key) {
case "deregistration_delay.connection_termination.enabled":
enabled, err := strconv.ParseBool(aws.StringValue(attr.Value))
if err != nil {
return fmt.Errorf("error converting deregistration_delay.connection_termination.enabled to bool: %s", aws.StringValue(attr.Value))
}
d.Set("connection_termination", enabled)
case "deregistration_delay.timeout_seconds":
timeout, err := strconv.Atoi(aws.StringValue(attr.Value))
if err != nil {
Expand Down
69 changes: 68 additions & 1 deletion internal/service/elbv2/target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ func TestAccELBV2TargetGroup_NetworkLB_targetGroup(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "vpc_id"),
resource.TestCheckResourceAttr(resourceName, "deregistration_delay", "200"),
resource.TestCheckResourceAttr(resourceName, "proxy_protocol_v2", "false"),
resource.TestCheckResourceAttr(resourceName, "connection_termination", "false"),
resource.TestCheckResourceAttr(resourceName, "health_check.#", "1"),
resource.TestCheckResourceAttr(resourceName, "health_check.0.enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "health_check.0.interval", "10"),
Expand Down Expand Up @@ -819,6 +820,35 @@ func TestAccELBV2TargetGroup_NetworkLB_targetGroup(t *testing.T) {
})
}

func TestAccELBV2TargetGroup_networkLB_TargetGroupWithConnectionTermination(t *testing.T) {
var confBefore, confAfter elbv2.TargetGroup
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_lb_target_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, elbv2.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckTargetGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccTargetGroupConfig_typeTCP(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(resourceName, &confBefore),
resource.TestCheckResourceAttr(resourceName, "connection_termination", "false"),
),
},
{
Config: testAccTargetGroupConfig_typeTCP_withConnectionTermination(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(resourceName, &confAfter),
resource.TestCheckResourceAttr(resourceName, "connection_termination", "true"),
),
},
},
})
}

func TestAccELBV2TargetGroup_NetworkLB_targetGroupWithProxy(t *testing.T) {
var confBefore, confAfter elbv2.TargetGroup
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -901,6 +931,7 @@ func TestAccELBV2TargetGroup_protocolGeneve(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"connection_termination",
"lambda_multi_value_headers_enabled",
"proxy_protocol_v2",
"slow_start",
Expand Down Expand Up @@ -1674,6 +1705,7 @@ func TestAccELBV2TargetGroup_A_lambda(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"connection_termination",
"deregistration_delay",
"proxy_protocol_v2",
"slow_start",
Expand Down Expand Up @@ -1708,6 +1740,7 @@ func TestAccELBV2TargetGroup_A_lambdaMultiValueHeadersEnabled(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"connection_termination",
"deregistration_delay",
"proxy_protocol_v2",
"slow_start",
Expand Down Expand Up @@ -2807,7 +2840,7 @@ resource "aws_lb_target_group" "test" {
protocol = "TCP"
vpc_id = aws_vpc.test.id

proxy_protocol_v2 = "true"
proxy_protocol_v2 = true
deregistration_delay = 200

health_check {
Expand All @@ -2833,6 +2866,40 @@ resource "aws_vpc" "test" {
`, rName)
}

func testAccTargetGroupConfig_typeTCP_withConnectionTermination(rName string) string {
return fmt.Sprintf(`
resource "aws_lb_target_group" "test" {
name = %[1]q
port = 8082
protocol = "TCP"
vpc_id = aws_vpc.test.id

connection_termination = true
deregistration_delay = 200

health_check {
interval = 10
port = "traffic-port"
protocol = "TCP"
healthy_threshold = 3
unhealthy_threshold = 3
}

tags = {
Name = %[1]q
}
}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"

tags = {
Name = %[1]q
}
}
`, rName)
}

func testAccTargetGroupConfig_updateHealthCheck(rName string) string {
return fmt.Sprintf(`
resource "aws_lb_target_group" "test" {
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/lb_target_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ resource "aws_lb_target_group" "lambda-example" {

The following arguments are supported:

* `connection_termination` - (Optional) Whether to terminate connections at the end of the deregistration timeout on Network Load Balancers. See [doc](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-target-groups.html#deregistration-delay) for more information. Default is `false`.
* `deregistration_delay` - (Optional) 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.
* `health_check` - (Optional, Maximum of 1) Health Check configuration block. Detailed below.
* `lambda_multi_value_headers_enabled` - (Optional) 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`. Default is `false`.
Expand Down