Skip to content

Instantly share code, notes, and snippets.

@superseb
Last active July 25, 2019 15:42
Show Gist options
  • Save superseb/1244d86bf63cad4fbd4637ba0c55274c to your computer and use it in GitHub Desktop.
Save superseb/1244d86bf63cad4fbd4637ba0c55274c to your computer and use it in GitHub Desktop.
Add SSH keypair to all EC2 regions using awscli

Add SSH key pair to all regions using awscli

Based on https://fedoramagazine.org/ssh-key-aws-regions/

docker run -ti python bash
apt update && apt -y install jq
pip install awscli
# Need to set access key, secret key AND region in aws configure
aws configure
KEY_NAME=your-key-name
REGIONS=$(aws ec2 describe-regions | jq -r .Regions[].RegionName)
for REGION in $REGIONS; do echo $REGION; aws ec2 import-key-pair --key-name "${KEY_NAME}" --public-key-material file:///key-filename.pub --region $REGION; done
# Optional, Lightsail
for REGION in $REGIONS; do echo $REGION; aws lightsail import-key-pair --key-pair-name "${KEY_NAME}" --public-key-base64 "$(cat key-filename.pub)" --region $REGION; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment