User-data scripts is not running on my custom AMI, but working in standard Amazon linux

Kai picture Kai · Nov 23, 2014 · Viewed 52.7k times · Source

I searched a lot of topic about "user-data script is not working" in these few days, but until now, I haven't gotten any idea about my case yet, please help me to figure out what happened, thanks a lot!

According to AWS User-data explanation:

When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts.

So I tried to pass my own user-data when instance launch, this is my user-data:

\#!/bin/bash

echo 'test' > /home/ec2-user/user-script-output.txt

But there is no file in this path: /home/ec2-user/user-script-output.txt

I checked /var/lib/cloud/instance/user-data.txt, the file is exist and same as my user-data script.

Also I checked the log in /var/log/cloud-init.log, there is no error message.

But the user-data script is working if I launch an new instance with Amazon linux(2014.09.01), but I'm not sure what difference between my AMI (based on Amazon linux) and Amazon linux.

The only different part I saw is if I run this script:

sudo yum list installed | grep cloud-init

My AMI:

cloud-init.noarch 0.7.2-8.33.amzn1 @amzn-main

Amazon linux:

cloud-init.noarch 0.7.2-8.33.amzn1 installed

I'm not sure this is the reason?

If you need more information, I'm glad to provide, please let me know what happened in my own AMI and how to fix it?

many thanks

Update

Just found an answer from this post,

If I add #cloud-boothook in the top of user-data file, it works!

#cloud-boothook
#!/bin/bash
echo 'test' > /home/ec2-user/user-script-output.txt

But still not sure why.

Answer

Thibault Deheurles picture Thibault Deheurles · Jun 1, 2016

User_data is run only at the first start up. As your image is a custom one, I suppose it have already been started once and so user_data is desactivated.

For windows, it can be done by checking a box in Ec2 Services Properties. I'm looking at the moment how to do that in an automated way at the end of the custom image creation.

For linux, I suppose the mechanism is the same, and user_data needs to be re-activated on your custom image.

The #cloud-boothook make it works because it changes the script from a user_data mechanism to a cloud-boothook one that runs on each start.


EDIT :

Here is the code to reactivate start on windows using powershell:

$configFile = "C:\\Program Files\\Amazon\\Ec2ConfigService\\Settings\\Config.xml"
[xml] $xdoc = get-content $configFile
$xdoc.SelectNodes("//Plugin") |?{ $_.Name -eq "Ec2HandleUserData"} |%{ $_.State = "Enabled" }
$xdoc.SelectNodes("//Plugin") |?{ $_.Name -eq "Ec2SetComputerName"} |%{ $_.State = "Enabled" }
$xdoc.OuterXml | Out-File -Encoding UTF8 $configFile

$configFile = "C:\\Program Files\\Amazon\\Ec2ConfigService\\Settings\\BundleConfig.xml"
[xml] $xdoc = get-content $configFile
$xdoc.SelectNodes("//Property") |?{ $_.Name -eq "AutoSysprep"} |%{ $_.Value = "Yes" }
$xdoc.OuterXml | Out-File -Encoding UTF8 $configFile

(I know the question focus linux, but it could help others ...)