简体   繁体   中英

how to assign an ip address to an ec2 instance using the v2 ruby sdk

how to assign an ip address to an ec2 instance using the v2 ruby sdk after the instance has been created.

I tried the

ec2.associate_address()

method (mentioned in http://docs.aws.amazon.com/sdk-for-ruby/v2/developer-guide/ec2-using-elastic-ip-addresses.html ) but that seems to be a v1 method

This is the code in Ruby with aws Ruby SDK v1. I am fetching allocation id of the Elastic IP and passing it to associate_elastic_ip method of instance object.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM