aws_key_pair.kyc_app_public_key: Error import KeyPair: InvalidKey.Format: Key is not in valid OpenSSH public key format

I always get this error on my terraform. How do I fix this?

aws_key_pair.kyc_app_public_key: Error import KeyPair: InvalidKey.Format: Key is not in valid OpenSSH public key format

I already generated the ssh with this command ssh-keygen -t rsa -N "" -b 2048 -C "assignment"

Here is my configuration script on terraform

resource "aws_key_pair" "kyc_app_public_key" {
  key_name = "assignment"
  public_key ="//~/.ssh/id_rsa.pub"
}

Upvotes: 3

Views: 8324

Answers (1)

BMW
BMW

Reputation: 45223

So if you go through the official document, the public_key is the content of the public key file.

resource "aws_key_pair" "deployer" {
  key_name   = "deployer-key"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
}

If you want to reference the file name, more than content, use function file()

resource "aws_key_pair" "kyc_app_public_key" {
  key_name = "assignment"
  public_key = "${file("//~/.ssh/id_rsa.pub")}"
}

I am not 100% sure if works with your way //~/.ssh/id_rsa.pub, normally I copy the public key to local folder and reference as

public_key = "${file("${path.module}/id_rsa.pub")}"

Upvotes: 7

Related Questions