AWS autoscaling starts non-finished instances due to userdata script

I have autoscaling that works fine, with a launch configuration, where I defined a userdata script that runs when a new instance starts.

Basecode updates update and generate a cache, it takes a few seconds. But as soon as the instance is โ€œcreatedโ€ (and not โ€œreadyโ€), autoscaling adds it to the load balancer.

This is a problem because while the userdata script is running, the instance does not respond with a good answer (basically, 500 errors are thrown).

I would like to avoid this, of course, I saw this documentation: http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/InstallingAdd

As with a standalone EC2 instance, you can configure instances running in an auto-scaling group using user data. For example, you can specify a script configuration using the user data field in the AWS management console or the -userdata parameter in the AWS CLI.

If you have software that cannot be installed using the script configuration, or if you need to manually change the software before Auto Scaling adds an instance to the group, add a lifecycle binding to your Auto Scaling group that notifies you when the Auto group Scaling starts the instance. This hook holds the instance in the Pending: Wait state when installing and configuring additional software.

Looks like I'm not in that. Also, changing the waiting hook to a userdata script is difficult. There should be a simple solution to fix my problem.

Thank you for your help!

+2
amazon-web-services amazon-ec2
source share
2 answers

@Brooks said the easiest way to โ€œwaitโ€ before an ELB serves an instance is to deal with the health status of the ELB.

I solved my problem by disabling the http server at the beginning of the userdata script. Therefore, the ELB cannot have a green health status, and it does not send clients to the instance. I restarted the http server at the end of the script, health is good, so ELB serves it.

-2
source share

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.

+2
source share

All Articles