Amit Joshi
Amit Joshi

Reputation: 57

Terraform apply fails with "Failed to read ssh private key: no key found"

I am using following terraform configuration on windows-10 Terraform v1.0.4 on windows_amd64

I am trying to install some packages on ec2 instance using provisioner "remote-exec". In this when i supply private_key in th connection block, i am getting following error message

         Failed to read ssh private key: no key found 

This is how my remote-exec and connection block look like

                    terraform {
                    required_providers {
                        aws = {
                        source  = "hashicorp/aws"
                        version = "~> 3.0"
                        }
                    }
                    }
                    resource "aws_instance" sandbox {
                        ami =           "ami-0ff338189efb7ed37"
                        instance_type =     "t3.micro"
                        tags    = {
                            Name =      "sandbox"
                            Description = "sandbox server"
                        }
                            provisioner "remote-exec" {
                            inline = [ "sudo apt update",
                                        "sudo apt install ansible -y" 
                                    ]
                        }
                            connection {
                            type = "ssh"
                            host = self.public_ip
                            user = "ubuntu"
                            private_key = file("C:\\Users\\asdfsd\\Downloads\\asdfsd-ubuntu.pem")
                        }
                        key_name  = aws_key_pair.sandbox_key.id
                        vpc_security_group_ids = [aws_security_group.ssh_access.id]
                    }
                    resource "aws_key_pair"  "sandbox_key" {
                        public_key = file("C:\\Users\\asdfsd\\Downloads\\asdfsd-ubuntu-public.pem")
                    }

I tried referring to other similar threads on stackoverflow, but they talk about some ${module.path}. I am not sure what this path refers to. Therefore, that solution does not work for me.

Does anyone face similar problem? Any help is much appreciated.

In addition, I tried reading contents of the private key and pass it as a text to the connection block but it did not work either.

         locals {
             key_data = file("C:\\Users\\asdfasdf\\Downloads\\asdffa-ubuntu.pem")
         }
         .
         .
         .
         private_key = local.key_data
         .
         .


Best regards,
Amit Joshi.

Upvotes: 0

Views: 3006

Answers (1)

Johnny9
Johnny9

Reputation: 496

On my ubuntu system I am refering to the key without the .pem extension.

I faced the same problem when I tried with .pem at the end. I don't know if that'll fix the problem since you are on windows but you can try it.

Also try to move the key to the same directory where your tf code is.

Upvotes: 0

Related Questions