terraform-google-vm icon indicating copy to clipboard operation
terraform-google-vm copied to clipboard

File provisioner upload file and startup-script - not synchronised?

Open vfedoriv opened this issue 6 months ago • 3 comments

What I have: terraform code that deploys VM on GCP It contains a startup script

 metadata = {
   ssh-keys = "${var.ssh_username}:${var.ssh_public_key}"
   startup-script = templatefile("${path.module}/startup-script.sh", {..}

and this template has code

sudo tar -xvf "/tmp/${my_archive_name}"

that should unpack the file from /tmp dir, then put some files in another place.

those my_archive_name file should be uploaded by the file provisioner.

What I see:

Terraform apply executed successfully, and an instance was created. The file provisioner successfully uploaded the file in /tmp dir, but SOMETIME there are a few missing files (expected as the result of the unpack operation) If I manually unpack the archive from /tmp dir, files are present After I recalled that tar can unpack an archive even if it's incomplete, and missing files seem to be at the end of the archive, I added a delay before the unpack command to check if the arch file size is not changing Log results: Arch file size: 210403328 Arch file size: 235031211 Arch file size: 235031211

So it seems that or file provisioner "finished" before upload was fully completed (async?) or startup-script started prematurely

Expected behavior

startup-script should be executed after file provisioner completely uploaded the file

Observed behavior

startup-script starts before file provisioner completely uploaded the file

Terraform Configuration

resource "google_compute_instance" "my_vm" {
  provider     = google-beta
  count        = var.instance_count
  name         = "${var.vm_name}-${count.index + 1}"
  machine_type = var.vm_machine_type
  zone         = var.zone
  tags = ["my-vm-instance"]

  boot_disk {
    initialize_params {
      image = "${var.vm_image_project}/${var.vm_image_family}"
    }
  }

  attached_disk {
    source      = google_compute_disk.persistent_disk.id
    device_name = google_compute_disk.persistent_disk.name
    mode        = "READ_WRITE"
  }

  network_interface {
    network    = var.private_network
    subnetwork = var.private_subnet
    access_config {
      // Ephemeral public IP
    }
  }

  service_account {
    email = var.service_acc_email
    scopes = ["https://www.googleapis.com/auth/cloud-platform"]
  }

  metadata = {
    ssh-keys = "${var.ssh_username}:${var.ssh_public_key}"
    startup-script = templatefile("${path.module}/startup-script.sh", {
      postgres_user         = var.db_user,
      postgres_password     = var.db_password, postgres_db_name = var.db_name,
      postgres_host         = var.db_host, postgres_port = var.db_port,
      path_to_license_file  = var.dest_license_file_path,
      path_to_my_archive = var.local_my_archive_dir,
 my_archive_name = var.my_archive_name
    })

  }

  connection {
    type        = "ssh"
    user        = var.ssh_username
    private_key = file(var.ssh_private_key)
    host        = self.network_interface[0].access_config[0].nat_ip
    timeout     = "10m"
    agent       = false
  }

  provisioner "file" {
    source      = var.local_license_file
    destination = var.dest_license_file_path
  }

  provisioner "file" {
    source      = "${var.local_my_archive_dir}/${var.my_archive_name}"
    destination = "/tmp/${var.my_archive_name}"
  }

  # Ignore changes for persistent disk attachments
  lifecycle {
    ignore_changes = [attached_disk]
  }
}

Terraform Version

Terraform v1.4.6
on darwin_arm64
+ provider registry.terraform.io/hashicorp/google v5.41.0
+ provider registry.terraform.io/hashicorp/google-beta v5.41.0
+ provider registry.terraform.io/hashicorp/null v3.2.2

Additional information

No response

vfedoriv avatar Aug 22 '24 19:08 vfedoriv