@@ -767,6 +767,78 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e
767
767
return nil
768
768
}
769
769
770
+ func waitUntilAutoscalingGroupLoadBalancerTargetGroupsRemoved (conn * autoscaling.AutoScaling , asgName string ) error {
771
+ input := & autoscaling.DescribeLoadBalancerTargetGroupsInput {
772
+ AutoScalingGroupName : aws .String (asgName ),
773
+ }
774
+ var tgRemoving bool
775
+
776
+ for {
777
+ output , err := conn .DescribeLoadBalancerTargetGroups (input )
778
+
779
+ if err != nil {
780
+ return err
781
+ }
782
+
783
+ for _ , tg := range output .LoadBalancerTargetGroups {
784
+ if aws .StringValue (tg .State ) == "Removing" {
785
+ tgRemoving = true
786
+ break
787
+ }
788
+ }
789
+
790
+ if tgRemoving {
791
+ tgRemoving = false
792
+ input .NextToken = nil
793
+ continue
794
+ }
795
+
796
+ if aws .StringValue (output .NextToken ) == "" {
797
+ break
798
+ }
799
+
800
+ input .NextToken = output .NextToken
801
+ }
802
+
803
+ return nil
804
+ }
805
+
806
+ func waitUntilAutoscalingGroupLoadBalancerTargetGroupsAdded (conn * autoscaling.AutoScaling , asgName string ) error {
807
+ input := & autoscaling.DescribeLoadBalancerTargetGroupsInput {
808
+ AutoScalingGroupName : aws .String (asgName ),
809
+ }
810
+ var tgAdding bool
811
+
812
+ for {
813
+ output , err := conn .DescribeLoadBalancerTargetGroups (input )
814
+
815
+ if err != nil {
816
+ return err
817
+ }
818
+
819
+ for _ , tg := range output .LoadBalancerTargetGroups {
820
+ if aws .StringValue (tg .State ) == "Adding" {
821
+ tgAdding = true
822
+ break
823
+ }
824
+ }
825
+
826
+ if tgAdding {
827
+ tgAdding = false
828
+ input .NextToken = nil
829
+ continue
830
+ }
831
+
832
+ if aws .StringValue (output .NextToken ) == "" {
833
+ break
834
+ }
835
+
836
+ input .NextToken = output .NextToken
837
+ }
838
+
839
+ return nil
840
+ }
841
+
770
842
func resourceAwsAutoscalingGroupUpdate (d * schema.ResourceData , meta interface {}) error {
771
843
conn := meta .(* AWSClient ).autoscalingconn
772
844
shouldWaitForCapacity := false
@@ -920,22 +992,55 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
920
992
add := expandStringList (ns .Difference (os ).List ())
921
993
922
994
if len (remove ) > 0 {
923
- _ , err := conn .DetachLoadBalancerTargetGroups (& autoscaling.DetachLoadBalancerTargetGroupsInput {
924
- AutoScalingGroupName : aws .String (d .Id ()),
925
- TargetGroupARNs : remove ,
926
- })
927
- if err != nil {
928
- return fmt .Errorf ("Error updating Load Balancers Target Groups for AutoScaling Group (%s), error: %s" , d .Id (), err )
995
+ // AWS API only supports adding/removing 10 at a time
996
+ var batches [][]* string
997
+
998
+ batchSize := 10
999
+
1000
+ for batchSize < len (remove ) {
1001
+ remove , batches = remove [batchSize :], append (batches , remove [0 :batchSize :batchSize ])
929
1002
}
1003
+ batches = append (batches , remove )
1004
+
1005
+ for _ , batch := range batches {
1006
+ _ , err := conn .DetachLoadBalancerTargetGroups (& autoscaling.DetachLoadBalancerTargetGroupsInput {
1007
+ AutoScalingGroupName : aws .String (d .Id ()),
1008
+ TargetGroupARNs : batch ,
1009
+ })
1010
+ if err != nil {
1011
+ return fmt .Errorf ("Error updating Load Balancers Target Groups for AutoScaling Group (%s), error: %s" , d .Id (), err )
1012
+ }
1013
+
1014
+ if err := waitUntilAutoscalingGroupLoadBalancerTargetGroupsRemoved (conn , d .Id ()); err != nil {
1015
+ return fmt .Errorf ("error describing AutoScaling Group (%s) Load Balancer Target Groups being removed: %s" , d .Id (), err )
1016
+ }
1017
+ }
1018
+
930
1019
}
931
1020
932
1021
if len (add ) > 0 {
933
- _ , err := conn .AttachLoadBalancerTargetGroups (& autoscaling.AttachLoadBalancerTargetGroupsInput {
934
- AutoScalingGroupName : aws .String (d .Id ()),
935
- TargetGroupARNs : add ,
936
- })
937
- if err != nil {
938
- return fmt .Errorf ("Error updating Load Balancers Target Groups for AutoScaling Group (%s), error: %s" , d .Id (), err )
1022
+ batchSize := 10
1023
+
1024
+ var batches [][]* string
1025
+
1026
+ for batchSize < len (add ) {
1027
+ add , batches = add [batchSize :], append (batches , add [0 :batchSize :batchSize ])
1028
+ }
1029
+ batches = append (batches , add )
1030
+
1031
+ for _ , batch := range batches {
1032
+ _ , err := conn .AttachLoadBalancerTargetGroups (& autoscaling.AttachLoadBalancerTargetGroupsInput {
1033
+ AutoScalingGroupName : aws .String (d .Id ()),
1034
+ TargetGroupARNs : batch ,
1035
+ })
1036
+
1037
+ if err != nil {
1038
+ return fmt .Errorf ("Error updating Load Balancers Target Groups for AutoScaling Group (%s), error: %s" , d .Id (), err )
1039
+ }
1040
+
1041
+ if err := waitUntilAutoscalingGroupLoadBalancerTargetGroupsAdded (conn , d .Id ()); err != nil {
1042
+ return fmt .Errorf ("error describing AutoScaling Group (%s) Load Balancer Target Groups being added: %s" , d .Id (), err )
1043
+ }
939
1044
}
940
1045
}
941
1046
}
0 commit comments