処理の実行
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}
 
 
結果(例):
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}
 
 
結果(例):
EC2インスタンスにログインします。
変数の確認
cat << END
  # FILE_SSH_KEY_SECRET: ${HOME}/.ssh/handson-cli-ec2-ebs-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-ebs-instance"にログインしている。」ことを確認します。
ここでは、EC2インスタンスのメタデータからグローバルIPアドレスを取得して、ログイン先のグローバルIPアドレスと同じであることを確認します。
コマンド:
curl -q http://169.254.169.254/latest/meta-data/public-ipv4
 
 
結果: