I have an auto-scaling group (ASG) on AWS. The EC2 instances in the ASG run a user-data bash script when they launch. I am using terraform and their aws_autoscaling_group resource as my API.
I am now trying to add a warm pool to the ASG (warm_pool
block in the API). I have a problem with the user-data script on the warm pool instances:
Stopped
state, they do not run the bash script. When these instances are re-started and transitioned into the ASG, they do not run the bash script either.Running
state, they execute the user data bash script, and everything is fineWhat could be the problem? I would like warm pool instances launching into the Stopped
state to run the bash script before they are shut down.
In this tutorial from AWS they do exactly that. They run the user-data script on the warm pool instance before the instances are shut down. I am working to understand the difference between their CloudFormation set up and what I do, but on the surface it looks very similar. It looks like they are using a custom life cycle hook, but I am not sure if the hook controls the execution of user-data, or if it's the user-data script controlling the state of the hook.
As suggested in the comment by Vikram S, I had to use a MIME-formatted script with a config that ensures user script is always ran.
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="script.sh"
#!/bin/bash
touch /home/ec2-user/text
echo "Hello World" >> /home/ec2-user/text
--//