1

hay folks , I want to run a script in gcp machine for that i created a resource below file

  disk     = google_compute_disk.default2.id
  instance = google_compute_instance.default.id
} # aatach disk to vm

resource "google_compute_firewall" "firewall" {
  name    = "gritfy-firewall-externalssh"
  network = "default"
  allow {
    protocol = "tcp"
    ports    = ["22"]
  }
  source_ranges = ["0.0.0.0/0"] 
  target_tags   = ["externalssh"]
} # allow ssh

resource "google_compute_address" "static" {
  name = "vm-public-address"
  project = "fit-visitor-305606"
  region = "asia-south1"
  depends_on = [ google_compute_firewall.firewall ]
} # reserve ip

resource "google_compute_instance" "default" {
  name         = "new"
  machine_type = "custom-8-16384"
  zone         = "asia-south1-a"

  tags = ["foo", "bar"]

  boot_disk {
    initialize_params {
      image = "centos-cloud/centos-7"
    }
  }

  network_interface {
    network = "default"

    access_config { 
        nat_ip = google_compute_address.static.address     
    }
  }
  metadata = {
    ssh-keys = "${var.user}:${file(var.publickeypath)}"
  }
  lifecycle {
    ignore_changes = [attached_disk]
  }
    provisioner "file" {
    source      = "autoo.sh"
    destination = "/tmp/autoo.sh"
  }
provisioner "remote-exec" {
    connection {
      host        = google_compute_address.static.address
      type        = "ssh"
      user        = var.user
      timeout     = "500s"
      private_key = file(var.privatekeypath)
    }
    inline = [
      "sudo yum -y install epel-release",
      "sudo yum -y install nginx",
      "sudo nginx -v",
    ]
  }
} # Create VM

resource "google_compute_disk" "default2" {
  name  = "test-disk"
  type  = "pd-balanced"
  zone  = "asia-south1-a"
  image = "centos-7-v20210609"
  size =  100
} # Create Disk 

using this I am able to create VM and disk and also able to attach vm to disk but not able to run my script

error log are = logs

and private key part is working fine the key is assign to VM and I try to connect with that key it is connected may the problem with the provision part only any help or guidance would be really helpful...

1
  • Typically you want to do this kind of stuff in a null_resource. As is, the script will trigger after the compute instance is created, but won't wait for resources you might need. A null resource decouples your compute instance from code execution by letting you configure triggers so that the script won't run until you know all the cloud resources it depends on are created. Commented Jul 6, 2021 at 13:09

1 Answer 1

1

Like error message says, you need connection configuration for provisioner. Also you need remote-exec provisoner for running scripts.

    provisioner "file" {
    source = "autoo.sh"
    destination = "/tmp/autoo.sh"
    connection {
        type = "ssh"
        user = var.user
        private_key = file(var.privatekeypath)
    }
  }
    provisioner "remote-exec" {
    inline = [
      "chmod +x /tmp/autoo.sh",
      "cd /tmp",
      "./autoo.sh"
    ]
    connection {
        type = "ssh"
        user = var.user
        private_key = file(var.privatekeypath)
    }

source: https://stackoverflow.com/a/36668395/5454632

Sign up to request clarification or add additional context in comments.

2 Comments

well this is working find thnaks can you please let me know how can i run a script with root access
I think it is about your user. If your user have right to run commands with sudo privileges you can run it as root.