forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2-instance.go
189 lines (160 loc) · 4.88 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package resources
import (
"context"
"errors"
"fmt"
"time"
"github.com/gotidy/ptr"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
libsettings "github.com/ekristen/libnuke/pkg/settings"
"github.com/ekristen/libnuke/pkg/types"
"github.com/ekristen/aws-nuke/v3/pkg/awsutil"
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)
const EC2InstanceResource = "EC2Instance"
func init() {
registry.Register(®istry.Registration{
Name: EC2InstanceResource,
Scope: nuke.Account,
Resource: &EC2Instance{},
Lister: &EC2InstanceLister{},
Settings: []string{
"DisableDeletionProtection",
"DisableStopProtection",
},
})
}
type EC2InstanceLister struct{}
func (l *EC2InstanceLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)
svc := ec2.New(opts.Session)
params := &ec2.DescribeInstancesInput{}
resources := make([]resource.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,
ID: instance.InstanceId,
ImageID: instance.ImageId,
State: instance.State.Name,
InstanceType: instance.InstanceType,
LaunchTime: instance.LaunchTime,
Tags: instance.Tags,
})
}
}
if resp.NextToken == nil {
break
}
params = &ec2.DescribeInstancesInput{
NextToken: resp.NextToken,
}
}
return resources, nil
}
type EC2Instance struct {
svc *ec2.EC2
settings *libsettings.Setting
ID *string `property:"name=Identifier" description:"The instance ID (e.g. i-1234567890abcdef0)"`
ImageID *string `property:"name=ImageIdentifier" description:"The ID of the AMI used to launch the instance"`
State *string `property:"name=InstanceState" description:"The current state of the instance"`
InstanceType *string `description:"The instance type (e.g. t2.micro)"`
LaunchTime *time.Time `description:"The time the instance was launched"`
Tags []*ec2.Tag `description:"The tags associated with the instance"`
}
func (i *EC2Instance) Settings(setting *libsettings.Setting) {
i.settings = setting
}
func (i *EC2Instance) Filter() error {
if *i.State == ec2.InstanceStateNameTerminated {
return fmt.Errorf("already terminated")
}
return nil
}
func (i *EC2Instance) Remove(_ context.Context) error {
deleteTagsParams := &ec2.DeleteTagsInput{
Resources: []*string{i.ID},
}
if _, err := i.svc.DeleteTags(deleteTagsParams); err != nil {
return err
}
params := &ec2.TerminateInstancesInput{
InstanceIds: []*string{i.ID},
}
if _, err := i.svc.TerminateInstances(params); err != nil {
var awsErr awserr.Error
ok := errors.As(err, &awsErr)
// Check for Termination Protection, disable it, and try termination again.
if ok && awsErr.Code() == awsutil.ErrCodeOperationNotPermitted &&
awsErr.Message() == "The instance '"+*i.ID+"' may not be "+
"terminated. Modify its 'disableApiTermination' instance attribute and "+
"try again." && i.settings.GetBool("DisableDeletionProtection") {
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 {
ok = errors.As(err, &awsErr)
}
}
// Check for Stop Protection, disable it, and try termination again.
if ok && awsErr.Code() == "OperationNotPermitted" &&
awsErr.Message() == "The instance '"+*i.ID+"' may not be "+
"terminated. Modify its 'disableApiStop' instance attribute and try "+
"again." && i.settings.GetBool("DisableStopProtection") {
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.ID,
DisableApiStop: &ec2.AttributeBooleanValue{
Value: ptr.Bool(false),
},
}
_, err := i.svc.ModifyInstanceAttribute(params)
if err != nil {
return err
}
return nil
}
func (i *EC2Instance) DisableTerminationProtection() error {
params := &ec2.ModifyInstanceAttributeInput{
InstanceId: i.ID,
DisableApiTermination: &ec2.AttributeBooleanValue{
Value: ptr.Bool(false),
},
}
_, err := i.svc.ModifyInstanceAttribute(params)
if err != nil {
return err
}
return nil
}
func (i *EC2Instance) Properties() types.Properties {
return types.NewPropertiesFromStruct(i)
}
func (i *EC2Instance) String() string {
return *i.ID
}