Skip to content

Commit c50d984

Browse files
committed
lb_target_group: Allow invalid stickiness config, avoid breaking change
1 parent d7041a5 commit c50d984

3 files changed

Lines changed: 38 additions & 20 deletions

File tree

aws/resource_aws_lb_target_group.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ func resourceAwsLbTargetGroup() *schema.Resource {
152152
"lb_cookie", // Only for ALBs
153153
"source_ip", // Only for NLBs
154154
}, false),
155+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
156+
switch d.Get("protocol").(string) {
157+
case elbv2.ProtocolEnumTcp, elbv2.ProtocolEnumUdp, elbv2.ProtocolEnumTcpUdp, elbv2.ProtocolEnumTls:
158+
if new == "lb_cookie" && !d.Get("stickiness.0.enabled").(bool) {
159+
log.Printf("[WARN] invalid configuration, this will fail in a future version: stickiness enabled %v, protocol %s, type %s", d.Get("stickiness.0.enabled").(bool), d.Get("protocol").(string), new)
160+
return true
161+
}
162+
}
163+
return false
164+
},
155165
},
156166
"cookie_duration": {
157167
Type: schema.TypeInt,
@@ -447,23 +457,27 @@ func resourceAwsLbTargetGroupUpdate(d *schema.ResourceData, meta interface{}) er
447457
if len(stickinessBlocks) == 1 {
448458
stickiness := stickinessBlocks[0].(map[string]interface{})
449459

450-
attrs = append(attrs,
451-
&elbv2.TargetGroupAttribute{
452-
Key: aws.String("stickiness.enabled"),
453-
Value: aws.String(strconv.FormatBool(stickiness["enabled"].(bool))),
454-
},
455-
&elbv2.TargetGroupAttribute{
456-
Key: aws.String("stickiness.type"),
457-
Value: aws.String(stickiness["type"].(string)),
458-
})
459-
460-
switch d.Get("protocol").(string) {
461-
case elbv2.ProtocolEnumHttp, elbv2.ProtocolEnumHttps:
460+
if !stickiness["enabled"].(bool) && stickiness["type"].(string) == "lb_cookie" && d.Get("protocol").(string) != elbv2.ProtocolEnumHttp && d.Get("protocol").(string) != elbv2.ProtocolEnumHttps {
461+
log.Printf("[WARN] invalid configuration, this will fail in a future version: stickiness enabled %v, protocol %s, type %s", stickiness["enabled"].(bool), d.Get("protocol").(string), stickiness["type"].(string))
462+
} else {
462463
attrs = append(attrs,
463464
&elbv2.TargetGroupAttribute{
464-
Key: aws.String("stickiness.lb_cookie.duration_seconds"),
465-
Value: aws.String(fmt.Sprintf("%d", stickiness["cookie_duration"].(int))),
465+
Key: aws.String("stickiness.enabled"),
466+
Value: aws.String(strconv.FormatBool(stickiness["enabled"].(bool))),
467+
},
468+
&elbv2.TargetGroupAttribute{
469+
Key: aws.String("stickiness.type"),
470+
Value: aws.String(stickiness["type"].(string)),
466471
})
472+
473+
switch d.Get("protocol").(string) {
474+
case elbv2.ProtocolEnumHttp, elbv2.ProtocolEnumHttps:
475+
attrs = append(attrs,
476+
&elbv2.TargetGroupAttribute{
477+
Key: aws.String("stickiness.lb_cookie.duration_seconds"),
478+
Value: aws.String(fmt.Sprintf("%d", stickiness["cookie_duration"].(int))),
479+
})
480+
}
467481
}
468482
} else if len(stickinessBlocks) == 0 {
469483
attrs = append(attrs, &elbv2.TargetGroupAttribute{

aws/resource_aws_lb_target_group_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,15 @@ func TestAccAWSLBTargetGroup_stickinessValidNLB(t *testing.T) {
10471047
resource.TestCheckResourceAttr(resourceName, "stickiness.0.type", "source_ip"),
10481048
),
10491049
},
1050+
{
1051+
// this test should be invalid but allowed to avoid breaking changes
1052+
Config: testAccAWSLBTargetGroupConfig_stickinessValidity("TCP", "lb_cookie", false),
1053+
Check: resource.ComposeAggregateTestCheckFunc(
1054+
testAccCheckAWSLBTargetGroupExists(resourceName, &conf),
1055+
resource.TestCheckResourceAttr(resourceName, "stickiness.#", "1"),
1056+
resource.TestCheckResourceAttr(resourceName, "stickiness.0.enabled", "false"),
1057+
),
1058+
},
10501059
{
10511060
Config: testAccAWSLBTargetGroupConfig_stickinessValidity("TCP", "source_ip", true),
10521061
Check: resource.ComposeAggregateTestCheckFunc(
@@ -1130,11 +1139,6 @@ func TestAccAWSLBTargetGroup_stickinessInvalidNLB(t *testing.T) {
11301139
Config: testAccAWSLBTargetGroupConfig_stickinessValidity("TCP_UDP", "lb_cookie", true),
11311140
ExpectError: regexp.MustCompile("Stickiness type 'lb_cookie' is not supported for target groups with"),
11321141
},
1133-
{
1134-
Config: testAccAWSLBTargetGroupConfig_stickinessValidity("TCP_UDP", "lb_cookie", false),
1135-
PlanOnly: true,
1136-
ExpectNonEmptyPlan: true,
1137-
},
11381142
},
11391143
})
11401144
}

website/docs/r/lb_target_group.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Stickiness Blocks (`stickiness`) support the following:
8585
* `cookie_duration` - (Optional) Only used when the type is `lb_cookie`. 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. The range is 1 second to 1 week (604800 seconds). The default value is 1 day (86400 seconds).
8686
* `enabled` - (Optional) Boolean to enable / disable `stickiness`. Default is `true`
8787

88-
~> **NOTE:** To help facilitate the authoring of modules that support target groups of any protocol, you can define `stickiness` regardless of the protocol chosen. However, for `TCP` target groups, `enabled` must be `false`.
88+
~> **NOTE:** Currently, an NLB (i.e., protocol of `HTTP` or `HTTPS`) can have an invalid `stickiness` block with `type` set to `lb_cookie` as long as `enabled` is set to `false`. However, please update your configurations to avoid errors in a future version of the provider: either remove the invalid `stickiness` block or set the `type` to `source_ip`.
8989

9090
Health Check Blocks (`health_check`):
9191

0 commit comments

Comments
 (0)