LoginSignup
7
3

More than 5 years have passed since last update.

はじめに

大阪にある株式会社鈴木商店の姜です。

弊社は100%自社開発で受託開発を行っている会社です。
主にAWS上にフルスクラッチでシステムを構築することが多く、クライアント毎にAWSアカウントが複数あります。
その中でそれぞれのアカウントのEC2にアクセスして作業するのは大変です。
そこで、普段の業務でEC2のIPアドレスやDBの情報などをなるべく楽に見れるように使っているワンライナーを公開します。

利用方法

こんな感じです。

# 全てのEC2の情報を取得する
$ ec2 foo  # fooは後述のアカウント名
| InstanceName   | PublicIpAddress | InstanceId          | InstanceType | KeyName  | State   |
| -------------- | --------------- | ------------------- | ------------ | -------- | ------- |
| pro-ec2-batch  | XX.XX.XX.XX     | i-xxxxxxxxxxxxxxxxx | t2.micro     | keypairA | running |
| pro-ec2-mta    | YY.YY.YY.YY     | i-yyyyyyyyyyyyyyyyy | t2.micro     | keypairB | stopped |

# 起動中のEC2のみ取得する
$ ec2-running foo
| InstanceName   | PublicIpAddress | InstanceId          | InstanceType | KeyName  | State   |
| -------------- | --------------- | ------------------- | ------------ | -------- | ------- |
| pro-ec2-batch  | XX.XX.XX.XX     | i-xxxxxxxxxxxxxxxxx | t2.micro     | keypairA | running |

# RDSの情報を取得する
$ rds foo
| DBInstanceIdentifier | DBInstanceStatus | DBName  | Address                                |
| -------------------- | ---------------- | ------- | -------------------------------------- |
| pro-ZZZZZ            | available        | DB_NAME | AA.XX.ap-northeast-1.rds.amazonaws.com |
| stg-ZZZZZ            | available        | DB_NAME | BB.YY.ap-northeast-1.rds.amazonaws.com |

下準備

AWSアカウントの設定

AWS CLIを使うので、各アカウントにIAMユーザを作成してキーを取得して設定しておきます。

$ cat ~/.aws/credentials
[foo]
aws_access_key_id = AKIAXXXXXXX
aws_secret_access_key = XXXXXXXX

各種ツールをインストール

  • pip
sudo easy_install pip
  • aws cli
sudo pip install awscli
  • jq
brew install jq
  • csvkit
pip install csvkit

関数を登録

次に~/.bashrc にでも以下を登録します。
source ~/.bashrc を忘れないように。

alias aws="aws --region ap-northeast-1"

function rds {
    aws --profile $1 rds describe-db-instances | jq -r '[ "DBInstanceIdentifier", "DBInstanceStatus", "DBName", "Address" ], (.DBInstances[] | [ .DBInstanceIdentifier, .DBInstanceStatus, .DBName, .Endpoint.Address]) | @csv' | csvsort -c DBInstanceIdentifier |  csvlook
}

function ec2-running {
    aws --profile $1 ec2 describe-instances --filter "Name=instance-state-name,Values=running" | jq -r '[ "InstanceName", "PublicIpAddress", "InstanceId", "InstanceType", "KeyName", "State" ], (.Reservations[].Instances[] | [(.Tags[] | select(.Key=="Name").Value), .PublicIpAddress, .InstanceId, .InstanceType, .KeyName ,.State.Name]) | @csv' | csvsort -c InstanceName |  csvlook
}

function ec2 {
    aws --profile $1 ec2 describe-instances | jq -r '[ "InstanceName", "PublicIpAddress", "InstanceId", "InstanceType", "KeyName", "State" ], (.Reservations[].Instances[] | [(.Tags[] | select(.Key=="Name").Value), .PublicIpAddress, .InstanceId, .InstanceType, .KeyName, .State.Name]) | @csv' | csvsort -c InstanceName |  csvlook
}

function ec2-start {
    aws --profile $1 ec2 start-instances --instance-ids $2
}

function ec2-stop {
    aws --profile $1 ec2 stop-instances --instance-ids $2
}

以上。

番外編

他にもよく使うものをご紹介。

  • グローバルIPを取得
curl ifconfig.io
  • マージ済みのローカルブランチを削除(AWSとは関係ないですが・・)
git branch --merged | grep -vE '^\*|master$|develop$' | xargs -I % git branch -d %
  • Apacheログをステータスコードでフィルタする(これは500番台)
cat access_log | awk '($9 >= 500 && $9 < 600 || $7 >= 500 && $7 < 600) {print $0 }' | less
  • sshdが上がってくるまで再接続を試みるスクリプト
sshr() {
    while ! ssh -o ConnectTimeout=1 $@; do sleep 1; done
}
7
3
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
7
3