I have created ssh_key by
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f terraform_ec2_key
and put that terraform_ec2_key
in my terraform file and it looks like this
provider "aws" {
region = "us-west-2"
}
resource "aws_key_pair" "terraform_ec2_key" {
key_name = "terraform_ec2_key"
public_key = "${file("terraform_ec2_key.pub")}"
}
resource "aws_instance" "myfirst" {
ami = "ami-830c94e3"
instance_type = "t2.micro"
key_name = aws_key_pair.terraform_ec2_key.key_name
tags = {
name = "terraformec2"
}
}
and when i did terraform apply
command it was apllied succesfully and the instance was created but when i connect to the instance using connect command provided by aws
ssh -i terraform_ec2_key [email protected]
I am getting [email protected]: Permission denied (publickey).
Here what I have tried chmod 400 terraform_ec2_key
but not working also open port 22
from inbound rule but still i am getting same error is there anything am I missing?
CodePudding user response:
Based on the comments.
The code is perfectly fine in itself. The issue was caused by wrong ami-id
. Instead of ubuntu
AMI, other was used, for example, amazon linux 2
. This will result in the Permission denied (publickey)
as amazon linux 2
uses ec2-user
user, not `ubuntu.