The startup script is executed on the VM defined as a resource but not on the VMs from the Terraform's instance template module

355 Views Asked by At

I tried to create a load balancer with the MIG consisting of three VMs. Additionally, I created a VM that is not part of the MIG but operates within the same subnet. The reason for making it was to check the connectivity between the VM and the VMs from the MIG.

Both MIG's VMs and the additional VM have the same startup script. I'll present it using the configuration of the additional VM (var.server_port is 80):

resource "google_compute_instance" "ssh-vm" {
  name = "ssh-vm"
  machine_type = "e2-standard-2"
  project = var.pro
  tags = ["allow-ssh"]
  zone = "europe-west1-b"

  boot_disk {
    initialize_params {
      image = "ubuntu-2004-focal-v20221213"
    }
  }

  network_interface {
    subnetwork = google_compute_subnetwork.subnetwork.self_link
    access_config {
      nat_ip = google_compute_address.static.address
    }
  }

  metadata = {
    startup-script = <<-EOF
        #!/bin/bash
        sudo snap install docker
        sudo docker version > file1.txt
        sleep 5
        sudo docker run -d --rm -p ${var.server_port}:${var.server_port} \
        busybox sh -c "while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; \
        echo 'yo'; } | nc -l -p ${var.server_port}; done"
        EOF
  }
}

One can see that the idea is to spin up docker and run the server that listens on :80. When I ssh onto this instance and run sudo docker ps, I receive the info that the process is running and serving on 80. The problem is that I have the same startup script within Terraform's instance_template module, but when I ssh into any of the machines from the MIG and run the same command, it says that the docker command is not found. Here's the configuration of the module:

module "instance_template" {
  source = "terraform-google-modules/vm/google//modules/instance_template"
  version = "7.9.0"
  region = var.region
  project_id = var.pro
  network = google_compute_network.vpc-network.self_link
  subnetwork = google_compute_subnetwork.subnetwork.self_link
  service_account = {
    email = google_service_account.service-acc.email
    scopes = ["cloud-platform"]
  }

  name_prefix = "webserver"
  tags = ["template-vm", "allow-ssh"]
  machine_type = "e2-standard-2"
  startup_script = <<-EOF
  #!/bin/bash
  sudo snap install docker
  sudo docker version > docker_version.txt
  sleep 5
  sudo docker run -d --rm -p ${var.server_port}:${var.server_port} \
  busybox sh -c "while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; \
  echo 'yo'; } | nc -l -p ${var.server_port}; done"
  EOF
  source_image = "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20221213"
  disk_size_gb = 10
  disk_type = "pd-balanced"
  preemptible = true

}

I checked and the module's attribute for the startup script is indeed startup_script and not metadata_startup_script.

0

There are 0 best solutions below