如何使用v2 ruby sdk将IP地址分配给ec2实例

问题描述 投票:0回答:1

如何使用v2 ruby​​ sdk将IP地址分配给ec2实例创建实例后。

我尝试过

ec2.associate_address()

方法(在http://docs.aws.amazon.com/sdk-for-ruby/v2/developer-guide/ec2-using-elastic-ip-addresses.html中提及),但这似乎是v1方法

ruby amazon-web-services amazon-ec2 sdk elastic-ip
1个回答
0
投票

这是aws Ruby SDK v1中Ruby中的代码。我正在获取弹性IP的分配ID,并将其传递给实例对象的associate_elastic_ip方法。

require 'aws-sdk-v1'
require 'json'

temp_ec2 = AWS::EC2.new
instance = temp_ec2.instances["i-8392ld62828f0c97"]
elasticIp = AWS::EC2::ElasticIp.new("49.234.122.12")
if elasticIp.exists?
           begin
                  instance.associate_elastic_ip(elasticIp.allocation_id)
              puts "[Success]EIP has been associated successfully...."
           rescue Exception => e
                  puts "There is an exception in allocating elastic ip and the exception is..#{e}"
           end
else
           puts "[Failed]EIP doesn't exist in the account."
end
© www.soinside.com 2019 - 2024. All rights reserved.