Last Updated: February 25, 2016
·
666
· pilt

Upload SSH key to AWS using AWS CLI

Region needs to be specified if you want to upload the key to some other region than your default.

My ~/.aws/config looks like:

[default]
output = json
region = eu-west-1
aws_access_key_id = ...
aws_secret_access_key = ...

Command that uploads the primary key, assuming that the public key is available in ~/.ssh/my-precious.pub:

aws \
  --region us-east-1 \
  ec2 import-key-pair \
  --key-name my-precious \
  --public-key-material "$(cat ~/.ssh/my-precious.pub)"

Output, in JSON because of the config's format directive:

{
    "KeyName": "my-precious",
    "KeyFingerprint": "aa:bb:cc..."
}