Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

user-data (cloud-init) script not executing on EC2

my user-data script

#!
set -e -x
echo `whoami`
su root
yum update -y
touch ~/PLEASE_WORK.txt

which is fed in from the command:

ec2-run-instances ami-05355a6c -n 1 -g mongo-group -k mykey -f myscript.sh -t t1.micro -z us-east-1a

but when I check the file /var/log/cloud-init.log, the tail -n 5 is:

[CLOUDINIT] 2013-07-22 16:02:29,566 - cloud-init-cfg[INFO]: cloud-init-cfg ['runcmd']
[CLOUDINIT] 2013-07-22 16:02:29,583 - __init__.py[DEBUG]: restored from cache type DataSourceEc2
[CLOUDINIT] 2013-07-22 16:02:29,686 - cloud-init-cfg[DEBUG]: handling runcmd with freq=None and args=[]
[CLOUDINIT] 2013-07-22 16:02:33,691 - cloud-init-run-module[INFO]: cloud-init-run-module ['once-per-instance', 'user-scripts', 'execute', 'run-parts', '/var/lib/cloud/data/scripts']
[CLOUDINIT] 2013-07-22 16:02:33,699 - __init__.py[DEBUG]: restored from cache type DataSourceEc2

I've also verified that curl http://169.254.169.254/latest/user-data returns my file as intended.

and no other errors or the output of my script happens. how do I get the user-data scrip to execute on boot up correctly?

like image 485
lollercoaster Avatar asked Jul 22 '13 16:07

lollercoaster


People also ask

Where is the user data script in EC2 instance runs?

When a user data script is processed, it is copied to and run from /var/lib/cloud/instances/ instance-id / . The script is not deleted after it is run. Be sure to delete the user data scripts from /var/lib/cloud/instances/ instance-id / before you create an AMI from the instance.

Does user data run on reboot EC2?

Short description. By default, user data scripts and cloud-init directives run only during the first boot cycle when an EC2 instance is launched.


1 Answers

Actually, cloud-init allows a single shell script as an input (though you may want to use a MIME archive for more complex setups).

The problem with the OP's script is that the first line is incorrect. You should use something like this:

#!/bin/sh

The reason for this is that, while cloud-init uses #! to recognize a user script, the operating system needs a complete shebang line in order to execute the script.

So what's happening in the OP's case is that cloud-init behaves correctly (i.e. it downloads and tries to run the script) but the operating system is unable to actually execute it.


See: Shebang (Unix) on Wikipedia

like image 172
Andrea Avatar answered Oct 12 '22 20:10

Andrea