'GCP Terraform file provisioner not able to connect to VM
I generated public and private key with the below command from terraofrm machine from where I am executing terraform code. ssh-keygen -t rsa -f ~/terraform_projects/vm-module/gcloud_instance1 -C user1 gcloud_instance - private key gcloud_instance.pub - public key
Terraform code.
variable "user" {
description = "user for ssh"
type = string
default = "user1"
}
variable "privatekey_path" {
description = "privatekey_path"
type = string
default = "~/terraform_projects/vm-module/gcloud_instance"
}
variable "publickey_path" {
description = "publickey_path"
type = string
default = "~/terraform_projects/vm-module/gcloud_instance.pub"
}
resource "google_compute_instance" "vm" {
....
....
metadata = {
ssh-keys = "${var.user}:${file("${var.publickey}")}"
}
}
resource "null_resource" "cluster" {
provisioner "file" {
source = "${path.module}/../scripts"
destination = "/home/${var.user}/"
}
connection {
host = google_compute_instance.vm.network_interface.0.access_config.0.nat_ip
type = "ssh"
user = "${var.user}"
private_key = file("${var.privatekey_path}")
timeout = "2m"
}
}
I have added public key to the vm machine from the metadata . Verified from console public key added successfully.
I am able to login to vm from terraform machine using ssh -i gcloud_instance [email protected] I ip address in the error is correct ip of the vm.
Getting below error while running the null_resource. If I dont add timeout property it takes 5 mins , and then fails with same error. I tried many things but didnt work . Not able to find where is the issue , can someone please help.
Error
module.virtual_machine.null_resource.cluster: Creating...
module.virtual_machine.null_resource.cluster: Provisioning with 'file'...
module.virtual_machine.null_resource.cluster: Still creating... [10s elapsed]
module.virtual_machine.null_resource.cluster: Still creating... [20s elapsed]
module.virtual_machine.null_resource.cluster: Still creating... [30s elapsed]
module.virtual_machine.null_resource.cluster: Still creating... [40s elapsed]
module.virtual_machine.null_resource.cluster: Still creating... [50s elapsed]
module.virtual_machine.null_resource.cluster: Still creating... [1m0s elapsed]
module.virtual_machine.null_resource.cluster: Still creating... [1m10s elapsed]
module.virtual_machine.null_resource.cluster: Still creating... [1m20s elapsed]
module.virtual_machine.null_resource.cluster: Still creating... [1m30s elapsed]
module.virtual_machine.null_resource.cluster: Still creating... [1m40s elapsed]
module.virtual_machine.null_resource.cluster: Still creating... [1m50s elapsed]
module.virtual_machine.null_resource.cluster: Still creating... [2m0s elapsed]
╷
│ Error: file provisioner error
│
│ with module.virtual_machine.null_resource.cluster,
│ on compute-vm/main.tf line 152, in resource "null_resource" "cluster":
│ 152: provisioner "file" {
│
│ timeout - last error: SSH authentication failed ([email protected]:22): ssh: handshake failed: ssh:
│ unable to authenticate, attempted methods [none publickey], no supported methods remain
╵
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
