Google Cloud Terraform – Using metadata_startup_script in Google Cloud Template with Terraform

google-cloud-platformterraform

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?

Best Answer

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.
Related Topic