LoginSignup
0
0

More than 5 years have passed since last update.

[JAWS-UG CLI] EC2 # EC2インスタンスへのログイン (ユーザデータ利用)

Posted at

前提条件

EC2への権限

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

注釈: 読み取り

AWS CLIのバージョン

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

  • AWS CLI 1.11.14
コマンド
aws --version

結果(例):

  aws-cli/1.11.34 Python/2.7.10 Darwin/15.6.0 botocore/1.4.91

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

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

AWSアカウントの属性

AWSアカウントがEC2-Classicに対応していないこと。

コマンド
AWS_SUPPORT_PLATFORMS=$( \
         aws ec2 describe-account-attributes \
           --query 'AccountAttributes[?AttributeName == `supported-platforms`].AttributeValues[].AttributeValue' \
           --output text \
) \
        && echo ${AWS_SUPPORT_PLATFORMS}

結果:

  VPC

注釈: 'VPC'の他に'EC2'が表示される場合、別のアカウントを作成もしくは
利用し てください。

0. 準備

0.1. リージョンの決定

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

0.2. プロファイルの確認:

プロファイルが想定のものになっていることを確認します。

変数の確認
aws configure list

結果(例):

        Name                    Value             Type    Location
        ----                    -----             ----    --------
     profile       ec2as_full-prjZ-mbp13        env    AWS_DEFAULT_PROFILE
  access_key     ****************XXXX shared-credentials-file
  secret_key     ****************XXXX shared-credentials-file
      region        ap-northeast-1        env    AWS_DEFAULT_REGION

1. 事前作業

1.1. 秘密鍵ファイルの指定

利用するキーペアの秘密鍵ファイルを指定します。

変数の設定
FILE_SSH_KEY="${HOME}/.ssh/<キーペア秘密鍵のファイル名>" \
        echo ${FILE_SSH_KEY}

秘密鍵が存在することを確認しましょう。

コマンド
ls ${FILE_SSH_KEY}

1.2. プライベートアドレスの指定

インスタンスのプライベートIPアドレスを指定します。

変数の設定
EC2_PRIVATE_ADDR='172.18.16.22'

1.3. パブリックIPアドレスの取得

コマンド
EC2_PUBLIC_IP=$( \
        aws ec2 describe-instances \
          --instance-id ${EC2_INSTANCE_ID} \
          --query "Reservations[].Instances[].PublicIpAddress" \
          --output text \
) \
        && echo ${EC2_PUBLIC_IP}

結果(例):

  xxx.xxx.xxx.xxx

2. ログイン

2.1. SSHログイン

変数の確認
cat << ETX

        FILE_SSH_KEY:  ${FILE_SSH_KEY}
        EC2_PUBLIC_IP: ${EC2_PUBLIC_IP}

ETX
コマンド
ssh -i ${FILE_SSH_KEY} ec2-user@${EC2_PUBLIC_IP}

結果(例):

  The authenticity of host '54.xxx.xxx.xxx (54.xxx.xxx.xxx)' can't be established.
  RSA key fingerprint is 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)?

入力:

  yes

結果(例):

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

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

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

2.2. userdataのログ確認

コマンド
tail -f /var/log/cloud-init-output.log

結果(例):

  (省略)

  Cloud-init v. 0.7.6 finished at Wed, 08 Feb 2017 23:21:42 +0000. Datasource DataSourceEc2.  Up 375.98 seconds

完了

0
0
0

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
0
0