Search code examples
ruby-on-railsrubyruby-on-rails-3amazon-web-servicesfog

How to verify AWS server termination with Fog?


I have 3 line of code for a Rails app in a 'begin' block that is meant to terminate an AWS compute instance using Fog and set a string value upon success:

  @server = @connection.servers.get(params[:id])
  @server.destroy
  @server_deletion_result = "success"

This code works, but it simply sends a command to terminate the instance to AWS. Using Fog, how can I verify that the instance has finished terminating?

I tried this, to no avail:

  while @server.state != "terminated" do
    sleep 3
  end
  @server_deletion_result = "success"

It just appears to hang, even well after the instance shows "terminated" in the AWS console.

So, thoughts?


Solution

  • A friend of mine helped me answer this question via Twitter. The answer was to call the reload() function on the server object, then check it. Fog caches the server object and it must be updated to check the state.

    Here was my final solution:

      @server.reload
      while @server.state != "terminated" do
        sleep 3
        @server.reload
      end
    

    EDIT: Thanks to Frederick Cheung, who has a better answer in the comments:

    @server.wait_for {state == 'terminated'}