Skip to content

Commit

Permalink
deprecate 'alb_target_group_arn' and add 'lb_target_group_arn' (#22662)
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 authored Feb 2, 2022
1 parent bb56afd commit 1c91d14
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changelog/22662.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:note
resource/aws_autoscaling_attachment: The `alb_target_group_arn` argument has been deprecated. All configurations using `alb_target_group_arn` should be updated to use the new `lb_target_group_arn` argument instead
```

```release-note:enhancement
resource/aws_autoscaling_attachment: Add `lb_target_group_arn` argument
```
62 changes: 56 additions & 6 deletions internal/service/autoscaling/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@ func ResourceAttachment() *schema.Resource {
},

"alb_target_group_arn": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Deprecated: "Use lb_target_group_arn instead",
ConflictsWith: []string{"lb_target_group_arn"},
},

"lb_target_group_arn": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
ConflictsWith: []string{"alb_target_group_arn"},
},
},
}
Expand Down Expand Up @@ -65,7 +74,20 @@ func resourceAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] registering asg %s with ALB Target Group %s", asgName, v.(string))

if _, err := conn.AttachLoadBalancerTargetGroups(attachOpts); err != nil {
return fmt.Errorf("Failure attaching AutoScaling Group %s with ALB Target Group: %s: %s", asgName, v.(string), err)
return fmt.Errorf("failure attaching AutoScaling Group %s with ALB Target Group: %s: %w", asgName, v.(string), err)
}
}

if v, ok := d.GetOk("lb_target_group_arn"); ok {
attachOpts := &autoscaling.AttachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(asgName),
TargetGroupARNs: []*string{aws.String(v.(string))},
}

log.Printf("[INFO] registering asg %s with LB Target Group %s", asgName, v.(string))

if _, err := conn.AttachLoadBalancerTargetGroups(attachOpts); err != nil {
return fmt.Errorf("failure attaching AutoScaling Group %s with LB Target Group: %s: %w", asgName, v.(string), err)
}
}

Expand Down Expand Up @@ -123,6 +145,22 @@ func resourceAttachmentRead(d *schema.ResourceData, meta interface{}) error {
}
}

if v, ok := d.GetOk("lb_target_group_arn"); ok {
found := false
for _, i := range asg.TargetGroupARNs {
if v.(string) == aws.StringValue(i) {
d.Set("lb_target_group_arn", v.(string))
found = true
break
}
}

if !found {
log.Printf("[WARN] Association for %s was not found in ASG association", v.(string))
d.SetId("")
}
}

return nil
}

Expand All @@ -138,7 +176,7 @@ func resourceAttachmentDelete(d *schema.ResourceData, meta interface{}) error {

log.Printf("[INFO] Deleting ELB %s association from: %s", v.(string), asgName)
if _, err := conn.DetachLoadBalancers(detachOpts); err != nil {
return fmt.Errorf("Failure detaching AutoScaling Group %s with Elastic Load Balancer: %s: %s", asgName, v.(string), err)
return fmt.Errorf("failure detaching AutoScaling Group %s with Elastic Load Balancer: %s: %w", asgName, v.(string), err)
}
}

Expand All @@ -150,7 +188,19 @@ func resourceAttachmentDelete(d *schema.ResourceData, meta interface{}) error {

log.Printf("[INFO] Deleting ALB Target Group %s association from: %s", v.(string), asgName)
if _, err := conn.DetachLoadBalancerTargetGroups(detachOpts); err != nil {
return fmt.Errorf("Failure detaching AutoScaling Group %s with ALB Target Group: %s: %s", asgName, v.(string), err)
return fmt.Errorf("failure detaching AutoScaling Group %s with ALB Target Group: %s: %w", asgName, v.(string), err)
}
}

if v, ok := d.GetOk("lb_target_group_arn"); ok {
detachOpts := &autoscaling.DetachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(asgName),
TargetGroupARNs: []*string{aws.String(v.(string))},
}

log.Printf("[INFO] Deleting LB Target Group %s association from: %s", v.(string), asgName)
if _, err := conn.DetachLoadBalancerTargetGroups(detachOpts); err != nil {
return fmt.Errorf("failure detaching AutoScaling Group %s with LB Target Group: %s: %w", asgName, v.(string), err)
}
}

Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/autoscaling_attachment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ The following arguments are supported:

* `autoscaling_group_name` - (Required) Name of ASG to associate with the ELB.
* `elb` - (Optional) The name of the ELB.
* `alb_target_group_arn` - (Optional) The ARN of an ALB Target Group.
* `alb_target_group_arn` - (Optional, **Deprecated** use `lb_target_group_arn` instead) The ARN of an ALB Target Group.
* `lb_target_group_arn` - (Optional) The ARN of a load balancer target group.

## Attributes Reference

Expand Down

0 comments on commit 1c91d14

Please sign in to comment.