Skip to content

Error while using public and private keys to connect to AWS #6

@vkarry

Description

@vkarry

I am seeing following error, when terraform is trying to use private key to connect to instances in AWS. Please advise.

$ terraform plan

Error: aws_instance.example: 1 error(s) occurred:

  • aws_instance.example: file: open MyKey: no such file or directory in:

${file("${var.path_to_private_key}")}

Here is the snippet from instance.tf which is executing:
resource "aws_key_pair" "mykey" {
key_name = "MyKey"
public_key = "${file("${var.path_to_public_key}")}"
}

resource "aws_instance" "example" {
ami = "${lookup(var.amis,var.region)}"
instance_type = "t2.micro"
key_name = "${aws_key_pair.mykey.key_name}"
tags {
Name = "${var.instance_tag}"
}
provisioner "file" {
source = "script.sh"
destination = "/tmp/script.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/script.sh",
"sudo /tmp/script.sh"
]
}
connection {
user = "${var.instance_username}"
private_key = "${file("${var.path_to_private_key}")}"
}
}

And variables.tf:
variable "path_to_public_key" {
description = "Path to public key"
default = "MyKey.pub"
}

variable "path_to_private_key" {
description = "Path to private key"
default = "MyKey"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions