when I want to use metadata_startup_script in template if I use commands in one line it works fine but in pass one line or even if I want to use some html it does not work fine and I face error (multiple lines in terraform and use new line and if I want to refer it to local bash script with some thing like metadata_startup_script = "${file("startup.sh")}" it work but when instance come up ,When I ssh to it my appache does not install in machine so my startup script doesnt work but I have the in the template! what is the issue for that ,is anybody have ay idea about that?
2 Answers
As you mentioned you can see the script in template, I would recommend you to check in the console and check if your script is visible, if yes check the serial console to check why your script is not running. It might due to typo or due to missing the confirmation/consent.
To reproduce your issue I am following the documentation now set my main.tf
as below:
metadata_startup_script = "${file("file-name-with-full-path")}"
In that file i set my startup script as below:
#! /bin/bash
………...
sudo apt update
wait 60
sudo apt install apache2 -y
wait 60
sudo systemctl start apache2
wait 60
sudo systemctl status apache2
After creating the vm i can see the apache is running in my instance.
$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled;
vendor preset: enabled)
Active: active (running) since Fri 2020-05-15 13:5
4:45 UTC; 10s ago
Main PID: 2338 (apache2)
Tasks: 55 (limit: 4915)
CGroup: /system.slice/apache2.service
├─2338 /usr/sbin/apache2 -k start
├─2340 /usr/sbin/apache2 -k start
└─2341 /usr/sbin/apache2 -k start
May 15 13:54:45 terra-test-2 systemd[1]: Starting The Apache HTT
P Server...
May 15 13:54:45 terra-test-2 systemd[1]: Started The Apache HTTP
Server.
![](../../users/profiles/533934.webp)
- 186
- 4
-
thank you for your answer but in my log say:INFO startup-script: /bin/bash: /startup-hvh0shxt/tmpyy84ro7l: /bin/bash^M: bad interpreter: No such file or directory May 16 17:13:53 gnsgcpproject-web-rrk9 startup-script: INFO startup-script: Return code 126. – farzin May 16 '20 at 17:48
-
thank you for your answer but in my log say:INFO startup-script: /bin/bash: /startup-hvh0shxt/tmpyy84ro7l: /bin/bash^M: bad interpreter: No such file or directory May 16 17:13:53 gnsgcpproject-web-rrk9 startup-script: INFO startup-script: Return code 126.also I should again say my startup is on my template but when I go to my instances it is not installed@Muss Rahman – farzin May 16 '20 at 18:03
I t is related to my text editor I used VSC so I usethis way because VSC which can not read,Even if you have a single CRLF in the file, the above setting will be ignored and the whole file will be converted to CRLF. You first need to convert all CRLF into LF before you can open it in Visual Studio Code.
![](../../users/profiles/574928.webp)
- 1
- 1
- 3