forked from gruntwork-io/cloud-nuke
/
elasticache.go
332 lines (286 loc) · 11.2 KB
/
elasticache.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package resources
import (
"context"
"fmt"
"strings"
"github.com/andrewderr/cloud-nuke-a1/config"
"github.com/andrewderr/cloud-nuke-a1/logging"
"github.com/andrewderr/cloud-nuke-a1/report"
"github.com/andrewderr/cloud-nuke-a1/telemetry"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/gruntwork-io/go-commons/errors"
commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"
)
// Returns a formatted string of Elasticache cluster Ids
func (cache *Elasticaches) getAll(c context.Context, configObj config.Config) ([]*string, error) {
// First, get any cache clusters that are replication groups, which will be the case for all multi-node Redis clusters
replicationGroupsResult, replicationGroupsErr := cache.Client.DescribeReplicationGroups(&elasticache.DescribeReplicationGroupsInput{})
if replicationGroupsErr != nil {
return nil, errors.WithStackTrace(replicationGroupsErr)
}
// Next, get any cache clusters that are not members of a replication group: meaning:
// 1. any cache clusters with a Engine of "memcached"
// 2. any single node Redis clusters
cacheClustersResult, cacheClustersErr := cache.Client.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{
ShowCacheClustersNotInReplicationGroups: aws.Bool(true),
})
if cacheClustersErr != nil {
return nil, errors.WithStackTrace(cacheClustersErr)
}
var clusterIds []*string
for _, replicationGroup := range replicationGroupsResult.ReplicationGroups {
if configObj.Elasticache.ShouldInclude(config.ResourceValue{
Name: replicationGroup.ReplicationGroupId,
Time: replicationGroup.ReplicationGroupCreateTime,
}) {
clusterIds = append(clusterIds, replicationGroup.ReplicationGroupId)
}
}
for _, cluster := range cacheClustersResult.CacheClusters {
if configObj.Elasticache.ShouldInclude(config.ResourceValue{
Name: cluster.CacheClusterId,
Time: cluster.CacheClusterCreateTime,
}) {
clusterIds = append(clusterIds, cluster.CacheClusterId)
}
}
return clusterIds, nil
}
type CacheClusterType string
const (
Replication CacheClusterType = "replication"
Single CacheClusterType = "single"
)
func (cache *Elasticaches) determineCacheClusterType(clusterId *string) (*string, CacheClusterType, error) {
replicationGroupDescribeParams := &elasticache.DescribeReplicationGroupsInput{
ReplicationGroupId: clusterId,
}
replicationGroupOutput, describeReplicationGroupsErr := cache.Client.DescribeReplicationGroups(replicationGroupDescribeParams)
if describeReplicationGroupsErr != nil {
if awsErr, ok := describeReplicationGroupsErr.(awserr.Error); ok {
if awsErr.Code() == elasticache.ErrCodeReplicationGroupNotFoundFault {
// It's possible that we're looking at a cache cluster, in which case we can safely ignore this error
} else {
return nil, Single, describeReplicationGroupsErr
}
}
}
if len(replicationGroupOutput.ReplicationGroups) > 0 {
return replicationGroupOutput.ReplicationGroups[0].ReplicationGroupId, Replication, nil
}
// A single cache cluster can either be standalone, in the case where Engine is memcached,
// or a member of a replication group, in the case where Engine is Redis, so we must
// check the current clusterId via both describe methods, otherwise we'll fail to find it
describeParams := &elasticache.DescribeCacheClustersInput{
CacheClusterId: clusterId,
}
cacheClustersOutput, describeErr := cache.Client.DescribeCacheClusters(describeParams)
if describeErr != nil {
if awsErr, ok := describeErr.(awserr.Error); ok {
if awsErr.Code() == elasticache.ErrCodeCacheClusterNotFoundFault {
// It's possible that we're looking at a replication group, in which case we can safely ignore this error
} else {
return nil, Single, describeErr
}
}
}
if len(cacheClustersOutput.CacheClusters) == 1 {
return cacheClustersOutput.CacheClusters[0].CacheClusterId, Single, nil
}
return nil, Single, CouldNotLookupCacheClusterErr{ClusterId: clusterId}
}
func (cache *Elasticaches) nukeNonReplicationGroupElasticacheCluster(clusterId *string) error {
logging.Debugf("Deleting Elasticache cluster Id: %s which is not a member of a replication group", aws.StringValue(clusterId))
params := elasticache.DeleteCacheClusterInput{
CacheClusterId: clusterId,
}
_, err := cache.Client.DeleteCacheCluster(¶ms)
if err != nil {
return err
}
return cache.Client.WaitUntilCacheClusterDeleted(&elasticache.DescribeCacheClustersInput{
CacheClusterId: clusterId,
})
}
func (cache *Elasticaches) nukeReplicationGroupMemberElasticacheCluster(clusterId *string) error {
logging.Debugf("Elasticache cluster Id: %s is a member of a replication group. Therefore, deleting its replication group", aws.StringValue(clusterId))
params := &elasticache.DeleteReplicationGroupInput{
ReplicationGroupId: clusterId,
}
_, err := cache.Client.DeleteReplicationGroup(params)
if err != nil {
return err
}
waitErr := cache.Client.WaitUntilReplicationGroupDeleted(&elasticache.DescribeReplicationGroupsInput{
ReplicationGroupId: clusterId,
})
if waitErr != nil {
return waitErr
}
logging.Debugf("Successfully deleted replication group Id: %s", aws.StringValue(clusterId))
return nil
}
func (cache *Elasticaches) nukeAll(clusterIds []*string) error {
if len(clusterIds) == 0 {
logging.Debugf("No Elasticache clusters to nuke in region %s", cache.Region)
return nil
}
logging.Debugf("Deleting %d Elasticache clusters in region %s", len(clusterIds), cache.Region)
var deletedClusterIds []*string
for _, clusterId := range clusterIds {
// We need to look up the cache cluster again to determine if it is a member of a replication group or not,
// because there are two separate codepaths for deleting a cluster. Cache clusters that are not members of a
// replication group can be deleted via DeleteCacheCluster, whereas those that are members require a call to
// DeleteReplicationGroup, which will destroy both the replication group and its member clusters
clusterId, clusterType, describeErr := cache.determineCacheClusterType(clusterId)
if describeErr != nil {
return describeErr
}
var err error
if clusterType == Single {
err = cache.nukeNonReplicationGroupElasticacheCluster(clusterId)
} else if clusterType == Replication {
err = cache.nukeReplicationGroupMemberElasticacheCluster(clusterId)
}
// Record status of this resource
e := report.Entry{
Identifier: aws.StringValue(clusterId),
ResourceType: "Elasticache",
Error: err,
}
report.Record(e)
if err != nil {
logging.Debugf("[Failed] %s", err)
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking Elasticache Cluster",
}, map[string]interface{}{
"region": cache.Region,
})
} else {
deletedClusterIds = append(deletedClusterIds, clusterId)
logging.Debugf("Deleted Elasticache cluster: %s", *clusterId)
}
}
logging.Debugf("[OK] %d Elasticache clusters deleted in %s", len(deletedClusterIds), cache.Region)
return nil
}
// Custom errors
type CouldNotLookupCacheClusterErr struct {
ClusterId *string
}
func (err CouldNotLookupCacheClusterErr) Error() string {
return fmt.Sprintf("Failed to lookup clusterId: %s", aws.StringValue(err.ClusterId))
}
/*
Elasticache Parameter Groups
*/
func (pg *ElasticacheParameterGroups) getAll(c context.Context, configObj config.Config) ([]*string, error) {
var paramGroupNames []*string
err := pg.Client.DescribeCacheParameterGroupsPages(
&elasticache.DescribeCacheParameterGroupsInput{},
func(page *elasticache.DescribeCacheParameterGroupsOutput, lastPage bool) bool {
for _, paramGroup := range page.CacheParameterGroups {
if pg.shouldInclude(paramGroup, configObj) {
paramGroupNames = append(paramGroupNames, paramGroup.CacheParameterGroupName)
}
}
return !lastPage
},
)
return paramGroupNames, errors.WithStackTrace(err)
}
func (pg *ElasticacheParameterGroups) shouldInclude(paramGroup *elasticache.CacheParameterGroup, configObj config.Config) bool {
if paramGroup == nil {
return false
}
//Exclude AWS managed resources. user defined resources are unable to begin with "default."
if strings.HasPrefix(aws.StringValue(paramGroup.CacheParameterGroupName), "default.") {
return false
}
return configObj.ElasticacheParameterGroups.ShouldInclude(config.ResourceValue{
Name: paramGroup.CacheParameterGroupName,
})
}
func (pg *ElasticacheParameterGroups) nukeAll(paramGroupNames []*string) error {
if len(paramGroupNames) == 0 {
logging.Debugf("No Elasticache parameter groups to nuke in region %s", pg.Region)
return nil
}
var deletedGroupNames []*string
for _, pgName := range paramGroupNames {
_, err := pg.Client.DeleteCacheParameterGroup(&elasticache.DeleteCacheParameterGroupInput{CacheParameterGroupName: pgName})
// Record status of this resource
e := report.Entry{
Identifier: aws.StringValue(pgName),
ResourceType: "Elasticache Parameter Group",
Error: err,
}
report.Record(e)
if err != nil {
logging.Debugf("[Failed] %s", err)
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking Elasticache Parameter Group",
}, map[string]interface{}{
"region": pg.Region,
})
} else {
deletedGroupNames = append(deletedGroupNames, pgName)
logging.Debugf("Deleted Elasticache parameter group: %s", aws.StringValue(pgName))
}
}
logging.Debugf("[OK] %d Elasticache parameter groups deleted in %s", len(deletedGroupNames), pg.Region)
return nil
}
/*
Elasticache Subnet Groups
*/
func (sg *ElasticacheSubnetGroups) getAll(c context.Context, configObj config.Config) ([]*string, error) {
var subnetGroupNames []*string
err := sg.Client.DescribeCacheSubnetGroupsPages(
&elasticache.DescribeCacheSubnetGroupsInput{},
func(page *elasticache.DescribeCacheSubnetGroupsOutput, lastPage bool) bool {
for _, subnetGroup := range page.CacheSubnetGroups {
if !strings.Contains(*subnetGroup.CacheSubnetGroupName, "default") &&
configObj.ElasticacheSubnetGroups.ShouldInclude(config.ResourceValue{
Name: subnetGroup.CacheSubnetGroupName,
}) {
subnetGroupNames = append(subnetGroupNames, subnetGroup.CacheSubnetGroupName)
}
}
return !lastPage
},
)
return subnetGroupNames, errors.WithStackTrace(err)
}
func (sg *ElasticacheSubnetGroups) nukeAll(subnetGroupNames []*string) error {
if len(subnetGroupNames) == 0 {
logging.Debugf("No Elasticache subnet groups to nuke in region %s", sg.Region)
return nil
}
var deletedGroupNames []*string
for _, sgName := range subnetGroupNames {
_, err := sg.Client.DeleteCacheSubnetGroup(&elasticache.DeleteCacheSubnetGroupInput{CacheSubnetGroupName: sgName})
// Record status of this resource
e := report.Entry{
Identifier: aws.StringValue(sgName),
ResourceType: "Elasticache Subnet Group",
Error: err,
}
report.Record(e)
if err != nil {
logging.Debugf("[Failed] %s", err)
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking Elasticache Subnet Group",
}, map[string]interface{}{
"region": sg.Region,
})
} else {
deletedGroupNames = append(deletedGroupNames, sgName)
logging.Debugf("Deleted Elasticache subnet group: %s", aws.StringValue(sgName))
}
}
logging.Debugf("[OK] %d Elasticache subnet groups deleted in %s", len(deletedGroupNames), sg.Region)
return nil
}