Vtanathip Vtanathip - 3 months ago 18
Ruby Question

How to get public ip address from AWS with Ruby SDK

I'm very newbie with AWS.

I would like to get the public IP address from Ruby SDK but can't find how to get it.


require 'aws-sdk'

ec2 = Aws::EC2::Resource.new(region: 'us-west-2')

i = ec2.instance('i-XXXXXX')


with this two command, I only get state and code.

How to get the public IP address since it usually changes when we shutdown the instance??

Thank in advance

Answer

Once you have your Instance object, in your case the i variable, use the public_ip_address attribute to reference the public IP address of your EC2 instance.

Example:

require 'aws-sdk'
ec2 = Aws::EC2::Resource.new(region: 'us-west-2')
i = ec2.instance('i-XXXXXX')
publicIp = i.public_ip_address

Docs: http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Instance.html#public_ip_address-instance_method

Comments