Skip to content

Instantly share code, notes, and snippets.

@mrowe
Last active August 29, 2015 14:14
Show Gist options
  • Save mrowe/6872a69e6aa5cd9dc3c0 to your computer and use it in GitHub Desktop.
Save mrowe/6872a69e6aa5cd9dc3c0 to your computer and use it in GitHub Desktop.
Find unused security groups
#!/usr/bin/env ruby
require 'aws-sdk'
ec2 = AWS::EC2.new
elb = AWS::ELB.new
rds = AWS::RDS.new.client # shrug
security_groups = ec2.security_groups.map { |sg| sg.id }.sort
instance_sgs = ec2.instances.map { |i| i.security_groups.map { |sg| sg.id } }.flatten.sort
elb_sgs = elb.load_balancers.map { |elb| elb.security_groups.map { |sg| sg.id } }.flatten.sort
rds_sgs = rds.describe_db_instances.db_instances.map { |db| db.vpc_security_groups.map { |sg| sg.vpc_security_group_id } }.flatten.sort
puts security_groups - instance_sgs - elb_sgs - rds_sgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment