EC2 UserData script is not running on startup

Viewed 921

I am trying to automate EC2 instance creation. I start an instance from a custom AMI, and specify a UserData script during launch configuration. I have noticed that UserData scripts are not being executed. How can I troubleshoot this and what am I missing? Can I specify the script somewhere in the AMI?

Here is my startup script:

#!/bin/sh
sudo su
systemctl start docker
docker run -it --publish 8080:8080 exampleapplication:latest

UPDATE: I removed the sudo su and added an echo command for debugging. I checked the system logs and saw my echoed string. This is what I got:

[   16.291460] cloud-init[3200]: plsssssss
[   17.378345] bridge: filtering via arp/ip/ip6tables is no longer available by default. Update your scripts to load br_netfilter if you need this.
[   17.394716] Bridge firewalling registered
[   17.412835] nf_conntrack version 0.5.0 (8192 buckets, 32768 max)
[   17.479117] Initializing XFRM netlink socket
[   17.489731] Netfilter messages via NETLINK v0.30.
[   17.502725] ctnetlink v0.93: registering with nfnetlink.
[   17.546680] IPv6: ADDRCONF(NETDEV_UP): docker0: link is not ready
[   18.114688] cloud-init[3200]: the input device is not a TTY
[   18.121279] cloud-init[3200]: May 31 15:11:07 cloud-init[3200]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
[   18.131795] cloud-init[3200]: May 31 15:11:07 cloud-init[3200]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
[   18.142379] cloud-init[3200]: May 31 15:11:07 cloud-init[3200]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
2 Answers

I suspect that the first 'sudo su'. That is launching new non-login root shell. It may be affecting execution of the existing shell, where user data is running. It's not needed. Not sure why you have it there. User data runs as root anyway.

Replace it with an 'echo start'. And relaunch. Check your system log and you should see that Echo in the Management console

UPDATE

Also add this https://aws.amazon.com/premiumsupport/knowledge-center/ec2-linux-log-user-data/

This is a major lifesaver for trouble shooting user data issues.

If you are using a custom AMI and had UserData passed in the instance you generated the AMI from, then, the cloud-init per-instance(i.e., on first boot, in this case the UserData you pass when you create an instance from your custom AMI) section will not be executed as it does not recognize this as first boot.

To make it recognize as first boot, you need to clean the cache of cloud-init on the Instance you generate the custom AMI from and also make sure you enable NoReboot option because if a reboot happens then cache will be repopulated before creating the custom AMI, so your custom AMI would arrive with a cache already showing that this is not the first boot but a subsequent one.

Luckily cleaning the cache is very easy,

So before you create custom AMI, ssh/ssm into your instance and execute the following,

sudo cloud-init clean

Optionally also remove the cloud-init logs, they will be recreated automatically

sudo rm -f /var/log/cloud-init*

and exit

Now you are ready to create your custom AMI and remember to tick the NoReboot option!

You can read more about all that in the cloud-init Boot Stages docs section.

Related