bin/handler-ec2_node.rb
# Method to check if there is any insance and if instance is in a valid state that could be deleted
Method to check if there is any insance and if instance is in a valid state that could be deleted
Method to check if there is any insance and if instance is in a valid state that could be deleted
def ec2_node_should_be_deleted?
    ec2 = Aws::EC2::Client.new(region: region)
    settings['ec2_node'] = {} unless settings['ec2_node']
    instance_states = @event['client']['ec2_states'] || settings['ec2_node']['ec2_states'] || ['shutting-down', 'terminated', 'stopping', 'stopped']
    instance_reasons = @event['client']['ec2_state_reasons'] || settings['ec2_node']['ec2_state_reasons'] || %w[Client.UserInitiatedShutdown Server.SpotInstanceTermination Client.InstanceInitiatedShutdown]
    begin
      instances = ec2.describe_instances(instance_ids: [instance_id]).reservations[0]
      if instances.nil? || instances.empty?
        true
      else
        instance_state_reason = instances.instances[0].state_reason.nil? ? nil : instances.instances[0].state_reason.code
        instance_state = instances.instances[0].state.name
        instance_states.include?(instance_state) && instance_reasons.include?(instance_state_reason)
      end
    rescue Aws::EC2::Errors::InvalidInstanceIDNotFound
      true
    end
  end
def ec2_node_should_be_deleted? ec2 = Aws :: EC2 :: Client . new ( region : region ) settings [ 'ec2_node' ] = { } unless settings [ 'ec2_node' ] instance_states = @event [ 'client' ] [ 'ec2_states' ] || settings [ 'ec2_node' ] [ 'ec2_states' ] || [ 'shutting-down' , 'terminated' , 'stopping' , 'stopped' ] instance_reasons = @event [ 'client' ] [ 'ec2_state_reasons' ] || settings [ 'ec2_node' ] [ 'ec2_state_reasons' ] || %w[ Client.UserInitiatedShutdown Server.SpotInstanceTermination Client.InstanceInitiatedShutdown ] begin instances = ec2 . describe_instances ( instance_ids : [ instance_id ] ) . reservations [ 0 ] if instances . nil? || instances . empty? true else instance_state_reason = instances . instances [ 0 ] . state_reason . nil? ? nil : instances . instances [ 0 ] . state_reason . code instance_state = instances . instances [ 0 ] . state . name instance_states . include? ( instance_state ) && instance_reasons . include? ( instance_state_reason ) end rescue Aws :: EC2 :: Errors :: InvalidInstanceIDNotFound true end end
Method to check if there is any insance and if instance is in a valid state that could be deleted
Method to check if there is any insance and if instance is in a valid state that could be deleted
# Defining region for aws SDK object # Finding the instance # If instance is empty/nil instance id is not valid so client can be deleted # Checking for instance state and reason, and if matches any of the user defined or default reasons then # method returns True # Returns instance state reason in AWS i.e: "Client.UserInitiatedShutdown" # Returns the instance state i.e: "terminated" # Return true is instance state and instance reason is valid