ruby/example_code/ec2/ec2-ruby-example-manage-instances.rb
def list_instances_information(ec2_client)
  result = ec2_client.describe_instances
  result.reservations.each do |reservation|
    if reservation.instances.count.positive?
      reservation.instances.each do |instance|
        puts '-' * 12
        puts "Instance ID:               #{instance.instance_id}"
        puts "State:                     #{instance.state.name}"
        puts "Image ID:                  #{instance.image_id}"
        puts "Instance type:             #{instance.instance_type}"
        puts "Architecture:              #{instance.architecture}"
        puts "IAM instance profile ARN:  #{instance.iam_instance_profile.arn}"
        puts "Key name:                  #{instance.key_name}"
        puts "Launch time:               #{instance.launch_time}"
        puts "Detailed monitoring state: #{instance.monitoring.state}"
        puts "Public IP address:         #{instance.public_ip_address}"
        puts "Public DNS name:           #{instance.public_dns_name}"
        puts "VPC ID:                    #{instance.vpc_id}"
        puts "Subnet ID:                 #{instance.subnet_id}"
        if instance.tags.count.positive?
          puts 'Tags:'
          instance.tags.each do |tag|
            puts "                           #{tag.key}/#{tag.value}"
          end
        end
      end
    end
  end
end
# Displays information about available
# Amazon Elastic Compute Cloud (Amazon EC2) instances.
#
# @param ec2_client [Aws::EC2::Client] An initialized EC2 client.
# @example
#   list_instances_information(Aws::EC2::Client.new(region: 'us-west-2'))

Displays information about available
Amazon Elastic Compute Cloud (Amazon EC2) instances.
Displays information about available Amazon Elastic Compute Cloud ( Amazon EC2 ) instances .
def list_instances_information(ec2_client)
  result = ec2_client.describe_instances
  result.reservations.each do |reservation|
    if reservation.instances.count.positive?
      reservation.instances.each do |instance|
        puts '-' * 12
        puts "Instance ID:               #{instance.instance_id}"
        puts "State:                     #{instance.state.name}"
        puts "Image ID:                  #{instance.image_id}"
        puts "Instance type:             #{instance.instance_type}"
        puts "Architecture:              #{instance.architecture}"
        puts "IAM instance profile ARN:  #{instance.iam_instance_profile.arn}"
        puts "Key name:                  #{instance.key_name}"
        puts "Launch time:               #{instance.launch_time}"
        puts "Detailed monitoring state: #{instance.monitoring.state}"
        puts "Public IP address:         #{instance.public_ip_address}"
        puts "Public DNS name:           #{instance.public_dns_name}"
        puts "VPC ID:                    #{instance.vpc_id}"
        puts "Subnet ID:                 #{instance.subnet_id}"
        if instance.tags.count.positive?
          puts 'Tags:'
          instance.tags.each do |tag|
            puts "                           #{tag.key}/#{tag.value}"
          end
        end
      end
    end
  end
end
def list_instances_information ( ec2_client ) result = ec2_client . describe_instances result . reservations . each do | reservation | if reservation . instances . count . positive? reservation . instances . each do | instance | puts '-' * 12 puts "Instance ID:               #{instance.instance_id}" puts "State:                     #{instance.state.name}" puts "Image ID:                  #{instance.image_id}" puts "Instance type:             #{instance.instance_type}" puts "Architecture:              #{instance.architecture}" puts "IAM instance profile ARN:  #{instance.iam_instance_profile.arn}" puts "Key name:                  #{instance.key_name}" puts "Launch time:               #{instance.launch_time}" puts "Detailed monitoring state: #{instance.monitoring.state}" puts "Public IP address:         #{instance.public_ip_address}" puts "Public DNS name:           #{instance.public_dns_name}" puts "VPC ID:                    #{instance.vpc_id}" puts "Subnet ID:                 #{instance.subnet_id}" if instance . tags . count . positive? puts 'Tags:' instance . tags . each do | tag | puts "                           #{tag.key}/#{tag.value}" end end end end end end
Displays information about available
Amazon Elastic Compute Cloud (Amazon EC2) instances.
Displays information about available Amazon Elastic Compute Cloud ( Amazon EC2 ) instances .
ec2_client
An initialized EC2 client.
An initialized EC2 client .
example
None