forked from gruntwork-io/cloud-nuke
/
ec2_vpc.go
762 lines (669 loc) · 23.3 KB
/
ec2_vpc.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
package resources
import (
"context"
cerrors "errors"
"fmt"
"strings"
"time"
"github.com/andrewderr/cloud-nuke-a1/util"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/pterm/pterm"
"github.com/andrewderr/cloud-nuke-a1/telemetry"
commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"
"github.com/andrewderr/cloud-nuke-a1/config"
"github.com/andrewderr/cloud-nuke-a1/logging"
"github.com/andrewderr/cloud-nuke-a1/report"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gruntwork-io/go-commons/errors"
"github.com/hashicorp/go-multierror"
)
func (v *EC2VPCs) setFirstSeenTag(vpc ec2.Vpc, value time.Time) error {
// We set a first seen tag because an Elastic IP doesn't contain an attribute that gives us it's creation time
_, err := v.Client.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{vpc.VpcId},
Tags: []*ec2.Tag{
{
Key: awsgo.String(util.FirstSeenTagKey),
Value: awsgo.String(util.FormatTimestamp(value)),
},
},
})
if err != nil {
return errors.WithStackTrace(err)
}
return nil
}
func (v *EC2VPCs) getFirstSeenTag(vpc ec2.Vpc) (*time.Time, error) {
tags := vpc.Tags
for _, tag := range tags {
if util.IsFirstSeenTag(tag.Key) {
firstSeenTime, err := util.ParseTimestamp(tag.Value)
if err != nil {
return nil, errors.WithStackTrace(err)
}
return firstSeenTime, nil
}
}
return nil, nil
}
func (v *EC2VPCs) getAll(c context.Context, configObj config.Config) ([]*string, error) {
result, err := v.Client.DescribeVpcs(&ec2.DescribeVpcsInput{
Filters: []*ec2.Filter{
// Note: this filter omits the default since there is special
// handling for default resources already
{
Name: awsgo.String("is-default"),
Values: awsgo.StringSlice([]string{"false"}),
},
},
})
if err != nil {
return nil, errors.WithStackTrace(err)
}
var ids []*string
for _, vpc := range result.Vpcs {
firstSeenTime, err := v.getFirstSeenTag(*vpc)
if err != nil {
logging.Error("Unable to retrieve tags")
return nil, errors.WithStackTrace(err)
}
if firstSeenTime == nil {
now := time.Now().UTC()
firstSeenTime = &now
if err := v.setFirstSeenTag(*vpc, time.Now().UTC()); err != nil {
return nil, err
}
}
if configObj.VPC.ShouldInclude(config.ResourceValue{
Time: firstSeenTime,
Name: GetEC2ResourceNameTagValue(vpc.Tags),
}) {
ids = append(ids, vpc.VpcId)
}
}
return ids, nil
}
func (v *EC2VPCs) nukeAll(vpcIds []string) error {
if len(vpcIds) == 0 {
logging.Debug("No VPCs to nuke")
return nil
}
logging.Debug("Deleting all VPCs")
deletedVPCs := 0
multiErr := new(multierror.Error)
for _, id := range vpcIds {
var err error
err = nuke(v.Client, id)
// Record status of this resource
e := report.Entry{
Identifier: id,
ResourceType: "VPC",
Error: err,
}
report.Record(e)
if err != nil {
pterm.Error.Println(fmt.Sprintf("Failed to nuke vpc with err: %s", err))
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking VPC",
}, map[string]interface{}{
"region": v.Region,
})
multierror.Append(multiErr, err)
} else {
deletedVPCs++
logging.Debug(fmt.Sprintf("Deleted VPC: %s", id))
}
}
return nil
}
func nuke(client ec2iface.EC2API, vpcID string) error {
// Note: order is quite important, otherwise you will encounter dependency violation errors.
logging.Debug(fmt.Sprintf("Start nuking VPC %s", vpcID))
err := nukeDhcpOptions(client, vpcID)
if err != nil {
logging.Debug(fmt.Sprintf("Error cleaning up DHCP Options for VPC %s: %s", vpcID, err.Error()))
return err
}
err = nukeRouteTables(client, vpcID)
if err != nil {
logging.Debug(fmt.Sprintf("Error cleaning up Route Tables for VPC %s: %s", vpcID, err.Error()))
return err
}
err = nukeNetworkInterfaces(client, vpcID)
if err != nil {
pterm.Error.Println(fmt.Sprintf("Error cleaning up Elastic Network Interfaces for VPC %s: %s", vpcID, err.Error()))
return err
}
err = nukeNatGateway(client, vpcID)
if err != nil {
logging.Debug(fmt.Sprintf("Error cleaning up NAT Gateways for VPC %s: %s", vpcID, err.Error()))
return err
}
err = nukeEgressOnlyGateways(client, vpcID)
if err != nil {
pterm.Error.Println(fmt.Sprintf("Error cleaning up Egress Only Internet Gateways for VPC %s: %s", vpcID, err.Error()))
return err
}
err = nukeInternetGateway(client, vpcID)
if err != nil {
logging.Debug(fmt.Sprintf("Error cleaning up Internet Gateway for VPC %s: %s", vpcID, err.Error()))
return err
}
err = nukeNacls(client, vpcID)
if err != nil {
logging.Debug(fmt.Sprintf("Error cleaning up Network ACLs for VPC %s: %s", vpcID, err.Error()))
return err
}
err = nukeSubnets(client, vpcID)
if err != nil {
logging.Debug(fmt.Sprintf("Error cleaning up Subnets for VPC %s: %s", vpcID, err.Error()))
return err
}
err = nukeEndpoints(client, vpcID)
if err != nil {
logging.Debug(fmt.Sprintf("Error cleaning up Endpoints for VPC %s: %s", vpcID, err.Error()))
return err
}
err = nukeSecurityGroups(client, vpcID)
if err != nil {
logging.Debug(fmt.Sprintf("Error cleaning up Security Groups for VPC %s: %s", vpcID, err.Error()))
return err
}
err = nukeVpc(client, vpcID)
if err != nil {
pterm.Error.Println(fmt.Sprintf("Error deleting VPC %s: %s ", vpcID, err))
return err
}
logging.Debug(fmt.Sprintf("Successfully nuked VPC %s", vpcID))
logging.Debug("")
return nil
}
func nukeInternetGateway(client ec2iface.EC2API, vpcID string) error {
logging.Debug(fmt.Sprintf("Start nuking Internet Gateway for vpc: %s", vpcID))
input := &ec2.DescribeInternetGatewaysInput{
Filters: []*ec2.Filter{
{
Name: awsgo.String("attachment.vpc-id"),
Values: []*string{awsgo.String(vpcID)},
},
},
}
igw, err := client.DescribeInternetGateways(input)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to describe internet gateways for vpc: %s", vpcID))
return errors.WithStackTrace(err)
}
if len(igw.InternetGateways) < 1 {
logging.Debug(fmt.Sprintf("No Internet Gateway to delete."))
return nil
}
logging.Debug(fmt.Sprintf("Detaching Internet Gateway %s",
awsgo.StringValue(igw.InternetGateways[0].InternetGatewayId)))
_, err = client.DetachInternetGateway(
&ec2.DetachInternetGatewayInput{
InternetGatewayId: igw.InternetGateways[0].InternetGatewayId,
VpcId: awsgo.String(vpcID),
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to detach internet gateway %s",
awsgo.StringValue(igw.InternetGateways[0].InternetGatewayId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully detached internet gateway %s",
awsgo.StringValue(igw.InternetGateways[0].InternetGatewayId)))
logging.Debug(fmt.Sprintf("Deleting internet gateway %s",
awsgo.StringValue(igw.InternetGateways[0].InternetGatewayId)))
_, err = client.DeleteInternetGateway(
&ec2.DeleteInternetGatewayInput{
InternetGatewayId: igw.InternetGateways[0].InternetGatewayId,
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to delete internet gateway %s",
awsgo.StringValue(igw.InternetGateways[0].InternetGatewayId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully deleted internet gateway %s",
awsgo.StringValue(igw.InternetGateways[0].InternetGatewayId)))
return nil
}
func nukeEgressOnlyGateways(client ec2iface.EC2API, vpcID string) error {
var allEgressGateways []*string
logging.Debug(fmt.Sprintf("Start nuking Egress Only Internet Gateways for vpc: %s", vpcID))
err := client.DescribeEgressOnlyInternetGatewaysPages(
&ec2.DescribeEgressOnlyInternetGatewaysInput{},
func(page *ec2.DescribeEgressOnlyInternetGatewaysOutput, lastPage bool) bool {
for _, gateway := range page.EgressOnlyInternetGateways {
for _, attachment := range gateway.Attachments {
if *attachment.VpcId == vpcID {
allEgressGateways = append(allEgressGateways, gateway.EgressOnlyInternetGatewayId)
break
}
}
}
return !lastPage
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to describe Egress Only Internet Gateways for vpc: %s", vpcID))
return err
}
logging.Debug(fmt.Sprintf("Found %d Egress Only Internet Gateways to nuke.", len(allEgressGateways)))
var allErrs *multierror.Error
for _, gateway := range allEgressGateways {
_, err := client.DeleteEgressOnlyInternetGateway(
&ec2.DeleteEgressOnlyInternetGatewayInput{EgressOnlyInternetGatewayId: gateway})
if err != nil {
logging.Debug(fmt.Sprintf("Failed to delete Egress Only Internet Gateway %s", *gateway))
allErrs = multierror.Append(allErrs, errors.WithStackTrace(err))
}
logging.Debug(fmt.Sprintf("Successfully deleted Egress Only Internet Gateway %s", *gateway))
}
return nil
}
func nukeEndpoints(client ec2iface.EC2API, vpcID string) error {
logging.Debug(fmt.Sprintf("Start nuking VPC endpoints for vpc: %s", vpcID))
endpoints, _ := client.DescribeVpcEndpoints(
&ec2.DescribeVpcEndpointsInput{
Filters: []*ec2.Filter{
{
Name: awsgo.String("vpc-id"),
Values: []*string{awsgo.String(vpcID)},
},
},
},
)
logging.Debug(fmt.Sprintf("Found %d VPC endpoint.", len(endpoints.VpcEndpoints)))
var endpointIds []*string
for _, endpoint := range endpoints.VpcEndpoints {
// Note: sometime the state is all lower cased, sometime it is not.
if strings.ToLower(*endpoint.State) != strings.ToLower(ec2.StateAvailable) {
logging.Debug(fmt.Sprintf("Skipping VPC endpoint %s as it is not in available state: %s",
awsgo.StringValue(endpoint.VpcEndpointId), awsgo.StringValue(endpoint.State)))
continue
}
endpointIds = append(endpointIds, endpoint.VpcEndpointId)
}
if len(endpointIds) == 0 {
logging.Debug(fmt.Sprintf("No VPC endpoint to nuke."))
return nil
}
_, err := client.DeleteVpcEndpoints(&ec2.DeleteVpcEndpointsInput{
VpcEndpointIds: endpointIds,
})
if err != nil {
logging.Debug(fmt.Sprintf("Failed to delete VPC endpoints: %s", err.Error()))
return errors.WithStackTrace(err)
}
if err := waitForVPCEndpointToBeDeleted(client, vpcID); err != nil {
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully deleted VPC endpoints"))
return nil
}
func waitForVPCEndpointToBeDeleted(client ec2iface.EC2API, vpcID string) error {
for i := 0; i < 30; i++ {
endpoints, err := client.DescribeVpcEndpoints(
&ec2.DescribeVpcEndpointsInput{
Filters: []*ec2.Filter{
{
Name: awsgo.String("vpc-id"),
Values: []*string{awsgo.String(vpcID)},
},
{
Name: awsgo.String("vpc-endpoint-state"),
Values: []*string{awsgo.String("deleting")},
},
},
},
)
if err != nil {
return err
}
if len(endpoints.VpcEndpoints) == 0 {
return nil
}
time.Sleep(20 * time.Second)
logging.Debug(fmt.Sprintf("Waiting for VPC endpoints to be deleted..."))
}
return nil
}
func nukeNetworkInterfaces(client ec2iface.EC2API, vpcID string) error {
logging.Debug(fmt.Sprintf("Start nuking network interfaces for vpc: %s", vpcID))
var allNetworkInterfaces []*ec2.NetworkInterface
vpcIds := []string{vpcID}
filters := []*ec2.Filter{{Name: awsgo.String("vpc-id"), Values: awsgo.StringSlice(vpcIds)}}
err := client.DescribeNetworkInterfacesPages(
&ec2.DescribeNetworkInterfacesInput{
Filters: filters,
},
func(page *ec2.DescribeNetworkInterfacesOutput, lastPage bool) bool {
for _, netInterface := range page.NetworkInterfaces {
allNetworkInterfaces = append(allNetworkInterfaces, netInterface)
}
return !lastPage
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to describe network interfaces for vpc: %s", vpcID))
return err
}
logging.Debug(fmt.Sprintf("Found %d Elastic Network Interfaces to Nuke.", len(allNetworkInterfaces)))
for _, netInterface := range allNetworkInterfaces {
if strings.ToLower(*netInterface.Status) == strings.ToLower(ec2.NetworkInterfaceStatusInUse) {
logging.Debug(fmt.Sprintf("Skipping network interface %s as it is in use",
*netInterface.NetworkInterfaceId))
continue
}
_, err = client.DeleteNetworkInterface(&ec2.DeleteNetworkInterfaceInput{
NetworkInterfaceId: netInterface.NetworkInterfaceId,
})
if err != nil {
logging.Debug(fmt.Sprintf("Failed to delete network interface: %s", *netInterface))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully deleted network interface: %s", *netInterface.NetworkInterfaceId))
}
return nil
}
func nukeSubnets(client ec2iface.EC2API, vpcID string) error {
logging.Debug(fmt.Sprintf("Start nuking subnets for vpc: %s", vpcID))
subnets, _ := client.DescribeSubnets(
&ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: awsgo.String("vpc-id"),
Values: []*string{awsgo.String(vpcID)},
},
},
},
)
logging.Debug(fmt.Sprintf("Found %d subnets to delete ", len(subnets.Subnets)))
if len(subnets.Subnets) > 0 {
for _, subnet := range subnets.Subnets {
_, err := client.DeleteSubnet(
&ec2.DeleteSubnetInput{
SubnetId: subnet.SubnetId,
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to delete subnet %s", awsgo.StringValue(subnet.SubnetId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully deleted subnet %s", awsgo.StringValue(subnet.SubnetId)))
}
return nil
}
logging.Debug(fmt.Sprintf("No subnets found"))
return nil
}
func nukeNatGateway(client ec2iface.EC2API, vpcID string) error {
gateways, err := client.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{
Filter: []*ec2.Filter{
{
Name: awsgo.String("vpc-id"),
Values: []*string{awsgo.String(vpcID)},
},
},
})
if err != nil {
logging.Debug(fmt.Sprintf("Failed to describe NAT gateways for vpc: %s", vpcID))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Found %d NAT gateways to delete", len(gateways.NatGateways)))
for _, gateway := range gateways.NatGateways {
if *gateway.State != ec2.NatGatewayStateAvailable {
logging.Debug(fmt.Sprintf("Skipping NAT gateway %s as it is not in available state: %s",
awsgo.StringValue(gateway.NatGatewayId), awsgo.StringValue(gateway.State)))
continue
}
_, err := client.DeleteNatGateway(&ec2.DeleteNatGatewayInput{
NatGatewayId: gateway.NatGatewayId,
})
if err != nil {
logging.Debug(
fmt.Sprintf("Failed to delete NAT gateway %s", awsgo.StringValue(gateway.NatGatewayId)))
return errors.WithStackTrace(err)
}
logging.Debug(
fmt.Sprintf("Successfully deleted NAT gateway %s", awsgo.StringValue(gateway.NatGatewayId)))
}
return nil
}
func nukeRouteTables(client ec2iface.EC2API, vpcID string) error {
logging.Debug(fmt.Sprintf("Start nuking route tables for vpc: %s", vpcID))
routeTables, err := client.DescribeRouteTables(
&ec2.DescribeRouteTablesInput{
Filters: []*ec2.Filter{
{
Name: awsgo.String("vpc-id"),
Values: []*string{awsgo.String(vpcID)},
},
},
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to describe route tables for vpc: %s", vpcID))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Found %d route tables.", len(routeTables.RouteTables)))
for _, routeTable := range routeTables.RouteTables {
// Skip main route table
if len(routeTable.Associations) > 0 && *routeTable.Associations[0].Main {
logging.Debug(fmt.Sprintf("Skipping main route table: %s",
awsgo.StringValue(routeTable.RouteTableId)))
continue
}
logging.Debug(fmt.Sprintf("Start nuking route table: %s", awsgo.StringValue(routeTable.RouteTableId)))
for _, association := range routeTable.Associations {
_, err := client.DisassociateRouteTable(&ec2.DisassociateRouteTableInput{
AssociationId: association.RouteTableAssociationId,
})
if err != nil {
logging.Debug(fmt.Sprintf("Failed to disassociate route table: %s",
awsgo.StringValue(association.RouteTableAssociationId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully disassociated route table: %s",
awsgo.StringValue(association.RouteTableAssociationId)))
}
_, err := client.DeleteRouteTable(
&ec2.DeleteRouteTableInput{
RouteTableId: routeTable.RouteTableId,
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to delete route table: %s", awsgo.StringValue(routeTable.RouteTableId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully deleted route table: %s", awsgo.StringValue(routeTable.RouteTableId)))
}
return nil
}
// nukeNacls nukes all network ACLs in a VPC except the default one. It replaces all subnet associations with
// the default in order to prevent dependency violations.
func nukeNacls(client ec2iface.EC2API, vpcID string) error {
logging.Debug(fmt.Sprintf("Start nuking network ACLs for vpc: %s", vpcID))
networkACLs, _ := client.DescribeNetworkAcls(
&ec2.DescribeNetworkAclsInput{
Filters: []*ec2.Filter{
{
Name: awsgo.String("vpc-id"),
Values: []*string{awsgo.String(vpcID)},
},
},
},
)
// Find the default network ACL to connect all subnets to.
var defaultNetworkAclID *string
for _, networkACL := range networkACLs.NetworkAcls {
if *networkACL.IsDefault {
defaultNetworkAclID = networkACL.NetworkAclId
break
}
}
if defaultNetworkAclID == nil {
logging.Debug(fmt.Sprintf("No default network ACL found"))
return cerrors.New("No default network ACL found in vpc: " + vpcID)
}
logging.Debug(fmt.Sprintf("Found default network ACL: %s", *defaultNetworkAclID))
logging.Debug(fmt.Sprintf("Found %d network ACLs to delete", len(networkACLs.NetworkAcls)-1))
for _, networkACL := range networkACLs.NetworkAcls {
if *networkACL.IsDefault {
continue
}
logging.Debug(fmt.Sprintf(
"Start nuking network ACL: %s", awsgo.StringValue(networkACL.NetworkAclId)))
for _, association := range networkACL.Associations {
logging.Debug(fmt.Sprintf(
"Found %d network ACL associations to replace", len(networkACL.Associations)))
_, err := client.ReplaceNetworkAclAssociation(&ec2.ReplaceNetworkAclAssociationInput{
AssociationId: association.NetworkAclAssociationId,
NetworkAclId: defaultNetworkAclID,
})
if err != nil {
logging.Debug(fmt.Sprintf("Failed to replace network ACL association: %s to default",
awsgo.StringValue(association.NetworkAclAssociationId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully replaced network ACL association: %s to default",
awsgo.StringValue(association.NetworkAclAssociationId)))
}
_, err := client.DeleteNetworkAcl(
&ec2.DeleteNetworkAclInput{
NetworkAclId: networkACL.NetworkAclId,
},
)
if err != nil {
logging.Debug(fmt.Sprintf(
"Failed to delete network ACL: %s", awsgo.StringValue(networkACL.NetworkAclId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf(
"Successfully deleted network ACL: %s", awsgo.StringValue(networkACL.NetworkAclId)))
}
return nil
}
func nukeSecurityGroups(client ec2iface.EC2API, vpcID string) error {
logging.Debug(fmt.Sprintf("Start nuking security groups for vpc: %s", vpcID))
securityGroups, _ := client.DescribeSecurityGroups(
&ec2.DescribeSecurityGroupsInput{
Filters: []*ec2.Filter{
{
Name: awsgo.String("vpc-id"),
Values: []*string{awsgo.String(vpcID)},
},
},
},
)
logging.Debug(fmt.Sprintf("Found %d security groups to delete", len(securityGroups.SecurityGroups)))
for _, securityGroup := range securityGroups.SecurityGroups {
securityGroupRules, _ := client.DescribeSecurityGroupRules(
&ec2.DescribeSecurityGroupRulesInput{
Filters: []*ec2.Filter{
{
Name: awsgo.String("group-id"),
Values: []*string{securityGroup.GroupId},
},
},
},
)
logging.Debug(fmt.Sprintf("Found %d security rules to delete for security group: %s",
len(securityGroupRules.SecurityGroupRules), awsgo.StringValue(securityGroup.GroupId)))
for _, securityGroupRule := range securityGroupRules.SecurityGroupRules {
logging.Debug(fmt.Sprintf("Deleting Security Group Rule %s", awsgo.StringValue(securityGroupRule.SecurityGroupRuleId)))
if *securityGroupRule.IsEgress {
_, err := client.RevokeSecurityGroupEgress(
&ec2.RevokeSecurityGroupEgressInput{
GroupId: securityGroup.GroupId,
SecurityGroupRuleIds: []*string{securityGroupRule.SecurityGroupRuleId},
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to revoke security group egress rule: %s",
awsgo.StringValue(securityGroupRule.SecurityGroupRuleId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully revoked security group egress rule: %s",
awsgo.StringValue(securityGroupRule.SecurityGroupRuleId)))
} else {
_, err := client.RevokeSecurityGroupIngress(
&ec2.RevokeSecurityGroupIngressInput{
GroupId: securityGroup.GroupId,
SecurityGroupRuleIds: []*string{securityGroupRule.SecurityGroupRuleId},
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to revoke security group ingress rule: %s",
awsgo.StringValue(securityGroupRule.SecurityGroupRuleId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully revoked security group ingress rule: %s",
awsgo.StringValue(securityGroupRule.SecurityGroupRuleId)))
}
}
}
for _, securityGroup := range securityGroups.SecurityGroups {
if *securityGroup.GroupName != "default" {
_, err := client.DeleteSecurityGroup(
&ec2.DeleteSecurityGroupInput{
GroupId: securityGroup.GroupId,
},
)
if err != nil {
logging.Debug(fmt.Sprintf(
"Successfully deleted security group %s", awsgo.StringValue(securityGroup.GroupId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf(
"Successfully deleted security group %s", awsgo.StringValue(securityGroup.GroupId)))
}
}
return nil
}
func nukeDhcpOptions(client ec2iface.EC2API, vpcID string) error {
// Deletes the specified set of DHCP options. You must disassociate the set of DHCP options before you can delete it.
// You can disassociate the set of DHCP options by associating either a new set of options or the default set of
// options with the VPC.
vpcs, err := client.DescribeVpcs(
&ec2.DescribeVpcsInput{
VpcIds: []*string{awsgo.String(vpcID)},
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to describe DHCP options associated with VPC w/ err: %s.", err))
return errors.WithStackTrace(err)
}
dhcpOptionID := vpcs.Vpcs[0].DhcpOptionsId
logging.Debug(fmt.Sprintf("Successfully found DHCP option associated with VPC: %s.", *dhcpOptionID))
if *dhcpOptionID == "default" {
logging.Debug(fmt.Sprintf("No DHCP options to nuke. DHCP already set to default."))
return nil
}
// Disassociates a set of DHCP options from a VPC by setting the options to default.
_, err = client.AssociateDhcpOptions(&ec2.AssociateDhcpOptionsInput{
DhcpOptionsId: awsgo.String("default"),
VpcId: awsgo.String(vpcID),
})
if err != nil {
logging.Debug(fmt.Sprintf("Failed to associate VPC with default set of DHCP options w/ err: %s.", err))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully associated VPC with default set of DHCP options."))
return err
}
func nukeVpc(client ec2iface.EC2API, vpcID string) error {
logging.Debug(fmt.Sprintf("Deleting VPC %s", vpcID))
input := &ec2.DeleteVpcInput{
VpcId: awsgo.String(vpcID),
}
_, err := client.DeleteVpc(input)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to delete VPC %s", vpcID))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully deleted VPC %s", vpcID))
return nil
}