Automatically import your keys to all EC2 Regions

by Alexander Weiß

The EC2 cloud from Amazon uses Key Pairs for authentication. You can use the Key Pairs which Amazon creates for you, or generate them by yourself. No matter which of the two options you choose, the Key Pair is only available in one EC2 Region. Importing them manually is tedious, so it’s better to automate the process.

Here is a simple script which imports the specified Key Pair into each EC2 Region.

key_pair_name=”my Key Pair name”
public_key=$HOME/.ssh/id_rsa.pub

regions=$(ec2-describe-regions | cut -f2)

for region in $regions; do
echo $region
ec2-import-keypair --region $region --public-key-file $public_key $key_pair_name
done

You have to change only two things, the value of the variables “key_pair_name” and “public_key”. I highly recommend that you give the Key Pair a meaningful name. The “public_key” variable should point to the public key file you want to use for your EC2 Key Pair. If the public key file lies in the default location you don’t have to change anything here.

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)
Automatically import your keys to all EC2 Regions, 10.0 out of 10 based on 1 rating