I have this script that checks if the webserver is up and the health check succeeds. I was using "sleep" before the service starts. I am looking to improve it, and put some "iteration/until" options which will check if service health check succeeds, but will timeout 60/120 seconds and do the LB registration else will throw an error.
#!/bin/bash
instanceid=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)
healthcheck=$(curl -X GET http://localhost | grep "yes its working")
HEALTHCHECK=$?
if [ $HEALTHCHECK -eq 0 ] ; then
echo "Server registered to ELB"
usr/local/bin/aws elbv2 register-targets --target-group-arn arn:aws:elasticloadbalancing:ap-south-1:xxxxxxxx:targetgroup/my-targetgroup/xxxxxxxx --targets Id=$instanceid
exit 0
else
echo "healthcheck failed"
exit 0
fi