【问题标题】:how to assign an ip address to an ec2 instance using the v2 ruby sdk如何使用 v2 ruby​​ sdk 为 ec2 实例分配 IP 地址
【发布时间】:2017-12-08 12:06:34
【问题描述】:

如何使用 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 方法

【问题讨论】:

  • v2 ruby​​ sdk 的变化是您构建客户端的方式,例如 ec2=Aws::EC2::Client.new( access_key_id: creds['access_key_id'], secret_access_key: creds['secret_access_key'] )
  • 您在使用该方法时遇到了什么问题?你也分配了弹性ip吗?

标签: ruby amazon-web-services amazon-ec2 sdk elastic-ip


【解决方案1】:

这是带有 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

【讨论】:

    猜你喜欢
    • 2021-04-19
    • 1970-01-01
    • 2014-03-19
    • 2022-07-29
    • 2021-01-20
    • 1970-01-01
    • 2015-06-07
    • 2014-09-21
    • 1970-01-01
    相关资源
    最近更新 更多