Copying a Key Pair Generated by AWS to Another Region (with Screenshots)

I have an existing key pair that was generated via AWS Console. Since I do not want to create another set of Key Pair for the other regions, I would like to use the same Key Pair. Is it possible to copy the Key Pair to another Region? How can I do this?

Dany


Hi Dany, the short answer to your question is yes, it is possible to copy your existing AWS generated Key Pair to another region and even copy this to another AWS account.


The Challenge with AWS Generated Key Pairs

Generating the Key Pairs via AWS Console is easy, it gives you the Private Key and you can launch EC2 instances and associate it with your instance by adjusting the settings during EC2 Instance Launch. Then you can SSH to your EC2 Instance via the Private Key.

The issue here is the Public Key. AWS does not provide the Public Key during creation or any time after that.

Do not worry, we can still get the Public Key. It is not easy as clicking on the console then selecting copy to other region, but it is still doable.

See the steps below.


Step-by-step guide on copying a Key Pair to another region.

For this tutorial, I have created an AWS Key Pair in N. Virginia Region (us-east-1) – radishlogic_key.

The goal is to copy the Key Pair to Oregon Region (us-west-2).

Here are screenshots of my Key Pair.

Key Pair in AWS Console
Private Key

1. Retrieving the Private Key in N. Virginia Region (us-east-1)

Launch a temporary Linux EC2 Instance in where the Key Pair is located (us-east-1). Any Linux Image will do.

I chose to launch an Amazon Linux 2 image with t3a.nano Instance Type (this is currently the cheapest instance).

Security Group, only allow SSH (port 22) to the internet 0.0.0.0/0.

SSH into the instance.

ssh [email protected][IPv4 Public IP] -i [Private Key File]

Copy the contents of of the file ~/.ssh/authorized_keys. This is your Public Key.

cat  ~/.ssh/authorized_keys 

Create a file in your local computer/laptop for the Public Key.

In my case I named the file radishlogic_key.pub.

Note: The contents of the Public Key is only one long line.

Red: cat command via SSH
Green: Contents of ~/.ssh/authorized_keys
Contents of my Public Key in Sublime Text.

2. Importing the Key Pair to Oregon Region (us-west-2)

Now that we have our Public Key it is time to add it to our Key Pair to our Target Region. (Oregon, us-west-2)

To do this, go to the Key Pair EC2 Console in your target region.

Click on Action >> Import Key Pair.

Fill up Name. Usually, it is just the same name as your Key Pair from the region you are copying from.

Then Browse to your Public Key. (radishlogic_key.pub) This will then paste the contents of your public key to the text box below.

Then click Import key pair.

You have successfully copied the Key Pair to the Target Region.


You may now use the same Private Key (radishlogic_key.pem) when launching a new EC2 Instance in that Oregon Region.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.