1

For learning purpose, I'm trying to install and setup my own Kubernetes Cluster on GCP.

I want to provision my instances on GCP with a bootstrap script.

Here is my google_compute_instance config

resource "google_compute_instance" "default" {
 name = var.vm_name
 machine_type = "f1-micro"
 zone = "europe-west1-b"
 boot_disk {
 initialize_params {
 image = "debian-cloud/debian-9"
 }
 }
 network_interface {
 network = var.network
 access_config {
 // Include this section to give the VM an external IP address
 }
 }
 provisioner "remote-exec" {
 script = var.script_path
 connection {
 type = "ssh"
 host = var.ip_address
 user = "root"
 }
 }
 tags = ["node"]
}

I have this issue when I do terraform apply

Error: Failed to open script '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 \ "deb [arch=amd64] https://download.docker.com/linux/debian \ $(lsb_release -cs) \
stable" sudo apt-get update && 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 <<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 ': open 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 \ "deb [arch=amd64] https://download.docker.com/linux/debian \ $(lsb_release -cs) \
stable" sudo apt-get update && 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 <<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 : no such file or directory

All my instances are created on the cloud, It's seems to find the bootstrap script but it is showing this error.

What did I miss? Is there a better way to do it ?

Here is the script:

#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 \
 "deb [arch=amd64] https://download.docker.com/linux/debian \
 $(lsb_release -cs) \
 stable"
sudo apt-get update && 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 <<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
asked Nov 25, 2020 at 18:20
4
  • 2
    You do not show the script. Does your script begin with this line #! /bin/bash? Are you defining your startup script correctly in the metadata: github.com/terraform-google-modules/… Commented Nov 25, 2020 at 18:32
  • I've just added the script @JohnHanley Commented Nov 25, 2020 at 18:53
  • @Kevin, I see ! missing in #bin/bash. Not sure, maybe.. Commented Nov 25, 2020 at 19:19
  • I have the same issue using #!/bin/bash Commented Nov 25, 2020 at 20:29

2 Answers 2

3

You should provide the private_key argument in the connection block of remote-exec.

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.

A sample block could be like this:

 provisioner "remote-exec" {
 script = var.script_path
 connection {
 host = var.ip_address
 type = "ssh" 
 user = "root"
 private_key = fileexists("/temp/private_key") ? file("/temp/private_key") : file("C:/private_key") 
 }
 }
answered Nov 25, 2020 at 19:09
Sign up to request clarification or add additional context in comments.

1 Comment

If he is getting Error: Failed to open script 'sudo apt-get update error, it's only after connecting to the machine.
1

For those who are interested, I have found an easier solution, without using ssh but by using the google metadata available at creation of the resource.

metadata_startup_script = file("./scripts/bootstrap.sh")

resource "google_compute_instance" "default" {
 name = var.vm_name
 machine_type = "e2-standard-2"
 zone = "europe-west1-b"
 boot_disk {
 initialize_params {
 image = "debian-cloud/debian-9"
 }
 }
 network_interface {
 network = var.network
 access_config {
 // Include this section to give the VM an external IP address
 }
 }
 metadata_startup_script = file("./scripts/bootstrap.sh")
 tags = ["node"]
}
answered Dec 6, 2020 at 19:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.