-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_nuker.rb
executable file
·166 lines (150 loc) · 4.68 KB
/
aws_nuker.rb
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
#!/usr/bin/env ruby
require 'optparse'
require 'aws-sdk-ec2'
require 'aws-sdk-s3'
class AWSNuker
def initialize
parse
verify_args
end
# Public methods
def run
puts "#{"="*20}DRY RUN STARTED" if @options[:dry_run]
case @options[:service]
when "ec2"
ec2_termination
when "s3"
s3_termination
else
raise ArgumentError.new("#{@options[:service]} is not a supported service.")
end
end
# Private methods
private def ec2_termination
# show what will be deleted
ec2 = Aws::EC2::Client.new(profile: @options[:profile], region: @options[:region])
resp = ec2.describe_instances({
filters: [
{
name: "instance-state-name",
values: ["running", "stopped", "stopping", "pending"],
},
],
})
puts "* The following instances will be deleted =>"
instances = resp.reservations.map(&:instances).flatten(1)
instances.each do |instance|
puts "#{instance.instance_id.ljust(20)} #{instance.instance_type.ljust(20)} #{instance.state.name}"
end
# exit here if desired
early_exit('instance')
# Turn off instance termination protection if enabled
protected_instances = instances.filter do |instance|
ec2.describe_instance_attribute({attribute: "disableApiTermination", instance_id: instance.instance_id }).disable_api_termination
end
protected_instances.each do |instance|
ec2.modify_instance_attribute({
instance_id: instance.instance_id,
disable_api_termination: {
value: false,
}
})
end
ec2.terminate_instances({
instance_ids: instances.map(&:instance_id)
})
puts "\nAll done!"
end
private def s3_termination
# show what will be deleted
s3 = Aws::S3::Client.new(profile: @options[:profile], region: @options[:region])
resp = s3.list_buckets
puts "* The following buckets will be deleted =>"
buckets = resp.buckets
buckets.each do |bucket|
puts "#{bucket.name}"
end
# exit here if desired
early_exit('bucket')
buckets.each do |bucket|
begin
resp = s3.delete_bucket({ bucket: bucket.name })
rescue Aws::S3::Errors::BucketNotEmpty
puts "FAILURE: Bucket '#{bucket.name}' is not empty. Setting a lifecycle policy to destroy all objects inside the bucket by tomorrow."
puts "\tPlease run the script again tomorrow."
resp = s3.put_bucket_lifecycle_configuration({
bucket: bucket.name,
lifecycle_configuration: {
rules: [
{
expiration: {
days: 1,
},
prefix: nil,
filter: {
prefix: "",
},
id: "DeleteAll",
status: "Enabled",
noncurrent_version_expiration: {
noncurrent_days: 1,
},
abort_incomplete_multipart_upload: {
days_after_initiation: 1,
},
},
],
},
})
rescue Aws::S3::Errors::ServiceError => e
puts "ERROR: #{e}"
puts "\tAttempting to delete bucket '#{bucket.name}' errored."
puts "\tTry deleting it manually."
else
puts "SUCCESS: Bucket '#{bucket.name}' got deleted successfully."
end
end
end
private def early_exit(service_obj)
puts
service_name = service_obj
# exit here if desired
if @options[:dry_run]
puts "#{"="*20}DRY RUN ENDED"
exit 0
end
puts "Are you sure that you want to terminate/delete all #{service_name}s above? (YES/NO)"
if gets.chomp.upcase != 'YES'
puts "Exiting..."
exit 0
end
end
private def verify_args
required_options = [:profile, :region, :service]
all_options_exist = required_options.inject(true) {|bool, curr| bool && @options.has_key?(curr) }
raise ArgumentError.new("All of #{required_options} are required.") unless all_options_exist
end
private def parse
@options = {}
OptionParser.new do |opts|
opts.banner = "Usage: aws_nuker.rb [options]"
opts.on("-p", "--profile PROFILE", "AWS profile name") do |o|
@options[:profile] = o
end
opts.on("-r", "--region REGION", "AWS region") do |o|
@options[:region] = o
end
opts.on("-s", "--service SERVICE", "AWS service name. Supported services are: ['ec2', 's3']") do |o|
@options[:service] = o
end
opts.on("-d", "--[no-]dry-run", "Dry run") do |o|
@options[:dry_run] = o
end
opts.on("-h", "--help", "Print this help") do
puts opts
exit
end
end.parse!
end
end
AWSNuker.new.run