Vincent furtado
Vincent furtado

Reputation: 51

how to copy local file to azure vm through terraform?

I am trying to create a VM in Azure using terraform and copy a file from local machine to the remote virtual machine on azure.


    os_profile {
    computer_name = "pdemo"
    admin_username = "ubuntu"
  }
  os_profile_linux_config {
    disable_password_authentication = true
    ssh_keys {
      key_data = "ssh-rsa ********************************** "
      path = "/home/ubuntu/.ssh/authorized_keys"
    }
  }
  provisioner "file" {
    connection {
      type = "ssh"
      user = "ubuntu"
      host = azurerm_public_ip.terraform-PUBLIC-IP.ip_address
      private_key = file("/home/ubuntu/.ssh/id_rsa")
    }
    source = "/home/ubuntu/.ssh/terraform.pub"
    destination = "/home/ubuntu/.ssh/terraform.pub"
  }
}

It gives an error:

azurerm_virtual_machine.terraform-app-VM: Still creating... [1m30s elapsed]
azurerm_virtual_machine.terraform-app-VM: Still creating... [1m40s elapsed]
azurerm_virtual_machine.terraform-app-VM: Still creating... [6m20s elapsed]
azurerm_virtual_machine.terraform-app-VM: Still creating... [6m30s elapsed]

Error: timeout - last error: SSH authentication failed (ubuntu@:22): ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain


Upvotes: 2

Views: 2500

Answers (1)

AmanGarg-MSFT
AmanGarg-MSFT

Reputation: 1153

Please check the private key that you are using as the error shows authentication issues.

Also add agent=false like this:

provisioner "file" {
    connection {
      type = "ssh"
      user = "ubuntu"
      host = azurerm_public_ip.terraform-PUBLIC-IP.ip_address
      private_key = file("/home/ubuntu/.ssh/id_rsa")
      agent    = false
      timeout  = "10m"
    }
    source = "/home/ubuntu/.ssh/terraform.pub"
    destination = "/home/ubuntu/.ssh/terraform.pub"
  }

Upvotes: 2

Related Questions