<p>For learning purpose, I'm trying to install and setup my own Kubernetes Cluster on GCP.</p>
<p>I want to provision my instances on GCP with a bootstrap script.</p>
<p>Here is my <code>google_compute_instance</code> config</p>
<pre class="lang-json prettyprint-override"><code>resource &quot;google_compute_instance&quot; &quot;default&quot; {
    name = var.vm_name
    machine_type = &quot;f1-micro&quot;
    zone = &quot;europe-west1-b&quot;

    boot_disk {
        initialize_params {
            image = &quot;debian-cloud/debian-9&quot;
        }
    }

    network_interface {
        network = var.network
        access_config {
            // Include this section to give the VM an external IP address
        }
    }

    provisioner &quot;remote-exec&quot; {
        script = var.script_path
        connection {
            type        = &quot;ssh&quot;
            host        = var.ip_address
            user        = &quot;root&quot;
        }
    }

    tags = [&quot;node&quot;]
}
</code></pre>
<p>I have this issue when I do <code>terraform apply</code></p>
<blockquote>
<p>Error: Failed to open script 'sudo apt-get update</p>
<p>sudo apt-get install <br />
apt-transport-https <br />
ca-certificates <br />
curl <br />
gnupg-agent <br />
software-properties-common <br />
zsh <br />
vim</p>
<p>curl -fsSL <a href="https://download.docker.com/linux/debian/gpg" rel="nofollow noreferrer">https://download.docker.com/linux/debian/gpg</a> | sudo apt-key
add - sudo add-apt-repository \    &quot;deb [arch=amd64]
<a href="https://download.docker.com/linux/debian" rel="nofollow noreferrer">https://download.docker.com/linux/debian</a> \    $(lsb_release -cs) \<br />
stable&quot; sudo apt-get update &amp;&amp; sudo apt-get install docker-ce
docker-ce-cli containerd.io</p>
<p>curl -s <a href="https://packages.cloud.google.com/apt/doc/apt-key.gpg" rel="nofollow noreferrer">https://packages.cloud.google.com/apt/doc/apt-key.gpg</a> | sudo
apt-key add - cat &lt;&lt;EOF | sudo tee
/etc/apt/sources.list.d/kubernetes.list deb <a href="https://apt.kubernetes.io/" rel="nofollow noreferrer">https://apt.kubernetes.io/</a>
kubernetes-xenial main EOF sudo apt-get update sudo apt-get install -y
kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl ':
open sudo apt-get update</p>
<p>sudo apt-get install <br />
apt-transport-https <br />
ca-certificates <br />
curl <br />
gnupg-agent <br />
software-properties-common <br />
zsh <br />
vim</p>
<p>curl -fsSL <a href="https://download.docker.com/linux/debian/gpg" rel="nofollow noreferrer">https://download.docker.com/linux/debian/gpg</a> | sudo apt-key
add - sudo add-apt-repository \    &quot;deb [arch=amd64]
<a href="https://download.docker.com/linux/debian" rel="nofollow noreferrer">https://download.docker.com/linux/debian</a> \    $(lsb_release -cs) \<br />
stable&quot; sudo apt-get update &amp;&amp; sudo apt-get install docker-ce
docker-ce-cli containerd.io</p>
<p>curl -s <a href="https://packages.cloud.google.com/apt/doc/apt-key.gpg" rel="nofollow noreferrer">https://packages.cloud.google.com/apt/doc/apt-key.gpg</a> | sudo
apt-key add - cat &lt;&lt;EOF | sudo tee
/etc/apt/sources.list.d/kubernetes.list deb <a href="https://apt.kubernetes.io/" rel="nofollow noreferrer">https://apt.kubernetes.io/</a>
kubernetes-xenial main EOF sudo apt-get update sudo apt-get install -y
kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl :
no such file or directory</p>
</blockquote>
<p>All my instances are created on the cloud, It's seems to find the bootstrap script but it is showing this error.</p>
<p>What did I miss? Is there a better way to do it ?</p>
<p>Here is the script:</p>
<pre class="lang-sh prettyprint-override"><code>#bin/bash

sudo apt-get update

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common \
    zsh \
    vim

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository \
   &quot;deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable&quot;
sudo apt-get update &amp;&amp; sudo apt-get install docker-ce docker-ce-cli containerd.io

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat &lt;&lt;EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
</code></pre>

<p>You should provide the <a href="https://www.terraform.io/docs/provisioners/connection.html#private_key" rel="nofollow noreferrer">private_key</a> argument in the <code>connection</code> block of <code>remote-exec</code>.</p>
<pre><code>private_key - The contents of an SSH key to use for the connection. These can be loaded from a file on disk using the file function. This takes preference over the password if provided.
</code></pre>
<p>A sample block could be like this:</p>
<pre><code>  provisioner &quot;remote-exec&quot; {
        script = var.script_path
        connection {
            host     = var.ip_address
            type     = &quot;ssh&quot;      
            user     = &quot;root&quot;
            private_key = fileexists(&quot;/temp/private_key&quot;) ? file(&quot;/temp/private_key&quot;) : file(&quot;C:/private_key&quot;)             
        }
  }
</code></pre>
