Skip to content

Instantly share code, notes, and snippets.

@bmhatfield
Created March 9, 2016 04:15
Show Gist options
  • Save bmhatfield/4c9eed784a03d495b6e2 to your computer and use it in GitHub Desktop.
Save bmhatfield/4c9eed784a03d495b6e2 to your computer and use it in GitHub Desktop.
Output a human-readable & colorized view of your EC2 security group rules
#!/usr/bin/env ruby
require 'aws-sdk'
require 'colorize'
ec2 = Aws::EC2::Resource.new
ec2.security_groups.sort_by{|s| s.group_name }.each do |sg|
puts sg.group_name.underline unless sg.ip_permissions.empty?
sg.ip_permissions.each do |perm|
ip_ranges = perm.ip_ranges.map{ |r| r.cidr_ip == '0.0.0.0/0' ? r.cidr_ip.red : r.cidr_ip }.join(", ")
groups = perm.user_id_group_pairs.map{ |r| r.group_name}.join(", ")
puts " #{perm.ip_protocol}/#{perm.from_port} -> instance:#{perm.to_port}".yellow
puts " #{groups}" unless groups.empty?
puts " #{ip_ranges}" unless ip_ranges.empty?
puts
end
end
@bmhatfield
Copy link
Author

An example:

screen shot 2016-03-08 at 11 19 14 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment