7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS SDK for RubyでEC2作成後に起動を待つ

Posted at

EC2作成後システムチェックとインスタンスステータスチェックが行われる

インスタンスステータスチェックが終わるまで待てば起動完了となる
インスタンスステータスチェックの結果はDescribeInstanceStatus APIで取得できる

以下のようにインスタンスステータスチェックの結果を取得して完了するまで待てば良い

require 'aws-sdk'

ec2 = AWS::EC2.new(:proxy_uri => ENV['HTTP_PROXY'] || ENV['HTTP_PROXY'], :region => "ap-northeast-1")

# 略

created_instance = ec2.instances.create(ec2hash)

ec2info = ec2.client.describe_instance_status({"instance_ids" => [created_instance.instance_id]})
status = ec2info.instance_status_set[0].instance_status.details[0].status rescue nil

st_retry = 0
while status != "passed" do
  sleep 15
  ec2info = ec2.client.describe_instance_status({"instance_ids" => [created_instance.instance_id]})
  status = ec2info.instance_status_set[0].instance_status.details[0].status rescue nil
  st_retry += 1
  raise "instance status is still bad" if st_retry >= 20
end

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?