forked from isovalent/aws-delete-vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoscalinggroup.go
97 lines (89 loc) · 3.3 KB
/
autoscalinggroup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/service/autoscaling/types"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/rs/zerolog/log"
"go.uber.org/multierr"
)
func autoScalingGroupNames(autoScalingGroups []types.AutoScalingGroup) []string {
autoScalingGroupNames := make([]string, 0, len(autoScalingGroups))
for _, autoScalingGroup := range autoScalingGroups {
if autoScalingGroup.AutoScalingGroupName != nil {
autoScalingGroupNames = append(autoScalingGroupNames, *autoScalingGroup.AutoScalingGroupName)
}
}
return autoScalingGroupNames
}
func deleteAutoScalingGroups(ctx context.Context, client *autoscaling.Client, ec2Client *ec2.Client, autoScalingGroups []types.AutoScalingGroup) (errs error) {
for _, autoScalingGroup := range autoScalingGroups {
if autoScalingGroup.AutoScalingGroupName == nil {
continue
}
// Resize the AutoScalingGroup to zero if not already zero.
if (autoScalingGroup.DesiredCapacity != nil && *autoScalingGroup.DesiredCapacity != 0) ||
(autoScalingGroup.MaxSize != nil && *autoScalingGroup.MaxSize != 0) ||
(autoScalingGroup.MinSize != nil && *autoScalingGroup.MinSize != 0) {
_, err := client.UpdateAutoScalingGroup(ctx, &autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: autoScalingGroup.AutoScalingGroupName,
DesiredCapacity: aws.Int32(0),
MaxSize: aws.Int32(0),
MinSize: aws.Int32(0),
})
log.Err(err).
Str("AutoScalingGroupName", *autoScalingGroup.AutoScalingGroupName).
Msg("UpdateAutoScalingGroup")
errs = multierr.Append(errs, err)
}
// Wait for any Instances to terminate.
instanceIds := make([]string, 0, len(autoScalingGroup.Instances))
for _, instance := range autoScalingGroup.Instances {
if instance.InstanceId != nil {
instanceIds = append(instanceIds, *instance.InstanceId)
}
}
if len(instanceIds) > 0 {
instanceTerminatedWaiter := ec2.NewInstanceTerminatedWaiter(ec2Client)
log.Info().
Strs("InstanceIds", instanceIds).
Msg("InstanceTerminatedWaiter.Wait")
err := instanceTerminatedWaiter.Wait(ctx, &ec2.DescribeInstancesInput{
InstanceIds: instanceIds,
}, instanceTerminatedWaiterMaxDuration)
log.Err(err).
Msg("InstanceTerminatedWaiter.Wait")
errs = multierr.Append(errs, err)
if err != nil {
continue
}
}
// Delete the AutoScalingGroup.
_, err := client.DeleteAutoScalingGroup(ctx, &autoscaling.DeleteAutoScalingGroupInput{
AutoScalingGroupName: autoScalingGroup.AutoScalingGroupName,
})
log.Err(err).
Str("AutoScalingGroupName", *autoScalingGroup.AutoScalingGroupName).
Msg("DeleteAutoScalingGroup")
errs = multierr.Append(errs, err)
}
return
}
func listAutoScalingGroups(ctx context.Context, client *autoscaling.Client, filters []types.Filter) ([]types.AutoScalingGroup, error) {
input := autoscaling.DescribeAutoScalingGroupsInput{
Filters: filters,
}
var autoScalingGroups []types.AutoScalingGroup
for {
output, err := client.DescribeAutoScalingGroups(ctx, &input)
if err != nil {
return nil, err
}
autoScalingGroups = append(autoScalingGroups, output.AutoScalingGroups...)
if output.NextToken == nil {
return autoScalingGroups, nil
}
input.NextToken = output.NextToken
}
}