Ubuntu – EC2 userdata not running with custom per-boot cloud-init script

amazon ec2cloud-initUbuntu

I'm trying to start a process at boot on a Ubuntu 14 LTS AMI (ami-3bdd502c) using cloud-init's per-boot script facility and configure that process with EC2 userdata. The process has sensible defaults but I'm attempting to allow these defaults to be overridden by using EC2 userdata to write some bits to /etc/default/foo.

However, when I drop the custom per-boot script on disk (/var/lib/cloud/scripts/per-boot/run-process.sh) it executes as expected but the EC2 userdata step does not run. If I launch a version of this AMI without the per-boot script the EC2 userdata step runs as expected. It seems as if the per-boot script is clobbering the EC2 userdata step.

The EC2 userdata script I am using looks like this:

#!/bin/bash

echo "CLUSTER=foo" >> /etc/default/foo

As per /etc/cloud/cloud.cfg I would expect this step to run before the scripts-per-boot step:

cloud_final_modules:
 - rightscale_userdata
 - scripts-vendor
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 ...

Further, manually running the running the cloud-final step as well as the userdata step individually works fine

sudo cloud-init modules --mode=final
sudo cloud-init single -n rightscale_userdata

Towards the end of my investigation I noticed that when I add the #cloud-boothook directive at the top of the userdata script everything works as expected:

#cloud-boothook
#!/bin/bash

echo "CLUSTER=foo" >> /etc/default/foo

I realize that there are a dozen other ways to accomplish this but I'm really looking to understand why I'm seeing this behavior. So, what am I missing here?

Best Answer

Amazon' userdata is only executed the first time a machine is started. If you create a custom ami the machine which was used to create this image was already started once.

You might try to setup your MIME as to run it every time on boot. Google helps with that