Skip to content

Instantly share code, notes, and snippets.

@whiteley
Created June 19, 2013 22:16
Show Gist options
  • Save whiteley/5818628 to your computer and use it in GitHub Desktop.
Save whiteley/5818628 to your computer and use it in GitHub Desktop.
This script copies your keypair to any regions missing it based solely on the keypair name and not the fingerprint.
#!/bin/sh
set -o errexit
set -o nounset
usage() {
echo "Usage: ${0} keypair_name public_key_file"
exit 1
}
[ $# = 2 ] || usage
keypair=${1}
pubkey=${2}
regions=( $(ec2-describe-regions | awk '{print $2}') )
keypair_exists() {
ec2-describe-keypairs ${keypair} --region ${1} >/dev/null 2>&1
}
import_keypair() {
ec2-import-keypair ${keypair} --public-key-file ${pubkey} --region ${1} >/dev/null
}
for region in ${regions[@]}; do
if keypair_exists ${region}
then
echo "The keypair '${keypair}' already exists in region '${region}'"
else
import_keypair ${region}
echo "The keypair '${keypair}' was imported to region '${region}'"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment