Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Terraform Error: Key is not in valid OpenSSH public key format

Code

==================================

provider "aws" {
  region     = "ap-south-1"
  access_key = ""
  secret_key = ""
}

resource "aws_key_pair" "terraform-demo" {
  key_name   = "terraform-demo"
  public_key = "file(/home/ec2-user/.ssh/authorized_keys)"
}

Error

Error: Error import KeyPair: InvalidKey.Format: Key is not in valid OpenSSH public key format
        status code: 400, request id: fa4b1c85-4372-467a-a228-83a159a5d12b

  on check.tf line 7, in resource "aws_key_pair" "terraform-demo":
   7: resource "aws_key_pair" "terraform-demo" {

Solutions:

Try converting the key to an openssh format using the -f flag. 
ssh-keygen -i -f /home/ec2-user/.ssh/authorized_keys > mykey-openssh.pub # NOT WORKING
ssh-keygen -f terraform_ec2_key # NOT WORKING
ssh-keygen -i -f terraform_ec2_key.pub > terraform_ec2_key1.pub # NOT WORKING

WORK IS IN PROGRESS
Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)