I am not very familiar with Beanstalk working-level instances, but as far as I know, they get 1 task at a time, right? If so, why do you need to stop the SQS daemon, presumably if scaling occurs because there are no more tasks in the work queue, so this instance should not get a new one. If someone really arrives at that moment, he will not be removed from the SQS queue, and as soon as the visibility timeout expires, he will be detected by another worker node.
The lifecycle hook will not fire until instance protection is disabled because it only starts when the instance is selected for completion.
If you can add a piece of code to your code, stating that "if the task ends and the new one does not come -> then disable the protection of the instance in this instance." Thus, only instances without tasks can be terminated.
Alternatively, you can leave everything permanently protected and periodically run the bash script on the instance using the Cron job, checking if it is currently running the task or not, and if not, turn off instance protection.
source share