Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

bmhatfield commented Mar 9, 2016

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
You can’t perform that action at this time.