Skip to content

Instantly share code, notes, and snippets.

@cm-watanabeseigo
Last active February 8, 2017 08:49
Show Gist options
  • Save cm-watanabeseigo/55b831e889c226939c478a5c754cebb4 to your computer and use it in GitHub Desktop.
Save cm-watanabeseigo/55b831e889c226939c478a5c754cebb4 to your computer and use it in GitHub Desktop.
起動したEC2インスタンスの一覧を簡易表示するスクリプトが欲しい | Developers.IO - http://dev.classmethod.jp/cloud/aws/aws-ec2-summary-script/
#!/usr/bin/env ruby
# coding: utf-8
# usage: $0 [some options for `aws ec2`]
require 'json'
line_format = "%-19s %-23s %-11s %-15s %-15s %-12s %-16s %s"
line_length = 142
# command line
aws_cli_cmd = ["aws","ec2",ARGV,"describe-instances"].join(" ")
# header
puts line_format % [
"InstanceID","Name","Status","PublicIP","PrivateIP","InstanceType","AvailabilityZone","LaunchTime"
]
puts "-" * line_length
# run aws cli and parse
JSON.parse(%x(#{aws_cli_cmd}))["Reservations"].each{|n|
# ReservationId loop
n["Instances"].each{|i|
puts line_format % [
i["InstanceId"],
if i.has_key?("Tags") then i["Tags"].find{|tag| tag["Key"] == "Name" }["Value"] else "" end,
i["State"]["Name"],
if i.has_key?("PublicIpAddress") then i["PublicIpAddress"] else "" end,
if i.has_key?("PrivateIpAddress") then i["PrivateIpAddress"] else "" end,
i["InstanceType"],
i["Placement"]["AvailabilityZone"],
i["LaunchTime"]
]
}
}
# footer
puts "-" * line_length
puts "%#{line_length}s" % [aws_cli_cmd]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment