Terraform remote-exec provisioner not working using GCP provider

Hi,

I am referring to https://www.terraform.io/docs/provisioners/remote-exec.html#script-arguments to install the Nginx web server on the remote server.

cat main.tf
variable “region” {
default = “us-west1-a” // We’re going to need it in several places in this config
}
provider “google” {
credentials = “whcdrupalproject.json”
project = “whcdrupalproject”
region = “us-west1-a”
}
resource “google_compute_instance” “whcdrupal8” {
count = 1 // Adjust as desired
name = “whcdrupal8” // yields “test1”, “test2”, etc. It’s also the machine’s name and hostname
machine_type = “f1-micro” // smallest (CPU & RAM) available instance
zone = “us-west1-a” // yields “europe-west1-d” as setup previously. Places your VM in Europe
boot_disk {
initialize_params {
image = “centos-7”
}
}
network_interface {
network = “default”
access_config {
// Ephemeral IP - leaving this block empty will generate a new external IP and assign it to the machine
}
}
provisioner “file” {
source = “script.sh”
destination = “/tmp/script.sh”
}
provisioner “remote-exec” {
inline = [
“chmod +x /tmp/script.sh”,
“/tmp/script.sh args”,
]
}
}

I have the below script.sh file locally. I want it to be pushed to the remote server and execute it there.

#cat script.sh
#!/bin/bash
yum -y install nginx 

Port 22 is open and please find the below details.

$gcloud compute ssh tmobiledrupal8
No zone specified. Using zone [us-west1-a] for instance: [tmobiledrupal8].
Warning: Permanently added 'compute.7260763458670404915' (ECDSA) to the list of known hosts.
(reverse-i-search)`': ^C
[kaushalshriyan@tmobiledrupal8 ~]$ curl ifconfig.co
35.247.72.150
[kaushalshriyan@tmobiledrupal8 ~]$logout
Connection to 35.247.72.150 closed.
Kaushals-MacBook-Pro:~ kaushalshriyan$ nc -v 35.247.72.150 22
Connection to 35.247.72.150 port 22 [tcp/ssh] succeeded!
SSH-2.0-OpenSSH_7.4
$terraform apply "ghcdrupalproject.tfplan"
google_compute_instance.tmobiledrupal8[0]: Creating...
google_compute_instance.tmobiledrupal8[0]: Still creating... [10s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [20s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [30s elapsed]
google_compute_instance.tmobiledrupal8[0]: Provisioning with 'file'...
google_compute_instance.tmobiledrupal8[0]: Still creating... [40s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [50s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [1m0s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [1m10s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [1m20s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [1m30s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [1m40s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [1m50s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [2m0s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [2m10s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [2m20s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [2m30s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [2m40s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [2m50s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [3m0s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [3m10s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [3m20s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [3m30s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [3m40s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [3m50s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [4m0s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [4m10s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [4m20s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [4m30s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [4m40s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [4m50s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [5m0s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [5m10s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [5m20s elapsed]
google_compute_instance.tmobiledrupal8[0]: Still creating... [5m30s elapsed]

*Error: timeout - last error: dial tcp :22: connect: connection refused* 

I ran terraform apply “whcdrupalproject.tfplan” it did not run the script.sh file remotely instead it reported Error: timeout - last error: dial tcp :22: connect: connection refused :disappointed: Am I missing anything in the above main.tf file? Any help will be highly appreciated and thanks in advance. I look forward to hearing from you.

Best Regards,

Kaushal

Hi,

Checking in again if someone can pitch in for my earlier question to this forum? Thanks in Advance and I look forward to hearing from you.

Best Regards,

Kaushal