View a markdown version of this page

Use ImportKeyPair with a CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use ImportKeyPair with a CLI

The following code examples show how to use ImportKeyPair.

CLI
AWS CLI

To import a public key

The following import-key-pair example imports a public key. First, generate a key pair with the tool of your choice. For example, use the following ssh-keygen command.

ssh-keygen -t rsa -C "my-key" -f ~/.ssh/my-key

The ssh-keygen command displays output similar to the following:

Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/ec2-user/.ssh/my-key. Your public key has been saved in /home/ec2-user/.ssh/my-key.pub. ...

The following command imports the specified public key.

aws ec2 import-key-pair \ --key-name "my-key" \ --public-key-material fileb://~/.ssh/my-key.pub

Output:

{ "KeyName": "my-key", "KeyFingerprint": "1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca" }

For more information, see Create a key pair for your Amazon EC2 instance in the Amazon EC2 User Guide.

  • For API details, see ImportKeyPair in AWS CLI Command Reference.

PowerShell
Tools for PowerShell V4

Example 1: This example imports a public key to EC2. The first line stores the contents of the public key file (*.pub) in the variable $publickey. Next, the example converts the UTF8 format of the public key file to a Base64-encoded string, and stores the converted string in the variable $pkbase64. In the last line, the converted public key is imported to EC2. The cmdlet returns the key fingerprint and name as results.

$publickey=[Io.File]::ReadAllText("C:\Users\TestUser\.ssh\id_rsa.pub") $pkbase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($publickey)) Import-EC2KeyPair -KeyName Example-user-key -PublicKey $pkbase64

Output:

KeyFingerprint KeyName -------------- ------- do:d0:15:8f:79:97:12:be:00:fd:df:31:z3:b1:42:z1 Example-user-key
  • For API details, see ImportKeyPair in AWS Tools for PowerShell Cmdlet Reference (V4).

Tools for PowerShell V5

Example 1: This example imports a public key to EC2. The first line stores the contents of the public key file (*.pub) in the variable $publickey. Next, the example converts the UTF8 format of the public key file to a Base64-encoded string, and stores the converted string in the variable $pkbase64. In the last line, the converted public key is imported to EC2. The cmdlet returns the key fingerprint and name as results.

$publickey=[Io.File]::ReadAllText("C:\Users\TestUser\.ssh\id_rsa.pub") $pkbase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($publickey)) Import-EC2KeyPair -KeyName Example-user-key -PublicKey $pkbase64

Output:

KeyFingerprint KeyName -------------- ------- do:d0:15:8f:79:97:12:be:00:fd:df:31:z3:b1:42:z1 Example-user-key
  • For API details, see ImportKeyPair in AWS Tools for PowerShell Cmdlet Reference (V5).