Skip to content

Instantly share code, notes, and snippets.

@mizzy
Created December 22, 2021 00:58
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 mizzy/912923846d5fdc2a380033f7ba3ef42b to your computer and use it in GitHub Desktop.
Save mizzy/912923846d5fdc2a380033f7ba3ef42b to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
require 'aws-sdk-ec2'
ec2 = Aws::EC2::Client.new
res = ec2.describe_security_groups
security_groups = res.security_groups
puts "### Number of Security Groups of the Region"
puts
puts security_groups.size
puts
puts "---"
puts
puts "### Number of Incoming Security Group Rules per Security Group"
puts
puts " Security Group | Number of Rules"
puts "----------|-------:"
security_groups.each do |sg|
count = 0
sg.ip_permissions.each do |p|
count += p.ip_ranges.size
end
puts "#{sg.group_id} | #{count}"
end
puts
puts "---"
puts
puts "### Number of Security Groups per ENI"
puts
res = ec2.describe_network_interfaces
network_interfaces = res.network_interfaces
puts "ENI | Number of SGs"
puts "--- | ------------:"
network_interfaces.each do |ni|
puts "#{ni.network_interface_id} | #{ni.groups.size}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment