diff --git a/lib/kitchen/driver/aws/client.rb b/lib/kitchen/driver/aws/client.rb index 0dca8341..aaf9b3ca 100644 --- a/lib/kitchen/driver/aws/client.rb +++ b/lib/kitchen/driver/aws/client.rb @@ -75,6 +75,13 @@ def get_instance_from_spot_request(request_id) ).to_a[0] end + # check if instance exists, given an id + # @param id [String] aws instance id + # @return boolean + def instance_exists?(id) + resource.instance(id).exists? + end + def client @client ||= ::Aws::EC2::Client.new end diff --git a/lib/kitchen/driver/ec2.rb b/lib/kitchen/driver/ec2.rb index 78af58bd..b4bcc844 100644 --- a/lib/kitchen/driver/ec2.rb +++ b/lib/kitchen/driver/ec2.rb @@ -285,7 +285,7 @@ def destroy(state) # If we are going to clean up an automatic security group, we need # to wait for the instance to shut down. This slightly breaks the # subsystem encapsulation, sorry not sorry. - if state[:auto_security_group_id] && server + if state[:auto_security_group_id] && server && ec2.instance_exists?(state[:server_id]) server.wait_until_terminated do |waiter| waiter.max_attempts = config[:retryable_tries] waiter.delay = config[:retryable_sleep]