0

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

我试过了

ec2.associate_address()

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

4

1 回答 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
于 2020-02-10T06:52:47.940 回答