Skip to content

Commit

Permalink
Batch up requests with more than 10 target group changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Beresford committed Oct 9, 2019
1 parent 1647a5b commit 8cb02b4
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions aws/resource_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,22 +920,47 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
add := expandStringList(ns.Difference(os).List())

if len(remove) > 0 {
_, err := conn.DetachLoadBalancerTargetGroups(&autoscaling.DetachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(d.Id()),
TargetGroupARNs: remove,
})
if err != nil {
return fmt.Errorf("Error updating Load Balancers Target Groups for AutoScaling Group (%s), error: %s", d.Id(), err)
// AWS API only supports adding/removing 10 at a time
var batches [][]*string

batchSize := 10

for batchSize < len(remove) {
remove, batches = remove[batchSize:], append(batches, remove[0:batchSize:batchSize])
}
batches = append(batches, remove)

for _, batch := range batches {
_, err := conn.DetachLoadBalancerTargetGroups(&autoscaling.DetachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(d.Id()),
TargetGroupARNs: batch,
})
if err != nil {
return fmt.Errorf("Error updating Load Balancers Target Groups for AutoScaling Group (%s), error: %s", d.Id(), err)
}
}

}

if len(add) > 0 {
_, err := conn.AttachLoadBalancerTargetGroups(&autoscaling.AttachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(d.Id()),
TargetGroupARNs: add,
})
if err != nil {
return fmt.Errorf("Error updating Load Balancers Target Groups for AutoScaling Group (%s), error: %s", d.Id(), err)
batchSize := 10

var batches [][]*string

for batchSize < len(add) {
add, batches = add[batchSize:], append(batches, add[0:batchSize:batchSize])
}
batches = append(batches, add)

for _, batch := range batches {
_, err := conn.AttachLoadBalancerTargetGroups(&autoscaling.AttachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(d.Id()),
TargetGroupARNs: batch,
})

if err != nil {
return fmt.Errorf("Error updating Load Balancers Target Groups for AutoScaling Group (%s), error: %s", d.Id(), err)
}
}
}
}
Expand Down

0 comments on commit 8cb02b4

Please sign in to comment.