Reputation: 83
We are trying to build an an autoscaling group(lets say AS) configured with an elastic load balancer(lets say ELB) in AWS. The autoscaling group itself is configured with a launch configuration(lets say LC). As far as I could understand from the AWS documentation, pasting a script, as-is, in the user data section of the launch configuration would run that script for every instance launched into an auto scaling group associated with that auto scaling group.
For example pasting this in user data would have a file named configure available in the home folder of a t2 micro ubuntu image:
#!/bin/bash
cd
touch configure
Our end goal is: Increase instances in auto scaling group, they launch with our startup script and this new instance gets added behind the load balancer tagged with the auto scaling group. But the script was not executed at the instance launch. My questions are: 1. Am i missing something here? 2. What should I do to run our startup script at time of launching any new instance in an auto scaling group? 3. Is there any way to verify if user data was really picked up by the launch?
Upvotes: 4
Views: 3429
Reputation: 6413
The direction you are following is right. What is wrong is your user data script.
Problem 1:
What you have to remember is that user data will be executed as user root
, not ubuntu
. So if your script worked fine, you would find your file in /root/configure
, NOT IN /home/ubuntu/configure
.
Problem 2:
Your script is actually executing, but it's incorrect and is failing at cd
command, thus file is not created.
cd
builtin command without any directory given will try to do cd $HOME
, however $HOME
is NOT SET during cloud-init run, so you have to be explicit here.
Change your script to below and it will work:
#!/bin/bash
cd /root
touch configure
You can also debug issues with your user-data script by inspecting /var/log/cloud-init.log
log file, in particular checking for errors in it: grep -i error /var/log/cloud-init.log
Hope it helps!
Upvotes: 5