AWS: EC2 インスタンスの簡易一覧表示
AWS CLI の出力結果(json)を表形式にフォーマットして見やすくしたい。
EC2 インスタンス一覧の表示
jq でやろうと思ったが断念。Python のスクリプトを書いた。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import subprocess
try:
import json
except ImportError:
print('You need python 2.6 or later to run this script.')
sys.exit(1)
def print_instances():
headers = [
('Name', 24),
('Instance', 12),
('Type', 12),
('State', 12),
('LaunchTime', 24),
]
print(''.join([x[0].ljust(x[1]) for x in headers]))
print('-' * sum([x[1] for x in headers]))
stdout = subprocess.check_output(['aws', 'ec2', 'describe-instances'])
output = json.loads(stdout)
for ins in output['Reservations'][0]['Instances']:
result = [
''.join([x['Value'] for x in ins['Tags'] if x['Key'] == 'Name']),
ins['InstanceId'],
ins['InstanceType'],
ins['State']['Name'],
ins['LaunchTime'],
]
print(''.join([result[i].ljust(x[1]) for i, x in enumerate(headers)]))
if __name__ == '__main__':
print_instances()
- 出力例
Name Instance Type State LaunchTime
------------------------------------------------------------------------------------
server1 i-00000000 t1.micro running 2013-07-01T10:58:49.000Z
server2 i-11111111 m1.small stopped 2013-07-01T11:58:49.000Z
server3 i-22222222 m1.small stopping 2013-07-01T12:58:49.000Z
i-33333333 t1.micro pending 2013-07-01T13:58:49.000Z
最終行は名前未設定のインスタンス。
0 件のコメント:
コメントを投稿