ようへいの日々精進XP

よかろうもん

2017 年 03 月 13 日(月)

中途半端な天気

雨なんだか、晴れなんだか。

風呂上がりに夜風にあたりたい季節になってきた。

春はそこまで来ているのかもしれない。

EC2 の一覧、ELB の一覧

仕事から EC2 の一覧とか ELB の一覧がすぐ欲しい時がある。

うっかりするとマネジメントコンソールのスクショを撮ろうとするが、ここは CLI でってことで。コマンド化してみた。

$ cat /usr/local/bin/list-ec2
#!/usr/bin/env bash

_PROFILE=${1}
_TAG_NAME_PREFIX="${2}"
_REGION=${3}

[ ! -n "${_REGION}" ] && _REGION="ap-northeast-1"

aws --profile ${1} --region ${_REGION} \
  ec2 describe-instances \
    --filter "Name=tag:Name,Values=${_TAG_NAME_PREFIX}" \
    --query 'Reservations[].Instances[].{InstanceId:InstanceId, Name:Tags[?Key==`Name`]| [0].Value, InstanceType:InstanceType, PrivateIpAddress:PrivateIpAddress, State:State.Name}' \
    --output table

EC2 の一覧を取得する。

以下のように使う。

list-ec2 oreno-profile oreno-ec2

ELB も同じように…

$ cat /usr/local/bin/list-elb-ec2
#!/usr/bin/env bash

_PROFILE=${1}
_ELB_NAME=${2}
_REGION=${3}

[ ! -n "${_REGION}" ] && _REGION="ap-northeast-1"

aws --profile ${1} --region ${_REGION} \
  elb describe-instance-health \
    --load-balancer-name ${_ELB_NAME} --query 'InstanceStates[].{InstanceId:InstanceId,State:State}' --output table

以下のように使う。

list-ec2 oreno-profile oreno-elb

list_instances というナゾのコマンド

上記のコマンドを仕込んでいる時に発見したんだけど、list_instances というコマンドがインストールされていた。

$ list_instances --help
Usage: list_instances [options]

Options:
  -h, --help            show this help message and exit
  -r REGION, --region=REGION
                        Region (default us-east-1)
  -H ID,Zone,Groups,Hostname,State,T:Name, --headers=ID,Zone,Groups,Hostname,State,T:Name
                        Set headers (use 'T:tagname' for including tags)
  -t, --tab             Tab delimited, skip header - useful in shell scripts
  -f FILTER, --filter=FILTER
                        Filter option sent to DescribeInstances API call,
                        format is key1=value1,key2=value2,...

どうやら、EC2 インスタンスの一覧を取得してくれるツールらしい。