Help us understand the problem. What is going on with this article?

aws cliをつかって全リージョンにssh公開鍵をimportする

More than 3 years have passed since last update.

EC2にログインするときにsshの鍵を使う。通常はAWSに鍵をつくらせて、秘密鍵をダウンロードする。この方法は楽な方法ではあるのだが、全リージョン別の鍵になってしまうので時に面倒という問題がある。そんなときに楽なのは手元にある鍵をimportしてしまう方法。

鍵の作成とJSONの準備

  1. ssh-keygenつかって公開鍵と秘密鍵を作成
  2. 作成した鍵を含むJSONを作成
sshkey.json
{
    "DryRun": true, 
    "KeyName": "mykey", 
    "PublicKeyMaterial": "ssh-rsa AAAA.....(略) user@local"
}

という具合になる。PublicKeyMaterialには、作成した公開鍵をそのままcatかなんかでいれればOK.

いざ登録

aws ec2 import-key-pair --cli-input-json file://sshkey.json --no-dry-run
という具合で登録すればOK.

全リージョンまとめて登録

まとめて登録するために楽をすることにして、aws ec2 describe-regionsをつかったスクリプトがこんな具合

aws-ssh-import.sh
#!/bin/bash
aws ec2 describe-regions |jq '.Regions | .[].RegionName' | while read line
do
    hoge=`echo $line | cut -d"\"" -f2 | cut -d"\"" -f1`
    aws ec2 import-key-pair --cli-input-json file://sshkey.json --no-dry-run --region $hoge
done
ar1
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした