title: CodeDeploy Error : Fail to remove instance from Loadbalancer
body: <p>I am getting below code deploy error in AWS Console </p>
  
  <pre><code>LifecycleEvent - ApplicationStop
  Script - scripts/deregister_from_elb.sh
  [stderr]Running AWS CLI with region: us-east-1
  [stderr]Started deregister_from_elb.sh at 2020-01-21 10:53:43
  [stderr]Checking that user set at least one load balancer
  [stderr]Checking validity of load balancer named 'lb'
  [stderr]Checking health of 'i-xxxxxxxxxxxxxxxx' as known by ELB 'lb'
  [stderr]Checking status of instance 'i-xxxxxxxxxxxxxxxx' in load balancer 'lb'
  [stderr]Deregistering i-xxxxxxxxxxxxxxxx from lb
  [stderr]Waiting for instance to de-register from its load balancers
  [stderr]Checking 2 times, every 70 seconds, for instance i-xxxxxxxxxxxxxxxx to be in state OutOfService
  [stderr]Checking status of instance 'i-xxxxxxxxxxxxxxxx' in load balancer 'lb'
  [stderr]Instance is currently in state: InService
  [stderr]Checking status of instance 'i-xxxxxxxxxxxxxxxx' in load balancer 'lb'
  [stderr]Instance is currently in state: InService
  [stderr]Instance failed to reach state, OutOfService within 140 seconds
  [stderr][FATAL] Failed waiting for i-xxxxxxxxxxxxxxxx to leave lb
  </code></pre>
  
  <p>While checking code-deploy agent log file in EC2 Instance its showing </p>
  
  <pre><code>ERROR [codedeploy-agent(14347)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Error during perform: InstanceAgent::Plugins::CodeDeployPlugin::ScriptError - Script at specified location: scripts/deregister_from_elb.sh failed with exit code 1 -
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:183:in `execute_script'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:129:in `block (2 levels) in execute'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:117:in `each'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:117:in `block in execute'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:208:in `create_script_log_file_if_needed'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:115:in `execute'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:158:in `block (3 levels) in map'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:146:in `each'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:146:in `block (2 levels) in map'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:68:in `execute_command'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_poller.rb:114:in `process_command'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_poller.rb:96:in `acknowledge_and_process_command'
  /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_poller.rb:75:in `block in perform'
  /opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:348:in `call'
  /opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:348:in `run_task'
  /opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:337:in `block (3 levels) in create_worker'
  /opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `loop'
  /opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `block (2 levels) in create_worker'
  /opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `catch'
  /opt/codedeploy-agent/vendor/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `block in create_worker'
  /opt/codedeploy-agent/vendor/gems/logging-1.8.2/lib/logging/diagnostic_context.rb:323:in `call'
  /opt/codedeploy-agent/vendor/gems/logging-1.8.2/lib/logging/diagnostic_context.rb:323:in `block in create_with_logging_context'
  2020-01-21 10:54:56 WARN  [codedeploy-agent(14347)]:
  InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Calling PutHostCommandComplete: "Code Error" 
  2020-01-21 10:54:56 WARN  [codedeploy-agent(14347)]: 
  InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Calling PutHostCommandComplete: "Code Error" 
  2020-01-21 10:54:56 INFO  [codedeploy-agent(14347)]: Version file found in /opt/codedeploy-agent/.version with agent version OFFICIAL_1.0-1.1597_deb.
  2020-01-21 10:54:56 INFO  [codedeploy-agent(14347)]: Version file found in /opt/codedeploy-agent/.version with agent version OFFICIAL_1.0-1.1597_deb.
  2020-01-21 10:54:56 INFO  [codedeploy-agent(14347)]: [Aws::CodeDeployCommand::Client 200 0.186875 0 retries] put_host_command_complete(command_status:"Failed",diagnostics:{format:"JSON",payload:"{\"error_code\":5,\"script_name\":\"\",\"message\":\"Script at specified location: scripts/deregister_from_elb.sh failed with exit code 1\",\"log\":\"\"}"},host_command_identifier:"1YmxpYzAwMS9hcm46YXdzOnNkczp1cy1lYXN0LTE6MTMxMjE0OTI1MjQ1OmRlcGxveW1lbnQvZC1LVjgyTVRPNTIiLCJob3N0SWQiOiJhcm46YXdzOmVjMjp1cy1lYXN0LTE6MTMxMjE0OT1MjQ1Omluc3RhbmNlL2ktMDFhMGFiMjlhMWM2NjEwNjJ8MXwwIiwiY29tbWFuZE5hbWUiOiJBcHBsaWNhdGlvblN0b3AiLRleCI6MX0=") 
  </code></pre>
  
  <p>I solved the issue temporarily by removing all the contents in /opt/codedeploy-agent/deployment-root folder and restarting code-deploy agent service. But the issue get repeating after some time. Did any one know why is it occuring ?.  </p>