-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathaws.go
2048 lines (1907 loc) · 67 KB
/
aws.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package aws
import (
"fmt"
"math/rand"
"sort"
"strings"
"time"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/gruntwork-io/cloud-nuke/telemetry"
commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"
"github.com/aws/aws-sdk-go/aws"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/externalcreds"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/progressbar"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/go-commons/collections"
"github.com/gruntwork-io/go-commons/errors"
)
// OptInNotRequiredRegions contains all regions that are enabled by default on new AWS accounts
// Beginning in Spring 2019, AWS requires new regions to be explicitly enabled
// See https://aws.amazon.com/blogs/security/setting-permissions-to-enable-accounts-for-upcoming-aws-regions/
var OptInNotRequiredRegions = []string{
"eu-north-1",
"ap-south-1",
"eu-west-3",
"eu-west-2",
"eu-west-1",
"ap-northeast-3",
"ap-northeast-2",
"ap-northeast-1",
"sa-east-1",
"ca-central-1",
"ap-southeast-1",
"ap-southeast-2",
"eu-central-1",
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2",
}
// GovCloudRegions contains all of the U.S. GovCloud regions. In accounts with GovCloud enabled, these are the
// only available regions.
var GovCloudRegions = []string{
"us-gov-east-1",
"us-gov-west-1",
}
const (
GlobalRegion string = "global"
)
func newSession(region string) *session.Session {
return externalcreds.Get(region)
}
// Try a describe regions command with the most likely enabled regions
func retryDescribeRegions() (*ec2.DescribeRegionsOutput, error) {
regionsToTry := append(OptInNotRequiredRegions, GovCloudRegions...)
for _, region := range regionsToTry {
svc := ec2.New(newSession(region))
regions, err := svc.DescribeRegions(&ec2.DescribeRegionsInput{})
if err != nil {
continue
}
return regions, nil
}
return nil, errors.WithStackTrace(fmt.Errorf("could not find any enabled regions"))
}
// GetEnabledRegions - Get all regions that are enabled (DescribeRegions excludes those not enabled by default)
func GetEnabledRegions() ([]string, error) {
var regionNames []string
// We don't want to depend on a default region being set, so instead we
// will choose a region from the list of regions that are enabled by default
// and use that to enumerate all enabled regions.
// Corner case: user has intentionally disabled one or more regions that are
// enabled by default. If that region is chosen, API calls will fail.
// Therefore we retry until one of the regions works.
regions, err := retryDescribeRegions()
if err != nil {
return nil, err
}
for _, region := range regions.Regions {
regionNames = append(regionNames, awsgo.StringValue(region.RegionName))
}
return regionNames, nil
}
func getRandomRegion() (string, error) {
return getRandomRegionWithExclusions([]string{})
}
// getRandomRegionWithExclusions - return random from enabled regions, excluding regions from the argument
func getRandomRegionWithExclusions(regionsToExclude []string) (string, error) {
allRegions, err := GetEnabledRegions()
if err != nil {
return "", errors.WithStackTrace(err)
}
rand.Seed(time.Now().UnixNano())
// exclude from "allRegions"
exclusions := make(map[string]string)
for _, region := range regionsToExclude {
exclusions[region] = region
}
// filter regions
var updatedRegions []string
for _, region := range allRegions {
_, excluded := exclusions[region]
if !excluded {
updatedRegions = append(updatedRegions, region)
}
}
randIndex := rand.Intn(len(updatedRegions))
logging.Logger.Debugf("Random region chosen: %s", updatedRegions[randIndex])
return updatedRegions[randIndex], nil
}
func split(identifiers []string, limit int) [][]string {
if limit < 0 {
limit = -1 * limit
} else if limit == 0 {
return [][]string{identifiers}
}
var chunk []string
chunks := make([][]string, 0, len(identifiers)/limit+1)
for len(identifiers) >= limit {
chunk, identifiers = identifiers[:limit], identifiers[limit:]
chunks = append(chunks, chunk)
}
if len(identifiers) > 0 {
chunks = append(chunks, identifiers[:])
}
return chunks
}
// GetTargetRegions - Used enabled, selected and excluded regions to create a
// final list of valid regions
func GetTargetRegions(enabledRegions []string, selectedRegions []string, excludedRegions []string) ([]string, error) {
if len(enabledRegions) == 0 {
return nil, fmt.Errorf("Cannot have empty enabled regions")
}
// neither selectedRegions nor excludedRegions => select enabledRegions
if len(selectedRegions) == 0 && len(excludedRegions) == 0 {
return enabledRegions, nil
}
if len(selectedRegions) > 0 && len(excludedRegions) > 0 {
return nil, fmt.Errorf("Cannot specify both selected and excluded regions")
}
var invalidRegions []string
// Validate selectedRegions
for _, selectedRegion := range selectedRegions {
if !collections.ListContainsElement(enabledRegions, selectedRegion) {
invalidRegions = append(invalidRegions, selectedRegion)
}
}
if len(invalidRegions) > 0 {
return nil, fmt.Errorf("Invalid values for region: [%s]", invalidRegions)
}
if len(selectedRegions) > 0 {
return selectedRegions, nil
}
// Validate excludedRegions
for _, excludedRegion := range excludedRegions {
if !collections.ListContainsElement(enabledRegions, excludedRegion) {
invalidRegions = append(invalidRegions, excludedRegion)
}
}
if len(invalidRegions) > 0 {
return nil, fmt.Errorf("Invalid values for exclude-region: [%s]", invalidRegions)
}
// Filter out excludedRegions from enabledRegions
var targetRegions []string
if len(excludedRegions) > 0 {
for _, region := range enabledRegions {
if !collections.ListContainsElement(excludedRegions, region) {
targetRegions = append(targetRegions, region)
}
}
}
if len(targetRegions) == 0 {
return nil, fmt.Errorf("Cannot exclude all regions: %s", excludedRegions)
}
return targetRegions, nil
}
// GetAllResources - Lists all aws resources
func GetAllResources(targetRegions []string, excludeAfter time.Time, resourceTypes []string, configObj config.Config, allowDeleteUnaliasedKeys bool) (*AwsAccountResources, error) {
account := AwsAccountResources{
Resources: make(map[string]AwsRegionResource),
}
count := 1
totalRegions := len(targetRegions)
resourcesCache := map[string]map[string][]*string{}
defaultRegion := targetRegions[0]
for _, region := range targetRegions {
// The "global" region case is handled outside this loop
if region == GlobalRegion {
continue
}
logging.Logger.Debugf("Checking region [%d/%d]: %s", count, totalRegions, region)
cloudNukeSession := newSession(region)
stsService := sts.New(cloudNukeSession)
resp, err := stsService.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err == nil {
telemetry.SetAccountId(*resp.Account)
}
resourcesInRegion := AwsRegionResource{}
// The order in which resources are nuked is important
// because of dependencies between resources
// ACMPCA arns
acmpca := ACMPCA{}
if IsNukeable(acmpca.ResourceName(), resourceTypes) {
start := time.Now()
arns, err := getAllACMPCA(cloudNukeSession, region, excludeAfter)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve ACMPCAs",
ResourceType: acmpca.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing ACMPA arns",
}, map[string]interface{}{
"recordCount": len(arns),
"actionTime": time.Since(start).Seconds(),
})
if len(arns) > 0 {
acmpca.ARNs = awsgo.StringValueSlice(arns)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, acmpca)
}
}
// End ACMPCA arns
// ASG Names
asGroups := ASGroups{}
if IsNukeable(asGroups.ResourceName(), resourceTypes) {
start := time.Now()
groupNames, err := getAllAutoScalingGroups(cloudNukeSession, region, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve Auto-Scaling Groups",
ResourceType: asGroups.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing ASGs",
}, map[string]interface{}{
"region": region,
"recordCount": len(groupNames),
"actionTime": time.Since(start).Seconds(),
})
if len(groupNames) > 0 {
asGroups.GroupNames = awsgo.StringValueSlice(groupNames)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, asGroups)
}
}
// End ASG Names
// Launch Configuration Names
configs := LaunchConfigs{}
if IsNukeable(configs.ResourceName(), resourceTypes) {
start := time.Now()
configNames, err := getAllLaunchConfigurations(cloudNukeSession, region, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve Launch configurations",
ResourceType: configs.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing Launch Configurations",
}, map[string]interface{}{
"region": region,
"recordCount": len(configNames),
"actionTime": time.Since(start).Seconds(),
})
if len(configNames) > 0 {
configs.LaunchConfigurationNames = awsgo.StringValueSlice(configNames)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, configs)
}
}
// End Launch Configuration Names
// Launch Template Names
templates := LaunchTemplates{}
if IsNukeable(templates.ResourceName(), resourceTypes) {
start := time.Now()
templateNames, err := getAllLaunchTemplates(cloudNukeSession, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve Launch templates",
ResourceType: templates.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing Launch Templates",
}, map[string]interface{}{
"region": region,
"recordCount": len(templateNames),
"actionTime": time.Since(start).Seconds(),
})
if len(templateNames) > 0 {
templates.LaunchTemplateNames = awsgo.StringValueSlice(templateNames)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, templates)
}
}
// End Launch Template Names
// LoadBalancer Names
loadBalancers := LoadBalancers{}
if IsNukeable(loadBalancers.ResourceName(), resourceTypes) {
start := time.Now()
elbNames, err := getAllElbInstances(cloudNukeSession, region, excludeAfter)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve load balancers",
ResourceType: loadBalancers.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing ELBs",
}, map[string]interface{}{
"region": region,
"recordCount": len(elbNames),
"actionTime": time.Since(start).Seconds(),
})
if len(elbNames) > 0 {
loadBalancers.Names = awsgo.StringValueSlice(elbNames)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, loadBalancers)
}
}
// End LoadBalancer Names
// LoadBalancerV2 Arns
loadBalancersV2 := LoadBalancersV2{}
if IsNukeable(loadBalancersV2.ResourceName(), resourceTypes) {
start := time.Now()
elbv2Arns, err := getAllElbv2Instances(cloudNukeSession, region, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve load balancers v2",
ResourceType: loadBalancersV2.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing ELBV2s",
}, map[string]interface{}{
"region": region,
"recordCount": len(elbv2Arns),
"actionTime": time.Since(start).Seconds(),
})
if len(elbv2Arns) > 0 {
loadBalancersV2.Arns = awsgo.StringValueSlice(elbv2Arns)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, loadBalancersV2)
}
}
// End LoadBalancerV2 Arns
// SQS Queues
sqsQueue := SqsQueue{}
if IsNukeable(sqsQueue.ResourceName(), resourceTypes) {
start := time.Now()
queueUrls, err := getAllSqsQueue(cloudNukeSession, region, excludeAfter)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve SQS queues",
ResourceType: sqsQueue.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing SQS Queues",
}, map[string]interface{}{
"region": region,
"recordCount": len(queueUrls),
"actionTime": time.Since(start).Seconds(),
})
if len(queueUrls) > 0 {
sqsQueue.QueueUrls = awsgo.StringValueSlice(queueUrls)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, sqsQueue)
}
}
// End SQS Queue
// TransitGatewayVpcAttachment
transitGatewayVpcAttachments := TransitGatewaysVpcAttachment{}
transitGatewayIsAvailable, err := tgIsAvailableInRegion(cloudNukeSession, region)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve Transit Gateways",
ResourceType: transitGatewayVpcAttachments.ResourceName(),
}
report.RecordError(ge)
}
if IsNukeable(transitGatewayVpcAttachments.ResourceName(), resourceTypes) && transitGatewayIsAvailable {
start := time.Now()
transitGatewayVpcAttachmentIds, err := getAllTransitGatewayVpcAttachments(cloudNukeSession, region, excludeAfter)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Could not retrieve Transit Gateway attachments",
ResourceType: transitGatewayVpcAttachments.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing Transit Gateway VPC Attachments",
}, map[string]interface{}{
"region": region,
"recordCount": len(transitGatewayVpcAttachmentIds),
"actionTime": time.Since(start).Seconds(),
})
if len(transitGatewayVpcAttachmentIds) > 0 {
transitGatewayVpcAttachments.Ids = awsgo.StringValueSlice(transitGatewayVpcAttachmentIds)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, transitGatewayVpcAttachments)
}
}
// End TransitGatewayVpcAttachment
// TransitGatewayRouteTable
transitGatewayRouteTables := TransitGatewaysRouteTables{}
if IsNukeable(transitGatewayRouteTables.ResourceName(), resourceTypes) && transitGatewayIsAvailable {
start := time.Now()
transitGatewayRouteTableIds, err := getAllTransitGatewayRouteTables(cloudNukeSession, region, excludeAfter)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve Transit Gateway route tables",
ResourceType: transitGatewayRouteTables.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing Transit Gateway Route Tables",
}, map[string]interface{}{
"region": region,
"recordCount": len(transitGatewayRouteTableIds),
"actionTime": time.Since(start).Seconds(),
})
if len(transitGatewayRouteTableIds) > 0 {
transitGatewayRouteTables.Ids = awsgo.StringValueSlice(transitGatewayRouteTableIds)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, transitGatewayRouteTables)
}
}
// End TransitGatewayRouteTable
// TransitGateway
transitGateways := TransitGateways{}
if IsNukeable(transitGateways.ResourceName(), resourceTypes) && transitGatewayIsAvailable {
start := time.Now()
transitGatewayIds, err := getAllTransitGatewayInstances(cloudNukeSession, region, excludeAfter)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve Transit Gateways",
ResourceType: transitGateways.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing Transit Gateway Instances",
}, map[string]interface{}{
"region": region,
"recordCount": len(transitGatewayIds),
"actionTime": time.Since(start).Seconds(),
})
if len(transitGatewayIds) > 0 {
transitGateways.Ids = awsgo.StringValueSlice(transitGatewayIds)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, transitGateways)
}
}
// End TransitGateway
// NATGateway
natGateways := NatGateways{}
if IsNukeable(natGateways.ResourceName(), resourceTypes) {
start := time.Now()
ngwIDs, err := getAllNatGateways(cloudNukeSession, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve NAT Gateways",
ResourceType: natGateways.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing NAT Gateways",
}, map[string]interface{}{
"region": region,
"recordCount": len(ngwIDs),
"actionTime": time.Since(start).Seconds(),
})
if len(ngwIDs) > 0 {
natGateways.NatGatewayIDs = awsgo.StringValueSlice(ngwIDs)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, natGateways)
}
}
// End NATGateway
// OpenSearch Domains
domains := OpenSearchDomains{}
if IsNukeable(domains.ResourceName(), resourceTypes) {
start := time.Now()
domainNames, err := getOpenSearchDomainsToNuke(cloudNukeSession, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve OpenSearch Domains",
ResourceType: domains.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing Opensearch Domains",
}, map[string]interface{}{
"region": region,
"recordCount": len(domainNames),
"actionTime": time.Since(start).Seconds(),
})
if len(domainNames) > 0 {
domains.DomainNames = awsgo.StringValueSlice(domainNames)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, domains)
}
}
// End OpenSearchDomains
// EC2 Instances
ec2Instances := EC2Instances{}
if IsNukeable(ec2Instances.ResourceName(), resourceTypes) {
start := time.Now()
instanceIds, err := getAllEc2Instances(cloudNukeSession, region, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve EC2 instances",
ResourceType: ec2Instances.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing EC2 Instances",
}, map[string]interface{}{
"region": region,
"recordCount": len(instanceIds),
"actionTime": time.Since(start).Seconds(),
})
if len(instanceIds) > 0 {
ec2Instances.InstanceIds = awsgo.StringValueSlice(instanceIds)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, ec2Instances)
}
}
// End EC2 Instances
// EC2 Dedicated Hosts
ec2DedicatedHosts := EC2DedicatedHosts{}
if IsNukeable(ec2DedicatedHosts.ResourceName(), resourceTypes) {
start := time.Now()
hostIds, err := getAllEc2DedicatedHosts(cloudNukeSession, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve EC2 dedicated hosts",
ResourceType: ec2DedicatedHosts.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing EC2 Dedicated Hosts",
}, map[string]interface{}{
"region": region,
"recordCount": len(hostIds),
"actionTime": time.Since(start).Seconds(),
})
if len(hostIds) > 0 {
ec2DedicatedHosts.HostIds = awsgo.StringValueSlice(hostIds)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, ec2DedicatedHosts)
}
}
// End EC2 Dedicated Hosts
// EBS Volumes
ebsVolumes := EBSVolumes{}
if IsNukeable(ebsVolumes.ResourceName(), resourceTypes) {
start := time.Now()
volumeIds, err := getAllEbsVolumes(cloudNukeSession, region, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve EBS volumes",
ResourceType: ebsVolumes.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing EBS Volumes",
}, map[string]interface{}{
"region": region,
"recordCount": len(volumeIds),
"actionTime": time.Since(start).Seconds(),
})
if len(volumeIds) > 0 {
ebsVolumes.VolumeIds = awsgo.StringValueSlice(volumeIds)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, ebsVolumes)
}
}
// End EBS Volumes
// EIP Addresses
eipAddresses := EIPAddresses{}
if IsNukeable(eipAddresses.ResourceName(), resourceTypes) {
start := time.Now()
allocationIds, err := getAllEIPAddresses(cloudNukeSession, region, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve EIP addresses",
ResourceType: eipAddresses.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing EIPs",
}, map[string]interface{}{
"region": region,
"recordCount": len(allocationIds),
"actionTime": time.Since(start).Seconds(),
})
if len(allocationIds) > 0 {
eipAddresses.AllocationIds = awsgo.StringValueSlice(allocationIds)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, eipAddresses)
}
}
// End EIP Addresses
// AMIs
amis := AMIs{}
if IsNukeable(amis.ResourceName(), resourceTypes) {
start := time.Now()
imageIds, err := getAllAMIs(cloudNukeSession, region, excludeAfter)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve AMIs",
ResourceType: amis.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing AMIs",
}, map[string]interface{}{
"region": region,
"recordCount": len(imageIds),
"actionTime": time.Since(start).Seconds(),
})
if len(imageIds) > 0 {
amis.ImageIds = awsgo.StringValueSlice(imageIds)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, amis)
}
}
// End AMIs
// Snapshots
snapshots := Snapshots{}
if IsNukeable(snapshots.ResourceName(), resourceTypes) {
start := time.Now()
snapshotIds, err := getAllSnapshots(cloudNukeSession, region, excludeAfter)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve Snapshots",
ResourceType: snapshots.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing Snapshots",
}, map[string]interface{}{
"region": region,
"recordCount": len(snapshotIds),
"actionTime": time.Since(start).Seconds(),
})
if len(snapshotIds) > 0 {
snapshots.SnapshotIds = awsgo.StringValueSlice(snapshotIds)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, snapshots)
}
}
// End Snapshots
// ECS resources
ecsServices := ECSServices{}
if IsNukeable(ecsServices.ResourceName(), resourceTypes) {
start := time.Now()
clusterArns, err := getAllEcsClusters(cloudNukeSession)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve ECS clusters",
ResourceType: ecsServices.ResourceName(),
}
report.RecordError(ge)
}
if len(clusterArns) > 0 {
serviceArns, serviceClusterMap, err := getAllEcsServices(cloudNukeSession, clusterArns, excludeAfter, configObj)
if err != nil {
return nil, errors.WithStackTrace(err)
}
ecsServices.Services = awsgo.StringValueSlice(serviceArns)
ecsServices.ServiceClusterMap = serviceClusterMap
resourcesInRegion.Resources = append(resourcesInRegion.Resources, ecsServices)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing ECS Services",
}, map[string]interface{}{
"region": region,
"recordCount": len(clusterArns),
"actionTime": time.Since(start).Seconds(),
})
}
ecsClusters := ECSClusters{}
if IsNukeable(ecsClusters.ResourceName(), resourceTypes) {
start := time.Now()
ecsClusterArns, err := getAllEcsClustersOlderThan(cloudNukeSession, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve ECS clusters",
ResourceType: ecsClusters.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing ECS Clusters",
}, map[string]interface{}{
"region": region,
"recordCount": len(ecsClusterArns),
"actionTime": time.Since(start).Seconds(),
})
if len(ecsClusterArns) > 0 {
ecsClusters.ClusterArns = awsgo.StringValueSlice(ecsClusterArns)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, ecsClusters)
}
}
// End ECS resources
// EKS resources
eksClusters := EKSClusters{}
if IsNukeable(eksClusters.ResourceName(), resourceTypes) {
start := time.Now()
eksClusterNames, err := getAllEksClusters(cloudNukeSession, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve EKS clusters",
ResourceType: eksClusters.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing EKS Clusters",
}, map[string]interface{}{
"region": region,
"recordCount": len(eksClusterNames),
"actionTime": time.Since(start).Seconds(),
})
if len(eksClusterNames) > 0 {
eksClusters.Clusters = awsgo.StringValueSlice(eksClusterNames)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, eksClusters)
}
}
// End EKS resources
// RDS DB Instances
dbInstances := DBInstances{}
if IsNukeable(dbInstances.ResourceName(), resourceTypes) {
start := time.Now()
instanceNames, err := getAllRdsInstances(cloudNukeSession, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve DB instances",
ResourceType: dbInstances.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing RDS Instances",
}, map[string]interface{}{
"region": region,
"recordCount": len(instanceNames),
"actionTime": time.Since(start).Seconds(),
})
if len(instanceNames) > 0 {
dbInstances.InstanceNames = awsgo.StringValueSlice(instanceNames)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, dbInstances)
}
}
// End RDS DB Instances
// RDS DB Clusters
// These reference the Aurora Clusters, for the use it's the same resource (rds), but AWS
// has different abstractions for each.
dbClusters := DBClusters{}
if IsNukeable(dbClusters.ResourceName(), resourceTypes) {
start := time.Now()
clustersNames, err := getAllRdsClusters(cloudNukeSession, excludeAfter)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve DB clusters",
ResourceType: dbClusters.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing RDS Clusters",
}, map[string]interface{}{
"region": region,
"recordCount": len(clustersNames),
"actionTime": time.Since(start).Seconds(),
})
if len(clustersNames) > 0 {
dbClusters.InstanceNames = awsgo.StringValueSlice(clustersNames)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, dbClusters)
}
}
// End RDS DB Clusters
// Lambda Functions
lambdaFunctions := LambdaFunctions{}
if IsNukeable(lambdaFunctions.ResourceName(), resourceTypes) {
start := time.Now()
lambdaFunctionNames, err := getAllLambdaFunctions(cloudNukeSession, excludeAfter, configObj, lambdaFunctions.MaxBatchSize())
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve Lambda functions",
ResourceType: lambdaFunctions.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing Lambda Functions",
}, map[string]interface{}{
"region": region,
"recordCount": len(lambdaFunctionNames),
"actionTime": time.Since(start).Seconds(),
})
if len(lambdaFunctionNames) > 0 {
lambdaFunctions.LambdaFunctionNames = awsgo.StringValueSlice(lambdaFunctionNames)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, lambdaFunctions)
}
}
// End Lambda Functions
// Secrets Manager Secrets
secretsManagerSecrets := SecretsManagerSecrets{}
if IsNukeable(secretsManagerSecrets.ResourceName(), resourceTypes) {
start := time.Now()
secrets, err := getAllSecretsManagerSecrets(cloudNukeSession, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve Secrets managers entries",
ResourceType: secretsManagerSecrets.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing Secrets Manager Secrets",
}, map[string]interface{}{
"region": region,
"recordCount": len(secrets),
"actionTime": time.Since(start).Seconds(),
})
if len(secrets) > 0 {
secretsManagerSecrets.SecretIDs = awsgo.StringValueSlice(secrets)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, secretsManagerSecrets)
}
}
// End Secrets Manager Secrets
// AccessAnalyzer
accessAnalyzer := AccessAnalyzer{}
if IsNukeable(accessAnalyzer.ResourceName(), resourceTypes) {
start := time.Now()
analyzerNames, err := getAllAccessAnalyzers(cloudNukeSession, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve Access analyzers",
ResourceType: accessAnalyzer.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing Access Analyzers",
}, map[string]interface{}{
"region": region,
"recordCount": len(analyzerNames),
"actionTime": time.Since(start).Seconds(),
})
if len(analyzerNames) > 0 {
accessAnalyzer.AnalyzerNames = awsgo.StringValueSlice(analyzerNames)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, accessAnalyzer)
}
}
// End AccessAnalyzer
// CloudWatchDashboard
cloudwatchDashboards := CloudWatchDashboards{}
if IsNukeable(cloudwatchDashboards.ResourceName(), resourceTypes) {
start := time.Now()
cwdbNames, err := getAllCloudWatchDashboards(cloudNukeSession, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve CloudWatch dashboards",
ResourceType: cloudwatchDashboards.ResourceName(),
}
report.RecordError(ge)
}
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Done Listing Cloudwatch Dashboards",
}, map[string]interface{}{
"region": region,
"recordCount": len(cwdbNames),
"actionTime": time.Since(start).Seconds(),
})
if len(cwdbNames) > 0 {
cloudwatchDashboards.DashboardNames = awsgo.StringValueSlice(cwdbNames)
resourcesInRegion.Resources = append(resourcesInRegion.Resources, cloudwatchDashboards)
}
}
// End CloudWatchDashboard
// CloudWatchLogGroup
cloudwatchLogGroups := CloudWatchLogGroups{}
if IsNukeable(cloudwatchLogGroups.ResourceName(), resourceTypes) {
start := time.Now()
lgNames, err := getAllCloudWatchLogGroups(cloudNukeSession, excludeAfter, configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Description: "Unable to retrieve CloudWatch log groups",
ResourceType: cloudwatchLogGroups.ResourceName(),
}