Terraform version 0.14.x fails to authenticate over SSH when using remote-exec
or null_resource
with EC2 instances running Amazon Linux 2023 (AL2023). The same code works fine with Amazon Linux 2 and CentOS-based images.
Error: timeout - last error: SSH authentication failed (ec2-user@<IP>:22):
ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
Even though the correct private key is specified and verified with
"ssh -i ~/.ssh/my_key.pem ec2-user@<IP>
# Login successful
"
Here is my code sample
resource "null_resource" "print_hostname" {
provisioner "remote-exec" {
inline = [
"echo 'Hostname is: $(hostname)'"
]
connection {
type = "ssh"
user = "ec2-user"
private_key = file("~/.ssh/my_key.pem")
host = "<IP_ADDRESS>"
timeout = "2m"
}
}
triggers = {
always_run = timestamp()
}
}
Note: Key format and permission are good.