-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
67 lines (56 loc) · 1.22 KB
/
main.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
package main
import (
"log"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)
func listAvailableVolumes(cfg aws.Config) ([]string, error) {
volumes := make([]string, 0)
svc := ec2.New(cfg)
req := svc.DescribeVolumesRequest(&ec2.DescribeVolumesInput{
Filters: []ec2.Filter{
ec2.Filter{
Name: aws.String("status"),
Values: []string{"available"},
},
},
})
res, err := req.Send()
if err != nil {
return volumes, err
}
for _, volume := range res.Volumes {
volumes = append(volumes, *volume.VolumeId)
}
return volumes, nil
}
func deleteMovie(cfg aws.Config, volumeId string) error {
svc := ec2.New(cfg)
req := svc.DeleteVolumeRequest(&ec2.DeleteVolumeInput{
VolumeId: &volumeId,
})
_, err := req.Send()
return err
}
func handler() error {
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
return err
}
volumes, err := listAvailableVolumes(cfg)
if err != nil {
return err
}
for _, volume := range volumes {
log.Println("Removing volume: ", volume)
if err != deleteMovie(cfg, volume) {
return err
}
}
return nil
}
func main() {
lambda.Start(handler)
}