はじめに
いちいちAWSのポータルサイトを開いてインスタンス一覧を表示するのが面倒なので、コマンドラインからインスタンスの情報を取得したい
手順
AWSCLI のインストール
awscli をインストールするために pip (python のパッケージ管理ツール) が必要なので、まずそれをインストールする。
そして、 awscli をインストールする
yum -y install epel-release
yum -y install python-pip
pip install pip --upgrade
pip install awscli --user
CentOS(RHEL)8 で pip が利用できない場合は以下に当たる可能性あり
(CentOS(RHEL) 8 で python , pip コマンドが利用できない
インストールできると、以下のようにインストール済みのパッケージ一覧として出力される。
# pip list |grep aws
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
インストール先がなぜか以下になっていた
# ls -al /root/.local/bin
合計 152
drwxr-xr-x 2 root root 4096 7月 15 17:37 .
drwxr-xr-x. 5 root root 41 7月 15 17:37 ..
-rwxr-xr-x 1 root root 815 7月 15 17:37 aws
-rwxr-xr-x 1 root root 1432 7月 15 17:37 aws.cmd
-rwxr-xr-x 1 root root 204 7月 15 17:37 aws_bash_completer
-rwxr-xr-x 1 root root 1136 7月 15 17:37 aws_completer
-rwxr-xr-x 1 root root 1807 7月 15 17:37 aws_zsh_completer.sh
-rwxr-xr-x 1 root root 1695 7月 15 17:37 jp.py
-rw-r--r-- 1 root root 1877 7月 15 17:37 jp.pyc
-rwxr-xr-x 1 root root 212 7月 15 17:37 pyrsa-decrypt
-rwxr-xr-x 1 root root 212 7月 15 17:37 pyrsa-encrypt
-rwxr-xr-x 1 root root 210 7月 15 17:37 pyrsa-keygen
-rwxr-xr-x 1 root root 233 7月 15 17:37 pyrsa-priv2pub
-rwxr-xr-x 1 root root 206 7月 15 17:37 pyrsa-sign
-rwxr-xr-x 1 root root 210 7月 15 17:37 pyrsa-verify
-rwxr-xr-x 1 root root 594 7月 15 17:37 rst2html.py
-rw-r--r-- 1 root root 604 7月 15 17:37 rst2html.pyc
-rwxr-xr-x 1 root root 714 7月 15 17:37 rst2html4.py
-rw-r--r-- 1 root root 726 7月 15 17:37 rst2html4.pyc
-rwxr-xr-x 1 root root 1140 7月 15 17:37 rst2html5.py
-rw-r--r-- 1 root root 730 7月 15 17:37 rst2html5.pyc
-rwxr-xr-x 1 root root 791 7月 15 17:37 rst2latex.py
-rw-r--r-- 1 root root 734 7月 15 17:37 rst2latex.pyc
-rwxr-xr-x 1 root root 600 7月 15 17:37 rst2man.py
-rw-r--r-- 1 root root 709 7月 15 17:37 rst2man.pyc
-rwxr-xr-x 1 root root 764 7月 15 17:37 rst2odt.py
-rw-r--r-- 1 root root 803 7月 15 17:37 rst2odt.pyc
-rwxr-xr-x 1 root root 1698 7月 15 17:37 rst2odt_prepstyles.py
-rw-r--r-- 1 root root 2044 7月 15 17:37 rst2odt_prepstyles.pyc
-rwxr-xr-x 1 root root 601 7月 15 17:37 rst2pseudoxml.py
-rw-r--r-- 1 root root 600 7月 15 17:37 rst2pseudoxml.pyc
-rwxr-xr-x 1 root root 637 7月 15 17:37 rst2s5.py
-rw-r--r-- 1 root root 649 7月 15 17:37 rst2s5.pyc
-rwxr-xr-x 1 root root 871 7月 15 17:37 rst2xetex.py
-rw-r--r-- 1 root root 817 7月 15 17:37 rst2xetex.pyc
-rwxr-xr-x 1 root root 602 7月 15 17:37 rst2xml.py
-rw-r--r-- 1 root root 612 7月 15 17:37 rst2xml.pyc
-rwxr-xr-x 1 root root 670 7月 15 17:37 rstpep2html.py
-rw-r--r-- 1 root root 680 7月 15 17:37 rstpep2html.pyc
AWS への接続
AMI でアクセスするユーザーのアクセスIDとアクセスキーを用意しておく。
そして以下のコマンドを実行する
Region は以下で確認できる
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/using-regions-availability-zones.html
/root/.local/bin/aws configure
AWS Access Key ID [None]:********************************
AWS Secret Access Key [None]:********************************
Default region name [None]:ap-northeast-1
Default output format []:text
以下のようにヒアドキュメントにしてスクリプト組んどけば楽。
# !/bin/bash
PATH=$PATH:/root/.local/bin
aws configure <<EOF
<アクセスID>
<アクセスキー>
ap-northeast-1
text
EOF
ヒアドキュメントだと APIキーがばればれなのでファイルアクセスできるのは root 以外にさせたくなので以下のようにしといた。
KEY=`cat /root/aws_cli.key`
aws configure <<EOF
${KEY}
EOF
cat /root/aws_cli.key
<アクセスID>
<アクセスキー>
ap-northeast-1
text
インスタンスの情報取得
インスタンスの全情報取得
/root/.local/bin/aws ec2 describe-instances
指定したインスタンスの情報取得と表示(テキスト表示)
/root/.local/bin/aws ec2 describe-instances --instance-ids i-042c57e43adae9b35 --output text
指定したインスタンスの情報取得と表示(テーブル表示)
/root/.local/bin/aws ec2 describe-instances --instance-ids i-042c57e43adae9b35 --output table
インスタンスの NAME タグだけを表示
/root/.local/bin/aws ec2 describe-tags --query 'Tags[?ResourceType==`instance`] | [?Key==`Name`].{ResourceName:Value}'
AWS のインスタンスIDとNAMEタグをCSV形式で出力
AWS のインスタンスID をもとにNAMEタグを取得してCSV形式で出力する。
(本当は、 --query のなかに固定文字「,」を入れたかったんだけどよくわからなかったので1つずつ属性を取得して結合した非効率なやり方)
for i in `aws ec2 describe-instances --query 'Reservations[].Instances[].{InstanceId:InstanceId}'`; do
NAME=`aws ec2 describe-instances --instance-ids $i --output text --query 'Reservations[].Instances[].Tags[?Key==\`Name\`].[Value]'`
echo $i,$NAME
done
実行しているインスタンス一覧の取得と表示
aws ec2 describe-instances --filter "Name=instance-state-name,Values=running"
インスタンスID とインスタンスタイプ(t2.microとか)の取得と表示
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId,InstanceType]' --output text`
その他
「--query」をもう少し自在に扱いたいと思う。固定文字入れたりとかとか
参考