Skip to content

Instantly share code, notes, and snippets.

@pacohope
Last active June 28, 2017 11:01
Show Gist options
  • Save pacohope/a62b7b28bfc6a4fda4e950581540c665 to your computer and use it in GitHub Desktop.
Save pacohope/a62b7b28bfc6a4fda4e950581540c665 to your computer and use it in GitHub Desktop.
Import a ssh keypair into EC2 in all AWS regions
#!/bin/bash
# I use an "Admin" profile that has all EC2 permissions
AWSPROFILE="Admin"
KEYNAME="my-aws-key"
KEYFILE="file:///Users/paco/.ssh/foo.pem"
# Create foo.pem via:
# sshkeygen -f /Users/paco/.ssh/foo.pub -e > /Users/paco/.ssh/foo.pem
# use ec2 describe-regions to get the official, up-to-the-minute region list.
REGIONS=$(aws ec2 describe-regions | jq -r '.Regions[].RegionName')
for i in $REGIONS
do
aws ec2 import-key-pair --profile "$AWSPROFILE" --region $i \
--key-name $KEYNAME --public-key-material "$KEYFILE"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment