Reputation: 2804
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.
Upvotes: 2
Views: 591
Reputation: 387
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.
Upvotes: 0