ruby/example_code/ec2/ec2-ruby-example-security-group.rb
def describe_security_groups(ec2_client)
  response = ec2_client.describe_security_groups

  if response.security_groups.count.positive?
    response.security_groups.each do |sg|
      puts '-' * (sg.group_name.length + 13)
      puts "Name:        #{sg.group_name}"
      puts "Description: #{sg.description}"
      puts "Group ID:    #{sg.group_id}"
      puts "Owner ID:    #{sg.owner_id}"
      puts "VPC ID:      #{sg.vpc_id}"

      if sg.tags.count.positive?
        puts 'Tags:'
        sg.tags.each do |tag|
          puts "  Key: #{tag.key}, Value: #{tag.value}"
        end
      end

      unless sg.ip_permissions.empty?
        puts 'Inbound rules:' if sg.ip_permissions.count.positive?
        sg.ip_permissions.each do |p|
          describe_security_group_permissions(p)
        end
      end

      unless sg.ip_permissions_egress.empty?
        puts 'Outbound rules:' if sg.ip_permissions.count.positive?
        sg.ip_permissions_egress.each do |p|
          describe_security_group_permissions(p)
        end
      end
    end
  else
    puts 'No security groups found.'
  end
rescue StandardError => e
  puts "Error getting information about security groups: #{e.message}"
end
# Displays information about available security groups in
# Amazon Elastic Compute Cloud (Amazon EC2).
#
# @param ec2_client [Aws::EC2::Client] An initialized Amazon EC2 client.
# @example
#   describe_security_groups(Aws::EC2::Client.new(region: 'us-west-2'))

Displays information about available security groups in
Amazon Elastic Compute Cloud (Amazon EC2).
Displays information about available security groups in Amazon Elastic Compute Cloud ( Amazon EC2 ) .
def describe_security_groups(ec2_client)
  response = ec2_client.describe_security_groups
  if response.security_groups.count.positive?
    response.security_groups.each do |sg|
      puts '-' * (sg.group_name.length + 13)
      puts "Name:        #{sg.group_name}"
      puts "Description: #{sg.description}"
      puts "Group ID:    #{sg.group_id}"
      puts "Owner ID:    #{sg.owner_id}"
      puts "VPC ID:      #{sg.vpc_id}"
      if sg.tags.count.positive?
        puts 'Tags:'
        sg.tags.each do |tag|
          puts "  Key: #{tag.key}, Value: #{tag.value}"
        end
      end
      unless sg.ip_permissions.empty?
        puts 'Inbound rules:' if sg.ip_permissions.count.positive?
        sg.ip_permissions.each do |p|
          describe_security_group_permissions(p)
        end
      end
      unless sg.ip_permissions_egress.empty?
        puts 'Outbound rules:' if sg.ip_permissions.count.positive?
        sg.ip_permissions_egress.each do |p|
          describe_security_group_permissions(p)
        end
      end
    end
  else
    puts 'No security groups found.'
  end
rescue StandardError => e
  puts "Error getting information about security groups: #{e.message}"
end
def describe_security_groups ( ec2_client ) response = ec2_client . describe_security_groups if response . security_groups . count . positive? response . security_groups . each do | sg | puts '-' * ( sg . group_name . length + 13 ) puts "Name:        #{sg.group_name}" puts "Description: #{sg.description}" puts "Group ID:    #{sg.group_id}" puts "Owner ID:    #{sg.owner_id}" puts "VPC ID:      #{sg.vpc_id}" if sg . tags . count . positive? puts 'Tags:' sg . tags . each do | tag | puts "  Key: #{tag.key}, Value: #{tag.value}" end end unless sg . ip_permissions . empty? puts 'Inbound rules:' if sg . ip_permissions . count . positive? sg . ip_permissions . each do | p | describe_security_group_permissions ( p ) end end unless sg . ip_permissions_egress . empty? puts 'Outbound rules:' if sg . ip_permissions . count . positive? sg . ip_permissions_egress . each do | p | describe_security_group_permissions ( p ) end end end else puts 'No security groups found.' end rescue StandardError => e puts "Error getting information about security groups: #{e.message}" end
Displays information about available security groups in
Amazon Elastic Compute Cloud (Amazon EC2).
Displays information about available security groups in Amazon Elastic Compute Cloud ( Amazon EC2 ) .
ec2_client
An initialized Amazon EC2 client.
An initialized Amazon EC2 client .
example
None