forked from rebuy-de/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2-instances.go
158 lines (136 loc) · 3.94 KB
/
ec2-instances.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package resources
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/dngferreira/aws-nuke/v2/pkg/config"
"github.com/dngferreira/aws-nuke/v2/pkg/types"
)
type EC2Instance struct {
svc *ec2.EC2
instance *ec2.Instance
featureFlags config.FeatureFlags
}
func init() {
register("EC2Instance", ListEC2Instances)
}
func ListEC2Instances(sess *session.Session) ([]Resource, error) {
svc := ec2.New(sess)
params := &ec2.DescribeInstancesInput{}
resources := make([]Resource, 0)
for {
resp, err := svc.DescribeInstances(params)
if err != nil {
return nil, err
}
for _, reservation := range resp.Reservations {
for _, instance := range reservation.Instances {
resources = append(resources, &EC2Instance{
svc: svc,
instance: instance,
})
}
}
if resp.NextToken == nil {
break
}
params = &ec2.DescribeInstancesInput{
NextToken: resp.NextToken,
}
}
return resources, nil
}
func (i *EC2Instance) FeatureFlags(ff config.FeatureFlags) {
i.featureFlags = ff
}
func (i *EC2Instance) Filter() error {
if *i.instance.State.Name == "terminated" {
return fmt.Errorf("already terminated")
}
return nil
}
func (i *EC2Instance) Remove() error {
params := &ec2.TerminateInstancesInput{
InstanceIds: []*string{i.instance.InstanceId},
}
_, err := i.svc.TerminateInstances(params)
if err != nil {
awsErr, ok := err.(awserr.Error)
// Check for Termination Protection, disable it, and try termination again.
if ok && awsErr.Code() == "OperationNotPermitted" &&
awsErr.Message() == "The instance '"+*i.instance.InstanceId+"' may not be "+
"terminated. Modify its 'disableApiTermination' instance attribute and "+
"try again." && i.featureFlags.DisableDeletionProtection.EC2Instance {
termErr := i.DisableTerminationProtection()
if termErr != nil {
return termErr
}
_, err = i.svc.TerminateInstances(params)
// If we still get an error, we'll check for type and let the next routine
// handle it.
if err != nil {
awsErr, ok = err.(awserr.Error)
}
}
// Check for Stop Protection, disable it, and try termination again.
if ok && awsErr.Code() == "OperationNotPermitted" &&
awsErr.Message() == "The instance '"+*i.instance.InstanceId+"' may not be "+
"terminated. Modify its 'disableApiStop' instance attribute and try "+
"again." && i.featureFlags.DisableEC2InstanceStopProtection {
stopErr := i.DisableStopProtection()
if stopErr != nil {
return stopErr
}
_, err = i.svc.TerminateInstances(params)
}
// If we still have an error at this point, we'll return it.
if err != nil {
return err
}
}
return nil
}
func (i *EC2Instance) DisableStopProtection() error {
params := &ec2.ModifyInstanceAttributeInput{
InstanceId: i.instance.InstanceId,
DisableApiStop: &ec2.AttributeBooleanValue{
Value: aws.Bool(false),
},
}
_, err := i.svc.ModifyInstanceAttribute(params)
if err != nil {
return err
}
return nil
}
func (i *EC2Instance) DisableTerminationProtection() error {
params := &ec2.ModifyInstanceAttributeInput{
InstanceId: i.instance.InstanceId,
DisableApiTermination: &ec2.AttributeBooleanValue{
Value: aws.Bool(false),
},
}
_, err := i.svc.ModifyInstanceAttribute(params)
if err != nil {
return err
}
return nil
}
func (i *EC2Instance) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("Identifier", i.instance.InstanceId)
properties.Set("ImageIdentifier", i.instance.ImageId)
properties.Set("InstanceState", i.instance.State.Name)
properties.Set("InstanceType", i.instance.InstanceType)
properties.Set("LaunchTime", i.instance.LaunchTime.Format(time.RFC3339))
for _, tagValue := range i.instance.Tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}
return properties
}
func (i *EC2Instance) String() string {
return *i.instance.InstanceId
}