I could create the key pair myKey to AWS with Terraform.
resource "tls_private_key" "pk" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "kp" {
key_name = "myKey" # Create a "myKey" to AWS!!
public_key = tls_private_key.pk.public_key_openssh
}
AWS:
But I couldn't download the myKey.pem file. Is it possible to download the myKey.pem file with Terraform like below?


Feb, 2022 Update:
No, it's not possible to download the
myKey.pemfile withTerraform. Instead, we can create themyKey.pemfile which has the sameprivate keyas the key pairmyKeyonAWS. So the createdmyKeyandmyKey.pemfile byTerraformare the same as those which we manually create and download onAWS. This is the code below. (I used Terraform v0.15.4)Don't forget to make
myKey.pemfile only readable by you running the code below before ssh to your ec2 instance.Otherwise the error below occurs.