From e06063dc0a70277afd766645d9129fe54a15cca6 Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Thu, 29 Dec 2022 10:23:50 -0500 Subject: [PATCH] Initial commit --- ec2-upload-keys-all.sh | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ec2-upload-keys-all.sh 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 +