5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[JAWS-UG CLI] EC2:#1 キーペアの作成 (新規)

Last updated at Posted at 2015-07-05

AWS CLIを利用して、EC2のキーペアを新規に作成してみます。

前提条件

EC2への権限

  • EC2に対してフル権限があること。

AWS CLIのバージョン

  • 以下のバージョンで動作確認済

    • AWS CLI 1.10.44
    • AWS CLI 1.8.11
    • AWS CLI 1.7.39
    • AWS CLI 1.7.36
コマンド
aws --version
結果(例)
      aws-cli/1.10.44 Python/2.7.11 Darwin/15.5.0 botocore/1.4.34

バージョンが古い場合は最新版に更新しましょう。

コマンド
sudo -H pip install -U awscli

0.1. リージョンの決定

変数の設定
export AWS_DEFAULT_REGION='ap-northeast-1'

0.2. 変数の確認

変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile     ec2Full-prjZ-mbp13                env    AWS_DEFAULT_PROFILE
      access_key     ****************LOAQ shared-credentials-file
      secret_key     ****************I1O1 shared-credentials-file
          region           ap-northeast-1              env    AWS_DEFAULT_REGION
  1. 事前作業
    ===========

1.1. キーペア名の決定

今回のハンズオンでは、キーペア名にプロジェクト名と作業場所を埋め込みます。

変数の設定(例)
PRJ_NAME='handson'
変数の設定
EC2_KEY_PAIR_NAME="${PRJ_NAME}-${AWS_DEFAULT_REGION}-ec2" \
  && echo ${EC2_KEY_PAIR_NAME}

1.2. 同名キーペアの不存在確認

コマンド
aws ec2 describe-key-pairs \
  --key-names ${EC2_KEY_PAIR_NAME}
結果(例)
A client error (InvalidKeyPair.NotFound) occurred when calling the DescribeKeyPairs operation: The key pair 'prjz-us-west-2-ec2' does not exist

1.3. Keyファイル名の決定

変数の確認
FILE_SSH_KEY="${HOME}/.ssh/${EC2_KEY_PAIR_NAME}.pem" \
  && echo ${FILE_SSH_KEY}

1.4. Keyファイルの不存在確認

コマンド
ls ${FILE_SSH_KEY}
結果(例)
ls: /Users/aws/.ssh/prjz-us-west-2-ec2.pem: No such file or directory
  1. キーペアの作成
    ================
変数の確認
cat << ETX

        EC2_KEY_PAIR_NAME: ${EC2_KEY_PAIR_NAME}
        FILE_SSH_KEY:      ${FILE_SSH_KEY}

ETX
コマンド
aws ec2 create-key-pair \
  --key-name ${EC2_KEY_PAIR_NAME} \
  --query 'KeyMaterial' \
  --output text \
  > ${FILE_SSH_KEY} \
  && cat ${FILE_SSH_KEY} \
  && chmod 400 ${FILE_SSH_KEY}
結果(例)
-----BEGIN RSA PRIVATE KEY-----
MIIEoQIBAAKCAQEAoeEnI72MIR/mCwTI9qBrD8vMPpBB3Jal5VsUvF4WX8j9q9yy/5SuSnoVptziYhc8n389ZkbHZaBK/hgEcNNecSpYcYSYmrpzLDDs1DTtdlNcLtExA3n0RmXnp1dO5WRmIothbVDzB9tFWURj1z/o4N1N2hMer+tV/0qQdFIG/7XkzzalIGHbbE1q+ZUiwGEygTvbZEjR/BjvT4VKf+Yyp5KrX+ZGpLCntDWoecRZdc1hZE+3mA+mF8p5pDdHR8nNqMVvmBB6uhGqChGsDnDGnIoC1YrV4YrjyZ+ej7W6ljlSmEbk6U4VdTr+GKOq79i1A6rR53/CqAnca3KlGNwuRwIDAQABAoH/fJHGjLpQGy1w2QnVm/PWb1Fk2sAn7pf/EYAFdO7ySMNicd5j4EmJ1cYc40sW6mT3QcWoegv9ZTb0fpP81tXQ10S+ypw+0tKnzlnX5XTVRzjetyhDj2lJkOLaYOvOyrd34R1g275vRpx1wTvfmMSVcHTcc/RCqR1wEARRJTH6MMuH/MfzjZhMSGCyzEL1ZA0p2vzKQIkZj1cUZwsIA6FP+wIEydQXUBrX+GGx5NNq5jU5kvHXUhNaT+xlvIqAWSf/TSC38soPtIfww2EYKIza9bfdBQSwy+gcAp2I+tJcE8N6e7zJgPQ2jMsViiw+q3+42a1F1iab3fG43Iin1wjhAoGBAPkvgR9MBeakAUvPJebv8wpTyzcDmfOVhd0CLD8NdvPiz3Vr9LF1F0DkH9fnQbSXw14+ns1lWenhLkNBp0t24p0QiCdGOM3uh5KQnYfkCy38rrG6JaP9rlm7+AqAE8QNZCx/9ld3SgKblSph9UPqihuVJcBIXy/YOs6h9onWpOERAoGBAKZOb+rBdNcNl4RMRfdkjzkxJG7JhNPhdFobzrwaglArAZZhTLQwpOTEGNlPjzv3N1VMdJlI6Nb/rC8HrZAaHp50/ZTkBnGs4Z49QvIWERyChcezlvPBTGr5OGm0NF1TZKufLmoLsUYszpY0Zeksn/U965GaraOumk8DoR6r4JnXAoGBAIXWfgPRUVoPvKJ7IGBpgKCLYRqrVIUASmXqRNmY4csch1EKKuMlgNfZr0DHdQMKgGSePa0UQrbbu31kRfbCY/T4bCisDtaMx0RxaCsU0OxiS3IqCQ2Ls5l+3DrHj9Y9WxCLqkRQTfAYoIWjF5ji4ErwUNZ2CKop+WwCkeh24RgBAoGAe7HxijFT3zRr0uoMTELOSGKlz4Uxt4qESRVPwhcRf+FM7/X04D0iGElGljOXvKlWzantO6nADVHjVqOmU30p8v/fMKQdquVEdrboGTmShpsKOAv6pdjLJvGxJOX04MlLIl9mXVfPBfwDuDLG7hTT8MuacBUVO559hXGUoESE7McCgYBbI5bGY11Hgq3dv+48FnlxVdd8Yj5KKvkNrAeCwg4aGns+en0tBU1mCIwEUhlJQH5fFdvVlWT98zOzcy5NemNwbD8a6Po3AFi7aIt3J1GpZqmIKaCxGmlREs7bUDGal8N+g0723pGMQVD0mqbw7KTL+6juANXayjaYpFAOwGc8BA==
-----END RSA PRIVATE KEY-----
  1. 事後作業
    ===========

キーペアの確認

コマンド
aws ec2 describe-key-pairs \
  --key-names ${EC2_KEY_PAIR_NAME}
結果(例)
      {
        "KeyPairs": [
          {
              "KeyName": "prjz-us-west-2-ec2",
              "KeyFingerprint": "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
          }
        ]
      }

完了

5
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?