Sparkler
Sparkler

Reputation: 2913

Send cloud-init script to gcp with terraform

How do I send cloud-init script to a gcp instance using terraform?

Documentation is very sparse around this topic.

Upvotes: 5

Views: 2884

Answers (1)

Sparkler
Sparkler

Reputation: 2913

You need the following:

A cloud-init file (say 'conf.yaml')

#cloud-config

# Create an empty file on the system
write_files:
- path: /root/CLOUD_INIT_WAS_HERE

cloudinit_config data source

gzip and base64_encode must be set to false (they are true by default).

data "cloudinit_config" "conf" {
  gzip = false
  base64_encode = false

  part {
    content_type = "text/cloud-config"
    content = file("conf.yaml")
    filename = "conf.yaml"
  }
}

A metadata section under the google_compute_instance resource

  metadata = {
    user-data = "${data.cloudinit_config.conf.rendered}"
  }

Upvotes: 10

Related Questions