forked from gruntwork-io/cloud-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch_config.go
59 lines (47 loc) · 1.75 KB
/
launch_config.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
package aws
import (
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/gruntwork-cli/errors"
)
// Returns a formatted string of Launch config Names
func getAllLaunchConfigurations(session *session.Session, region string, excludeAfter time.Time) ([]*string, error) {
svc := autoscaling.New(session)
result, err := svc.DescribeLaunchConfigurations(&autoscaling.DescribeLaunchConfigurationsInput{})
if err != nil {
return nil, errors.WithStackTrace(err)
}
var configNames []*string
for _, config := range result.LaunchConfigurations {
if excludeAfter.After(*config.CreatedTime) {
configNames = append(configNames, config.LaunchConfigurationName)
}
}
return configNames, nil
}
// Deletes all Launch configurations
func nukeAllLaunchConfigurations(session *session.Session, configNames []*string) error {
svc := autoscaling.New(session)
if len(configNames) == 0 {
logging.Logger.Infof("No Launch Configurations to nuke in region %s", *session.Config.Region)
return nil
}
logging.Logger.Infof("Deleting all Launch Configurations in region %s", *session.Config.Region)
var deletedConfigNames []*string
for _, configName := range configNames {
params := &autoscaling.DeleteLaunchConfigurationInput{
LaunchConfigurationName: configName,
}
_, err := svc.DeleteLaunchConfiguration(params)
if err != nil {
logging.Logger.Errorf("[Failed] %s", err)
} else {
deletedConfigNames = append(deletedConfigNames, configName)
logging.Logger.Infof("Deleted Launch configuration: %s", *configName)
}
}
logging.Logger.Infof("[OK] %d Launch Configuration(s) deleted in %s", len(deletedConfigNames), *session.Config.Region)
return nil
}