16

Following terraform best practice for bootstrapping instances, I'm working on a cloud-init config in order to bootstrap my instance. My only need is to install a specific package.

My terraform config looks like this:

resource "google_compute_instance" "bastion" {
  name         = "my-first-instance"
  machine_type = "n1-standard-1"
  zone         = "europe-west1-b"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    network = "default"

    access_config {
      // Ephemeral IP
    }
  }

  metadata = {
    ssh-keys = "eugene:${file("/Users/eugene/.ssh/id_rsa.pub")}"
    user-data = file("my_cloud_init.conf")
  }
}

Following example for installing packages from cloud-init docs, here's the contents of my_cloud_init.conf:

#cloud-config

packages:
 - kubectl

After running terraform plan -out myplan and terraform apply myplan, I ssh onto the node only to find kubectl not available. Moreover, there's no evidence that cloud-init was run or that it exists on the node:

$ which -a cloud-init
$ cat /var/log/cloud-init
cat: /var/log/cloud-init: No such file or directory

Looking for clues about usage of cloud-init with Google Cloud Compute instances wasn't fruitful:

  • "Google Cloud Engine" page from cloud-init docs suggests settings user-data to a cloud-init config should be enough,
  • I see a cloud-init tutorial, but it's for Container Optimized OS,
  • there are some clues about cloud-init on other images, but nothing indicates cloud-init is available on debian-cloud/debian-9,
  • there's "Running startup scripts", but it has no mention of cloud-init.

I don't mind using another image, as long as it's Debian or Ubuntu and I don't have to make an image template myself.

How to use cloud-init with a debian-based image on Google Cloud? What am I missing?

oldhomemovie
  • 14,621
  • 13
  • 64
  • 99
  • I don't have to work on kubectl, but also need docs on cloud-init for GCP and here's what I found: A [request](https://github.com/hashicorp/terraform-provider-google/issues/1816) to add it to docs, then the [open MR](https://github.com/hashicorp/terraform-provider-template/pull/35) in official docs on hashicorp website, and that's how I found the [incomplete documentation](https://github.com/hashicorp/terraform-provider-template/blob/master/website/docs/d/cloudinit_config.html.markdown) for that. Unfortunately it's still an AWS-specific doc, but just set base64 and gziping to `false`. Helpful? – boldnik Oct 05 '20 at 11:37

2 Answers2

5

cloud-init is installed on the latest (at the moment of writing) Ubuntu 18.04 LTS (ubuntu-1804-bionic-v20191002) image :

<my_user>@instance-1:~$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.3 LTS"

<my_user>@instance-1:~$ which cloud-init
/usr/bin/cloud-init

You should replace debian-cloud/debian-9 with ubuntu-os-cloud/ubuntu-1804-bionic-v20191002.

norbjd
  • 10,166
  • 4
  • 45
  • 80
  • Thank you, @norbjd! Did you know this from experience, or were you able to look it up just now? The reason I am asking is because googling failed me, and I want to understand how to properly search for it next time. – oldhomemovie Oct 05 '19 at 13:38
  • I just checked by starting an Ubuntu instance :) Sadly, I don't know how to find what packages are installed on a specific public image without trying. – norbjd Oct 06 '19 at 09:56
5

To complement answer from @norbjd, posting this for completeness:

oldhomemovie
  • 14,621
  • 13
  • 64
  • 99