Abhishek
Abhishek

Reputation: 7045

Does AWS SDK provide event based architecture?

I'm using this link as the tutorial to launch the instance. Following is my code,

instance = resource.create_instances.first
sleep 10 until instance.state.name == "running"
//Do something once instance is up and running

Above code launches a instances and wait in a loop until instance state is running. Once it is running I do some processing on it. As you can see, I'm waiting for instance to be ready before I could do my processing. It is an bad idea, as I'm polling AWS every 10 second (polling model).

Does AWS SDK has something in built that will trigger an event when instance changes it's state (Something like push model)? If yes, can you please share the way to do it?

Upvotes: 0

Views: 31

Answers (1)

roychri
roychri

Reputation: 2936

The SDK does provide a feature, but I am unsure what model it is using (push or pull). It's probably pull but I'm not sure. Regardless of the model, This is the recommended way of waiting.

begin
  instance.wait_until(max_attempts:10, delay:10) {|instance|
    instance.state.name == 'running'
  }
rescue Aws::Waiters::Errors::WaiterFailed
  # resource did not enter the desired state in time
end

More details here: http://docs.aws.amazon.com/sdkforruby/api/Aws/Resources/Resource.html#wait_until-instance_method

Upvotes: 1

Related Questions