Skip to content

Instantly share code, notes, and snippets.

@larryebaum
Created July 20, 2020 19:12
Show Gist options
  • Save larryebaum/7a59e88162287504777fd079e1073b50 to your computer and use it in GitHub Desktop.
Save larryebaum/7a59e88162287504777fd079e1073b50 to your computer and use it in GitHub Desktop.
Upload SSH Key to AWS Regions
#!/bin/bash
## Specify as the 1st parameter the keyname to display in AWS;
## use 1st param or current user
aws_keypair_name="${1:-$USER}"
## Specify as the 2nd parameter the full path to ssh public key file name to upload to AWS;
## use 2nd param or default id_rsa.pub will be used.
publickeyfile="${2:-$HOME/.ssh/id_rsa.pub}"
keydata=$(cat $publickeyfile | base64)
regions=$(aws ec2 describe-regions \
--output text \
--query 'Regions[*].RegionName')
for region in $regions; do
echo $region
aws ec2 import-key-pair \
--region "$region" \
--key-name "$aws_keypair_name" \
--public-key-material "$keydata"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment