Reputation: 8220
I am trying to remotely execute some commands on a freshly provisioned Google Cloud Platform Compute Engine VM using ssh keys with Terraform. Here's my code:
resource "tls_private_key" "ssh-key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "google_compute_instance" "static-content" {
# ...
metadata {
sshKeys = "root:${tls_private_key.ssh-key.public_key_openssh}"
}
connection {
type = "ssh"
user = "root"
private_key = "${tls_private_key.ssh-key.private_key_pem}"
}
provisioner "remote-exec" {
inline = [
"curl -L https://github.com/aelsabbahy/goss/releases/download/v0.3.6/goss-linux-amd64 -o ~/goss",
"chmod +x ~/goss",
"~/goss -g ~/gossfile.yml validate",
]
}
}
The output I get in the Terraform apply is
google_compute_instance.static-content: Still creating... (2m10s elapsed)
google_compute_instance.static-content (remote-exec): Connecting to remote host via SSH...
google_compute_instance.static-content (remote-exec): Host: 35.198.166.131
google_compute_instance.static-content (remote-exec): User: root
google_compute_instance.static-content (remote-exec): Password: false
google_compute_instance.static-content (remote-exec): Private key: true
google_compute_instance.static-content (remote-exec): SSH Agent: false
google_compute_instance.static-content (remote-exec): Checking Host Key: false
So it seems like the ssh key is not properly propagated to the VM. Any hints why this doesn't work?
Upvotes: 3
Views: 3336
Reputation: 196
it seems like you just tried it in a different way, give it a try with below code which worked for me
provisioner "remote-exec" {
connection {
type = "ssh"
port = 22
user = "username"
agent = "false"
private_key = "${file("/path/to/your/pem_file")}"
}
inline = [
"your command goes here",
]
}
}
Upvotes: 1