How to create aws ec2 private-public key pair
Asked Answered
J

3

5

I'm following this guide of creating aws environment. Now after I created my environment I want to ssh to the ec2.

What I need is to create private-public key pair, which I don't know how.

at the beginning of the guide, it tells:

Generate public key from private key
ssh-keygen -y -f ~/.ssh/pemfile/mumbai.pem

But how I create a mumbai.pem file on my host? Is there a command to download create this pem, or I need to download it from aws? I'm really new with aws, I hope this is not too obvious.

Junkojunkyard answered 27/3, 2019 at 23:30 Comment(2)
If you are using Windows, use Putty and Pageant. Linux, that command will probably work.Monitor
But, when you create the ec2 instance, it gives you the key though. Make sure port 22 is open through VPC for SSH. 3389 for RDP. So, you don’t need to make a key. It will give you one, you just need to remember to save it.Monitor
S
7

Just run ssh-keygen and it should prompt you for details on where to create the key. Just note: If you run this command on your local machine, it will generate both the public key and the private key. In this case, you will need to Import Your Own Public Key to Amazon EC2. This method works better for terraform as you can put the text value output of your public key into the aws_key_pair resource easily.

If you create the key via the ec2 console, AWS will keep the public key in the system automatically and your browser will download the private key. See Creating a Key Pair Using Amazon EC2. (this second approach will save you having to upload it to ec2 keypairs). This method also works with the aws_key_pair resource, however you'll have to import the existing resource into terraform. It's simpler to use the first approach.

If you're doing it all via terraform, check out aws_key_pair

Satterlee answered 28/3, 2019 at 5:19 Comment(0)
F
4

To generate the private key run:

ssh-keygen -m PEM -f key.pem

The public key

ssh-keygen -y -f key.pem > key.pem.pub

If you want to import it manually via aws cli to a Key Pair called AwsKeyName type:

aws ec2 import-key-pair --key-name AwsKeyName \ 
     --public-key-material $(openssl enc -base64 -A -in key.pem.pub)

To create the Pair on the console go to EC2 and in the Key Pairs click Import. Then paste the contents in the public key file (key.pem.pub)

Fidelafidelas answered 26/7, 2020 at 20:41 Comment(0)
D
1

First of all, it may be too much if you're new to AWS The tutorial you're using equiped servers with Terraform, which is a 3rd party tool out of AWS

You may consider a much more intuitive turtorial to create your first instance from AWS console, and AWS will help to generate a key-pair, and you will have the full control

In the other hand, this article is an advaced one, it's trying to automate all infra work including instance creation, network and etc. It's useful but may be too complicated to follow

So back to your question, TF will inject the public key generated based on mumbai.pem, into the new server created in this code snippet:

# Define SSH key pair for our instances
resource "aws_key_pair" "default" {
  key_name = "mumbai"
  public_key = "${file("${var.key_path}")}"
}

It's not too obivious as the author is so familiar with TF and he skips the basic part

Dicker answered 28/3, 2019 at 3:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.