Error Error import KeyPair InvalidKey Format Key is not in valid OpenSSH public key format status code 400 request id 7a16e6f1-5e30-485f-babf-a54e1b2e593e

0 votes

Hi Guys,

I am trying to create one key in AWS using Terraform. But it is showing me the below error.

Error: Error import KeyPair: InvalidKey.Format: Key is not in valid OpenSSH public key format
status code: 400, request id: 7a16e6f1-5e30-485f-babf-a54e1b2e593e

I have used this below configuration file.

provider "aws" {
  region = "ap-south-1"
  profile = "nadimpro"
}
resource "aws_key_pair" "deployer" {
  key_name   = "deployer-key"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 email@example.com"
}
Jun 10, 2020 in Terraform by akhtar
• 38,170 points
1,470 views

1 answer to this question.

0 votes

Hi@akhtar,

You have to pass your public key in a proper format. When importing an existing key pair the public key material may be in any format supported by AWS. Supported formats are:

  • OpenSSH public key format (the format in ~/.ssh/authorized_keys)

  • Base64 encoded DER format

  • SSH public key file format as specified in RFC4716

But you can create your key directly without passing any public key. You can use the below-given code in your file.

variable "key_name" {default="my-key"}
resource "tls_private_key" "example" {
  algorithm = "RSA"
  rsa_bits  = 4096
}
resource "aws_key_pair" "generated_key" {
  key_name   = "${var.key_name}"
  public_key = "${tls_private_key.example.public_key_openssh}"
}
answered Jun 10, 2020 by MD
• 95,060 points

Related Questions In Terraform

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to import module in Terraform code?

Hi@akhtar, A module is a container for multiple resources that ...READ MORE

answered Jul 21, 2020 in Terraform by MD
• 95,060 points
565 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
953 views
+2 votes
1 answer