An instance of an EC2 instance of Userdata does not use a lifecycle hook to stop a newly launched instance from starting until it completes execution.
Shutting down the web server at the beginning of your user data script sounds a bit unreliable to me, and as a result, I highly recommend using AutoScaling features that were designed to solve this very problem.
I have two suggestions:
Option1:
Using lifecycle hooks is not at all complicated once you read the docs . And in your user data, you can easily use the CLI to control the hook, check this one . In fact, the hook can be controlled from any supported language or scripting language.
Option 2:
If you donโt like manual handling of lifecycle hooks, I would recommend abandoning your user data script and working with AWS CodeDeploy. You could use CodeDeploy to deploy anything (for example, an empty S3 folder), but you could use deployment hook scripts to replace your custom script data. Code Deploy integrates seamlessly with AutoScaling and automatically handles lifecycle hooks. The new instance will not be started by AutoScaling until the deployment is completed. Read the docs here and here for more information.
However, I would like to suggest that you go to option 1. Lifecycle interceptors were designed to solve the problem you have. They are powerful, reliable, awesome and free. Use them.
mickzer
source share