ruby/example_code/ec2/ec2-ruby-example-elastic-ips.rb
# Gets information about addresses associated with an
# Amazon Elastic Compute Cloud (Amazon EC2) instance.
#
# Prerequisites:
#
# - The Amazon EC2 instance.
#
# @param ec2_client [Aws::EC2::Client] An initialized EC2 client.
# @param instance_id [String] The ID of the instance.
# @example
#   describe_addresses_for_instance(
#     Aws::EC2::Client.new(region: 'us-west-2'),
#     'i-033c48ef067af3dEX'
#   )

Gets information about addresses associated with an
Amazon Elastic Compute Cloud (Amazon EC2) instance.



The Amazon EC2 instance.
Gets information about addresses associated with an Amazon Elastic Compute Cloud ( Amazon EC2 ) instance . The Amazon EC2 instance .
def describe_addresses_for_instance(ec2_client, instance_id)
  response = ec2_client.describe_addresses(
    filters: [
      {
        name: 'instance-id',
        values: [instance_id]
      }
    ]
  )
  addresses = response.addresses
  if addresses.count.zero?
    puts 'No addresses.'
  else
    addresses.each do |address|
      puts '-' * 20
      puts "Public IP:  #{address.public_ip}"
      puts "Private IP: #{address.private_ip_address}"
    end
  end
rescue StandardError => e
  puts "Error getting address information for instance: #{e.message}"
end
def describe_addresses_for_instance ( ec2_client , instance_id ) response = ec2_client . describe_addresses ( filters : [ { name : 'instance-id' , values : [ instance_id ] } ] ) addresses = response . addresses if addresses . count . zero? puts 'No addresses.' else addresses . each do | address | puts '-' * 20 puts "Public IP:  #{address.public_ip}" puts "Private IP: #{address.private_ip_address}" end end rescue StandardError => e puts "Error getting address information for instance: #{e.message}" end
Gets information about addresses associated with an
Amazon Elastic Compute Cloud (Amazon EC2) instance.
Gets information about addresses associated with an Amazon Elastic Compute Cloud ( Amazon EC2 ) instance .
ec2_client
An initialized EC2 client.
An initialized EC2 client .
instance_id
The ID of the instance.
The ID of the instance .
example
describe_addresses_for_instance( Aws::EC2::Client.new(region: 'us-west-2'), 'i-033c48ef067af3dEX' )
describe_addresses_for_instance ( Aws :: EC2 :: Client . new ( region : ' us - west - 2 ' ) ' i - 033c48ef067af3dEX ' )