Search code examples
google-compute-engineterraformstackdrivergoogle-cloud-stackdriver

How to install stackdriver in LINUX(Ubuntu/Debian) GCE instances in GCP using Terraform script?


I am creating a LINUX Debian instance using the below terraform script.

resource "template_dir" "config" {
  source_dir      = "${path.module}/config.d/"
  destination_dir = "/tmp/fluent-templates"

  vars = {
    instance-name = "${var.instance_name}"
  }
}

resource "google_compute_instance" "default" {
  name         = "${var.instance_name}"
  project      = "${var.project}"
  machine_type = "${var.machine_type}"
  zone         = "${var.zone}"

  boot_disk {
    initialize_params {
      image = "${var.boot_disk_image}"
    }
  }

  network_interface {
    network = "default"

    access_config {
      // Ephemeral IP
    }
  }


  #StackDriver must be installed before this command runs,
  #as it will create the "/etc/google-fluentd/config.d" directory,
  #which is supposed to be replaced by the below provisioner

  provisioner "file" {
    source      = "${template_dir.config.destination_dir}"
    destination = "/etc/google-fluentd/config.d"
  }
}

I want to install StackDriver Logging Agent on these Debian/Ubuntu using Terraform to avoid SSH manually and install it every time I spin up an instance.

I tried using remote-exec but it didn't work for me. Below is the code for remote-exec:

provisioner "remote-exec" {
    inline = [
      "curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh",
      "bash install-logging-agent.sh",
    ]
  }

Putting the above code inside the google_compute_instance resource in my terraform script didn't work and ended up failing to connect after about 5 minutes with the following error:

* google_compute_instance.default:
 timeout - last error: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

I am not sure how to connect to the server to use remote-exec.


Solution

  • Here's a link to create the Terraform script:

    https://cloud.google.com/community/tutorials/getting-started-on-gcp-with-terraform

    Add the following metadata in order to install the Stackdriver Logging agent:

    metadata_startup_script = "curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh; sudo bash install-logging-agent.sh"
    

    Finally SSH into the instance and check the status of the service:

    $ sudo service google-fluentd status