bashamazon-web-servicesubuntu-14.04aws-code-deploycloud-init

Ubuntu ec2 instance fails to run userdata script


I am trying to execute a script which needs to be run on Ubuntu 14.04 ec2 instance whenever the instance or it's image in auto scaling group spins up-

#!/bin/bash-x
REGION=`curl http://169.254.169.254/latest/dynamic/instance-    identity/document|grep region|awk -F\" '{print $4}'`
sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev
cd
wget http://ftp.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
tar -xzvf ruby-2.3.1.tar.gz
cd ruby-2.3.1/
./configure
make
sudo make install
apt-get install apache2 
cd /home/ubuntu  
wget https://aws-codedeploy-$REGION.s3.amazonaws.com/latest/install
chmod +x ./install
./install auto

But the script never runs. I went to cloud-init-output.log file and following is the log details-

Cloud-init v. 0.7.5 running 'modules:final' at Mon, 12 Dec 2016 15:54:20 +0000. Up 36.92 seconds.
2016-12-12 15:54:20,432 - util.py[WARNING]: Failed running     /var/lib/cloud/instance/scripts/part-001 [-]
2016-12-12 15:54:20,436 - cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
2016-12-12 15:54:20,436 - util.py[WARNING]: Running scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scrip$
Cloud-init v. 0.7.5 finished at Mon, 12 Dec 2016 15:54:20 +0000.     Datasource DataSourceEc2.  Up 37.11 seconds

Specifically, I am trying to achieve is to install codedeploy in the instance.


Solution

  • To test this, I launched an Amazon Linux EC2 instance with #!/bin/bash-x at the top of the User Data.

    The log said:

    /bin/sh: /var/lib/cloud/instance/scripts/part-001: /bin/bash-x: bad interpreter: No such file or directory
    

    I re-launched it without the -x and it worked fine.

    So, that is definitely the cause of the problem for Amazon Linux. Things might be different with Ubuntu.

    Update: As per sqlbot's suggestion (below), I tried it again with #!/bin/bash -x (a space before -x) and it worked fine.