diff --git a/ec2-upload-keys-all.sh b/ec2-upload-keys-all.sh new file mode 100644 index 0000000..760c672 --- /dev/null +++ b/ec2-upload-keys-all.sh @@ -0,0 +1,27 @@ +#!/bin/bash +# I use an "Admin" profile that has all EC2 permissions +AWSPROFILE="Admin" +KEYFILE="/Users/USERNAME/.ssh/id_rsa.pub" + +# 1. check key file +if [ ! -f "${KEYFILE}" ] +then + echo "${KEYFILE} not found" + exit 1 +fi +KEYNAME=$(basename ${KEYFILE}) +KEYNAME="${KEYNAME%.pub}" + +# 2. use ec2 describe-regions to list available regions +REGIONS=$(aws ec2 describe-regions | jq -r '.Regions[].RegionName') + +# 3. Upload the key file to all the regions +for reg in $REGIONS +do + echo -n "${reg}: " + aws ec2 import-key-pair --profile "${AWSPROFILE}" \ + --region ${reg} --key-name ${KEYNAME} \ + --no-cli-pager --output text \ + --public-key-material "fileb://${KEYFILE}" +done +