Skip to content

Instantly share code, notes, and snippets.

@mikecarroll
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikecarroll/aa387aed66ad21b00e2b to your computer and use it in GitHub Desktop.
Save mikecarroll/aa387aed66ad21b00e2b to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
##########################################
#
# This script is to help you get around the dreaded 'Resource Has A Dependent Object'
# when deleting something in AWS. It will search a handful of places for that sg ID
# and let you know if it finds anything. Its a pretty quick n dirty script.
#
# If I missed any areas it should search, please let me know or better yet, make a pull request!
#
# You'll need to install the aws-sdk ruby gem: sudo gem install aws-sdk
# Then edit below the 3 items.
#
##########################################
########## Configure Here ##########
@mySg = "sg-11111111" ## security group ID to look for
aws_key = "xxxNOTREALLYMYKEYXXX" ## your aws key
aws_sec = "xxxPUTYOURAWSSECRETINHERENOWDOITNOW!xxxx" ## your aws secret key
########## Done configuring ##########
########## Start Script ##########
require 'rubygems'
begin
require 'aws-sdk'
rescue LoadError
puts "You dont have aws-sdk installed. Make sure to install it: sudo gem install aws-sdk"
end
begin
require 'pp' ## only used for debugging
rescue LoadError
end
## create ec2 client
AWS.config(
:access_key_id => aws_key,
:secret_access_key => aws_sec
)
@ec2 = AWS::EC2.new
@elb = AWS::ELB.new
puts "Searching security groups"
@ec2.security_groups.each do |sgs|
sgs.egress_ip_permissions.each do |perm|
# pp perm.groups.find{|sg| sg.id == @mySg}
perm.groups.each do |sg|
puts "Other Security Group: #{sgs.id}" if sg.id == @mySg
end
end
sgs.ingress_ip_permissions.each do |perm|
# pp perm.groups.find{|sg| sg.id == @mySg}
perm.groups.each do |sg|
puts "Other Security Group: #{sgs.id}" if sg.id == @mySg
end
end
end
exit
puts "Searching instances"
@ec2.instances.each do |instance|
instance.security_groups.each do |sg|
puts "Instance: #{instance.id}" if sg.id == @mySg
end
end
puts "Searching network interfaces"
@ec2.network_interfaces.each do |nic|
nic.security_groups.each do |sg|
puts "Network Interface: #{nic.id}" if sg.id == @mySg
end
end
puts "Searching ELBs"
@elb.load_balancers.each do |lb|
lb.security_groups.each do |sg|
puts "ELB: #{lb.id}" if sg.id == @mySg
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment