ハンズオン(簡易版): EC2基礎(AMI)

1. EC2インスタンスへのSSHログイン (handson-cli-ec2-ami-builder-instance)

手順の目的 [why]

EC2インスタンス"handson-cli-ec2-ami-builder-instance"にSSHログインします。

設定値の指定

設定値の指定

手順に必要な設定値を変数に格納をします。

0. リージョンの指定

リージョンを指定します。

環境変数の設定:

export AWS_DEFAULT_REGION='ap-northeast-1'

1. EC2インスタンス名

EC2インスタンス名を指定します。

変数の設定:

EC2_INSTANCE_TAG_NAME='handson-cli-ec2-ami-builder-instance'

2. SSHポート番号

EC2インスタンスへのSSHログインで利用するポート番号を指定します。

変数の設定:

PORT_SSHD='22022'

3. EC2キーペア名

EC2キーペア名を指定します。

変数の設定:

EC2_KEY_PAIR_NAME="handson-cli-ec2-ami-keypair"

4. SSH秘密鍵ファイル名

鍵ファイルディレクトリを指定します。

変数の設定:

DIR_SSH="${HOME}/.ssh"

AWS IDを取得します。

コマンド:

AWS_ID=$( \
  aws sts get-caller-identity \
    --query 'Account' \
    --output text \
) \
  && echo ${AWS_ID}

結果(例):

XXXXXXXXXXXX

SSH秘密鍵ファイル名を指定します。

変数の設定:

FILE_SSH_KEY_SECRET="${DIR_SSH}/${EC2_KEY_PAIR_NAME}-${AWS_ID}-${AWS_DEFAULT_REGION}-ec2.pem" \
  && echo ${FILE_SSH_KEY_SECRET}

結果(例):

${HOME}/.ssh/handson-cli-ec2-ami-keypair-XXXXXXXXXXXX-ap-northeast-1-ec2.pem

設定値の確認

各変数に正しい設定値が格納されていることを確認しながら保存します。

変数の確認:

cat << END

  # 0. AWS_DEFAULT_REGION:"ap-northeast-1"
       AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 1. EC2_INSTANCE_TAG_NAME:"handson-cli-ec2-ami-builder-instance"
       EC2_INSTANCE_TAG_NAME="${EC2_INSTANCE_TAG_NAME}"
  # 2. PORT_SSHD:"22022"
       PORT_SSHD="${PORT_SSHD}"
  # 3. EC2_KEY_PAIR_NAME:"handson-cli-ec2-ami-keypair"
       EC2_KEY_PAIR_NAME="${EC2_KEY_PAIR_NAME}"
  # 4. FILE_SSH_KEY_SECRET:"${HOME}/.ssh/handson-cli-ec2-ami-keypair-XXXXXXXXXXXX-ap-northeast-1-ec2.pem"
       FILE_SSH_KEY_SECRET="${FILE_SSH_KEY_SECRET}"

END

下段の変数が入っていない、もしくは上段と同等の値が入っていない場合は、それぞれの手順番号に戻って変数の設定を行います。

処理の実行

EC2インスタンスのインスタンスIDを取得します。

コマンド:

ARRAY_EC2_INSTANCE_IDS=$( \
  aws ec2 describe-instances \
    --filters Name=tag:Name,Values=${EC2_INSTANCE_TAG_NAME} \
              Name=instance-state-name,Values=running \
    --query Reservations[].Instances[].InstanceId \
    --output text \
) \
  && echo ${ARRAY_EC2_INSTANCE_IDS}

結果(例):

i-xxxxxxxxxxxxxxxxx

EC2インスタンスのグローバルIPアドレスを取得します。

コマンド:

EC2_IP_PUBLIC=$( \
  aws ec2 describe-instances \
    --filters Name=tag:Name,Values=${EC2_INSTANCE_TAG_NAME} \
    --instance-ids ${ARRAY_EC2_INSTANCE_IDS} \
    --query "Reservations[].Instances[].PublicIpAddress" \
    --output text \
) \
  && echo ${EC2_IP_PUBLIC}

結果(例):

xxx.xxx.xxx.xxx

EC2インスタンスにログインします。

変数の確認

cat << END

  # FILE_SSH_KEY_SECRET: ${HOME}/.ssh/handson-cli-ec2-ami-keypair-XXXXXXXXXXXX-ap-northeast-1-ec2.pem
    FILE_SSH_KEY_SECRET="${FILE_SSH_KEY_SECRET}"
  # EC2_IP_PUBLIC:"xxx.xxx.xxx.xxx"
    EC2_IP_PUBLIC="${EC2_IP_PUBLIC}"
  # PORT_SSHD:"22022"
    PORT_SSHD="${PORT_SSHD}"

END

コマンド:

ssh ${EC2_IP_PUBLIC} \
  -i ${FILE_SSH_KEY_SECRET} \
  -l ec2-user \
  -p ${PORT_SSHD}

注釈

以下が表示された場合は、'yes'と入力して、エンターキーを押します。

The authenticity of host 'xxx.xxx.xxx.xxx(xxx.xxx.xxx.xxx)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
ECDSA key fingerprint is MD5:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)?

結果(例):

Warning: Permanently added 'xxx.xxx.xxx.xxx' (RSA) to the list of known hosts.

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
24 package(s) needed for security, out of 53 available
Run "sudo yum update" to apply all updates.

完了確認

「EC2インスタンス"handson-cli-ec2-ami-builder-instance"にログインしている。」ことを確認します。

ここでは、EC2インスタンスのメタデータからグローバルIPアドレスを取得して、ログイン先のグローバルIPアドレスと同じであることを確認します。

コマンド:

curl -q http://169.254.169.254/latest/meta-data/public-ipv4

結果:

xxx.xxx.xxx.xxx

手順の完了