简体   繁体   English

使用ruby创建ec2实例并连接到创建的机器

[英]Create ec2 instance with ruby and connect to the created machine

After i create the instance using instances.create, i need to connect to the machine and perform some operations. 使用instances.create创建实例后,需要连接到计算机并执行一些操作。 The question is - what is the best way to know that the creation has finished and i can connect to the machine ? 问题是-知道创建完成并且可以连接到机器的最佳方法是什么?

The machine's state right after creating it is 'running', so how can i know when the machine finished its initialization and i can ssh to it ? 创建后机器的状态就在“运行中”,那么我怎么知道机器何时完成初始化并且可以通过ssh进入呢?

I saw an existing code where its done by ssh'ing every X seconds, and if its get timeout than it means that the machine is still initializing. 我看到了一个现有的代码,该代码通过每X秒ssh来完成,如果它超时,则表示该计算机仍在初始化。

I am looking for a more elegant way. 我正在寻找一种更优雅的方式。

Once the instance is created, you get the instances ID. 创建实例后,您将获得实例ID。 So, use that instance ID to check the "Instance Status" . 因此,使用该实例ID来检查“实例状态” Each instance has to go through 2 status checks : System Status Check and Instance Status Check. 每个实例必须经过2个status checks系统状态检查实例状态检查。

I have observed that once both these checks are passed, the instance is ready to be logged in. 我观察到,一旦这两项检查都通过,实例就可以登录了。

The corresponding Method in ruby sdk which pulls this data is describe_instance_status ruby sdk中提取此数据的相应方法为describe_instance_status

I was also looking for a similar solution, and I found a test on the AWS Ruby SDK that does just that: https://github.com/aws/aws-sdk-ruby/blob/master/samples/ec2/run_instance.rb 我也在寻找类似的解决方案,并且在AWS Ruby SDK上找到了一个测试,该测试就可以做到: https : //github.com/aws/aws-sdk-ruby/blob/master/samples/ec2/run_instance。 RB

begin
  Net::SSH.start(instance.ip_address, "ec2-user",
                 :key_data => [key_pair.private_key]) do |ssh|
  puts "Running 'uname -a' on the instance yields:"
  puts ssh.exec!("uname -a")
end
rescue SystemCallError, Timeout::Error => e
  # port 22 might not be available immediately after the instance finished
  launching
  sleep 1
  retry
end

It might not be the cleanest solution, but it's a solution Amazon adopts. 它可能不是最干净的解决方案,但却是亚马逊采用的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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