我想要一个快速而又脏的停止和启动实例,所以我写了下面的内容,这完全得到来自describe_instances的bizzare答案,并显示我的实例在它实际上已停止并且不会停止或启动任何内容时运行。
任何人都有指向我明显错误的指针吗?
#!/bin/ruby
require 'rubygems'
require 'bundler/setup'
require 'aws-sdk'
require 'uuid'
require 'pp'
class InstanceManager
def listInstances()
ec2 = Aws::EC2::Client.new(region: "eu-west-1")
resp = ec2.describe_instances()
resp.reservations.each do |res|
res.instances.each do |inst|
iid = inst[:instance_id]
istate = inst[:state].name
puts "#{iid} #{istate}"
end
end
end
def stop(iid)
ec2 = Aws::EC2::Client.new(region: "eu-west-1")
inst = ec2.describe_instances(instance_ids: [iid]).reservations[0].instances[0]
if ( inst[:state].name == "running" )
puts "stopping #{iid}"
res=ec2.stop_instances(dry_run: false, instance_ids: [iid],force: true)
pp res
else
puts "#{iid} already stopped"
end
pp inst
end
def start(iid)
ec2 = Aws::EC2::Client.new(region: "eu-west-1")
inst = ec2.describe_instances(instance_ids: [iid]).reservations[0].instances[0]
if ( inst[:state].name == "stopped" )
pp inst
puts "starting #{iid}"
res=ec2.start_instances(dry_run: false, instance_ids: [iid],force: true)
pp res
else
puts "#{iid} already started"
end
pp inst
end
end
if __FILE__ ==$0
z = InstanceManager.new
z.listInstances()
# z.stop("i-e122b106")
z.start("i-e122b106")
end
答案 0 :(得分:1)
我怀疑您遇到了与EC2 API最终的一致性问题。这在使用Amazon EC2时非常常见。当您发出命令(例如#stop_instance
)时,#describe_instances
呼叫将继续返回状态,例如运行一段时间,直到状态发生变化。这可能需要几分钟。