/
ec2_instance.go
99 lines (82 loc) · 3.01 KB
/
ec2_instance.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
98
99
package aws
import (
"github.com/aws/aws-sdk-go/service/ec2"
log "github.com/sirupsen/logrus"
"os"
"github.com/Qovery/pleco/pkg/common"
)
type EC2Instance struct {
common.CloudProviderResource
}
func deleteEC2Instances(ec2Session *ec2.EC2, ec2Instances []EC2Instance) {
for _, ec2Instance := range ec2Instances {
instanceIds := []*string{&ec2Instance.Identifier}
_, err := ec2Session.TerminateInstances(&ec2.TerminateInstancesInput{
InstanceIds: instanceIds,
})
if err != nil {
log.Errorf("Can't delete %s in %s", ec2Instance.Identifier, *ec2Session.Config.Region)
} else {
log.Debugf("EC2 instance %s in %s deleted.", ec2Instance.Identifier, *ec2Session.Config.Region)
}
}
}
func listExpiredEC2Instances(ec2Session *ec2.EC2, options *AwsOptions) ([]EC2Instance, error) {
result, err := ec2Session.DescribeInstances(&ec2.DescribeInstancesInput{})
if err != nil {
return nil, err
}
if len(result.Reservations) == 0 {
return nil, nil
}
var expiredEC2Instances []EC2Instance
for _, currentReservation := range result.Reservations {
for _, ec2Instance := range currentReservation.Instances {
// available instance states listed here: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_InstanceState.html
if *ec2Instance.State.Name != "running" {
log.Debugf("Skipping EC2 instance %s in region %s (current status is %s)", *ec2Instance.InstanceId, *ec2Session.Config.Region, *ec2Instance.State.Name)
continue
}
if options.DisableTTLCheck {
vpcId, isOk := os.LookupEnv("PROTECTED_VPC_ID")
if !isOk || vpcId == "" {
log.Fatalf("Unable to get PROTECTED_VPC_ID environment variable in order to protect VPC resources.")
}
if vpcId == *ec2Instance.VpcId {
log.Debugf("Skipping EC2 instance %s in region %s (protected vpc)", *ec2Instance.InstanceId, *ec2Session.Config.Region)
continue
}
}
essentialTags := common.GetEssentialTags(ec2Instance.Tags, options.TagName)
ec2Instance := EC2Instance{
CloudProviderResource: common.CloudProviderResource{
Identifier: *ec2Instance.InstanceId,
Description: "EC2 Instance: " + *ec2Instance.InstanceId,
CreationDate: ec2Instance.LaunchTime.UTC(),
TTL: essentialTags.TTL,
Tag: essentialTags.Tag,
IsProtected: essentialTags.IsProtected,
},
}
if ec2Instance.IsResourceExpired(options.TagValue, options.DisableTTLCheck) {
expiredEC2Instances = append(expiredEC2Instances, ec2Instance)
}
}
}
return expiredEC2Instances, nil
}
func DeleteExpiredEC2Instances(sessions AWSSessions, options AwsOptions) {
expiredEC2Instances, err := listExpiredEC2Instances(sessions.EC2, &options)
region := *sessions.EC2.Config.Region
if err != nil {
log.Errorf("Can't list instances: %s\n", err)
return
}
count, start := common.ElemToDeleteFormattedInfos("expired EC2 instance", len(expiredEC2Instances), region)
log.Info(count)
if options.DryRun || len(expiredEC2Instances) == 0 {
return
}
log.Info(start)
deleteEC2Instances(sessions.EC2, expiredEC2Instances)
}