2

I am using CloudFormation to create an EC2 instance that runs Ubuntu. I have inserted into the UserData property of the EC2 instance a command to install pip and a command to install the cfn cloudformation helper scripts. To wit, the key cloudformation template fragment is:

"UserData" : {
      "Fn::Base64" : {
        "Fn::Join": ["", [
          "#!/bin/bash -xe","\n",
          "apt-get update","\n",
          "apt-get install -y python-pip","\n",
          "apt-get install -y python3","\n",
          "apt-get install -y heat-cfntools","\n"
          ]
        ]
      }
    }

And no, I don't have a metadata section. Question: why is the script not running? Here is the output, which I am getting on the newly created EC2 instance:

ubuntu@ip-10-0-0-121 :~$ curl -s http://169.254.169.254/latest/user-data

#/bin/bash -xe
apt-get update
apt-get install -y python-pip
apt-get install -y heat-cfntools

The script gets transferred from the DataUser section of the EC2 Resource listing in the Cloudformation template to the live EC2 isntance but as you can see below, the script is not executing:

ubuntu@ip-10-0-0-121:~$ pip

The program 'pip' is currently not installed. You can install it by typing:
sudo apt install python-pip

ubuntu@ip-10-0-0-121:~$ vi /var/log/cloud-init.log

log show nothing that stands out.

ubuntu@ip-10-0-0-121:~$ cfn-init

The program 'cfn-init' is currently not installed. You can install it by typing:  
sudo apt install heat-cfntools

Note: I am running the same script in the EC2's UserData section without any trouble when I don't use Cloudformation and instead use the EC2 console.

EDIT: I routinely use that aws cloudformation validate-template [NameOfTemplate] command. However, this tool only enables me to verify that the template is fully compliant with the JSON syntax. The tool does not verify anything else. If the template were broken, running Cloudformation would have resulted in a rollback. Cloudformation ran until it reported completion.

Vietnhi Phuvan
  • 2,704
  • 2
  • 25
  • 25
  • try validate your template using `aws cloudformation validate-template` as shown here. http://stackoverflow.com/questions/11854772/how-can-i-quickly-and-effectively-debug-cloudformation-templates – mootmoot Apr 18 '17 at 15:22
  • Check the system logs of the instance, you can check from aws console itself and see if anything is logged while the user data section is getting executed. – omuthu Apr 19 '17 at 04:41
  • Share the complete output of `/var/log/cloud-init-output.log`. – wjordan May 10 '17 at 20:54

1 Answers1

0

Something is removing your bang (!) from the shebang (#!).

On your output you have:

ubuntu@ip-10-0-0-121 :~$ curl -s http://169.254.169.254/latest/user-data
#/bin/bash -xe
apt-get update
apt-get install -y python-pip
apt-get install -y heat-cfntools

So cloudformation is reading "#/bin/bash" instead of "#!/bin/bash" and doesn't know how to execute it and doesn't do anything.

I can't tell why this is happening and how to fix it, but probably something related to your uploading process is removing the !.

Hope it helps someone, although this is an old question.

GMartinez
  • 301
  • 4
  • 9
  • "UserData": { "Fn::Base64": { "Fn::Join": [ "", [ "#/bin/bash -xe", "/n", { "Ref": "UserData" } ] ] } } I gave the following shell command in userdata parameter "apt-get update","\n","apt-get install -y apache2","\n","apt-get install -y php","\n" It also does not work @GMartinez – Arun Kumar Jan 24 '18 at 06:29
  • how to achieve the same through userdata parameter – Arun Kumar Jan 24 '18 at 10:28