Last Updated: February 25, 2016
· 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:

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/

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

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

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