Trouble with Terraform Provisioner File: Copying File to GCP VM

50 Views Asked by At

I'm setting up a VM in Google Cloud Platform (GCP) via Terraform and transferring a service account to it using a file. I've utilized the the provisioner file to copy the file from source to destination. While my execution is successful, I'm puzzled as to why I don't see any files being created. What could be the reason for this?

I'm also generating SSH keys via Terraform and storing both private and public keys in a designated directory

resource "tls_private_key" "ssh_key" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

output "private_key" {
  value = tls_private_key.ssh_key.private_key_pem
}

output "public_key" {
  value = tls_private_key.ssh_key.public_key_openssh
}

resource "local_file" "private_key_file" {
  content  = tls_private_key.ssh_key.private_key_pem
  filename = var.private_key_file_path
}

resource "local_file" "public_key_file" {
  content  = tls_private_key.ssh_key.public_key_openssh
  filename = var.public_key_file_path
}

/*
 * Crate a VM
*/
resource "google_compute_instance" "vm_instance" {
  name         = var.vm_name
  machine_type = var.machine_type
  zone         = var.zone

  network_interface {
    network    = google_compute_network.tf_vpc_network.id
    subnetwork = google_compute_subnetwork.tf_vpc_subnetwork.id

    access_config {
      // Ephemeral public IP
    }
  }

  boot_disk {
    initialize_params {
      image = var.image
    }
  }

  metadata = {
    ssh-keys = "${var.provisioner_connection_user}:${file(local_file.public_key_file.filename)}"
  }

  metadata_startup_script = file(var.startup_script_path)

  provisioner "file" {
    source      = var.service_account_source      // "path/to/your/credentials.json"
    destination = var.service_account_destination // "/tmp/gcp/credentials.json"
  }
  connection {
    type        = var.provisioner_connection_type
    user        = var.provisioner_connection_user
    host        = self.network_interface.0.access_config.0.nat_ip
    private_key = file(var.private_key_file_path)
  }
}

below are my values from terraform.tfvars file

vm_name                     = "bc-binance-trds-usdm-vm"
zone                        = "europe-west1-b" # Choose any zone in europe-west1
machine_type                = "e2-medium"
image                       = "ubuntu-os-cloud/ubuntu-2004-lts"
network_name                = "vpc-network"
private_key_file_path       = "../../../../credentials/private_key.pem"
public_key_file_path        = "../../../../credentials/public_key.pub"
startup_script_path         = "./modules/create-vm/startup-script.sh"
provisioner_connection_type = "ssh"
provisioner_connection_user = "terraform"
service_account_source      = "../../../../credentials/credentials.json"
service_account_destination = "/tmp/gcp/credentials.json"

The Terraform execution completes successfully

enter image description here

I'm not noticing the creation of any folders within my VM

enter image description here

after ls -all enter image description here

after la -l /tmp enter image description here

0

There are 0 best solutions below