Open In App

Amazon Web Services – Using Single SSH Key For all AWS Regions

Last Updated : 28 Mar, 2023
Like Article

Secure Shell also known as SSH is a cryptographic network protocol that helps secure network services over an unsecured network.  It securely helps users to log in to a server with SSH than using a password alone. SSH keys are nearly impossible to decipher by brute force alone unlike passwords.

In this article, we are going to look into how users can use a single SSH key pair for all their AWS regions.

To use a single SHH key pair for all your AWS regions follow the below steps:

For Linux:

Step 1: First generate a public SSH key from a private SSH key.

Step 2: Now import the key into each of your AWS regions. If you don’t have one already begin by creating an ssh key pair.

First, generate a public SSH key file from a private SSH key file.  Replace the key pair name with the name of your private .pem file. Make sure you are working on a bash shell and that the AWS command-line interface is configured with the user that has valid access.

Step 3: Run the following command to set the AWS regions:

AWS_REGIONS="aws ec2 describe-regions --query 'Regionsd[].RegionName'text)"

Step 4: Now run the following command to import the public SSH key into all the regions:

$ for each_region in ${AWS_REGIONS}; do aws ec2 import-key-pair --key-name MyKeyPair 
--public-key-material file://$HOME/.ssh/
--region $each_region; done

For Windows:

Step 1:  First, generate a public SSH key file from a private SSH key file. So,  open puttygen.

Step 2: Choose Load to load your private key file.

Step 3: Choose Save public key.

Step 4: Import the public ssh key into the desired AWS regions by running the following commands:

$PubFile = Get-Content .\ -raw
$Key = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($PubFile))
foreach $Region in (Get-AWSRegion).Region){Import-EC2KeyPair -keyName YOUR_PUBLIC_KEY 
  -PublicKeyMaterial $Key -Region $Region}

Note: replace YOUR_PUBLIC_KEY with your public ssh key file.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads