Created
August 22, 2020 09:25
-
-
Save mattsan/d0e5e2f989dbd33dc78eca1f3fc86417 to your computer and use it in GitHub Desktop.
AWS EC2 インスタンスの状態を調べる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'aws-sdk-ec2' | |
require 'thor' | |
class Ec2Stat < Thor | |
default_command :stat | |
desc :stat, 'show statuses of ec2 instances' | |
option :profile, aliases: '-p', default: 'default' | |
def stat | |
# Class: Aws::EC2::Client — AWS SDK for Ruby V3 | |
# https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/EC2/Client.html | |
client = Aws::EC2::Client.new(profile: profile) | |
# https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/EC2/Client.html#describe_instances-instance_method | |
response = client.describe_instances | |
puts | |
puts <<~EOS | |
launch-time name state image-id instance-id | |
------------------------------------------------------------------------------------------------------------------------ | |
EOS | |
puts response.reservations.map {|reservation| | |
instances = reservation.instances.select {|instance| | |
instance.tags.any? {|tag| | |
tag.key == 'Name' && tag.value =~ /^(production|staging)-.*$/ | |
} | |
} | |
instances.map {|instance| | |
instance_id = instance.instance_id | |
image_id = instance.image_id | |
launch_time = instance.launch_time | |
state = instance.state | |
name = instance.tags.find {|tag| tag.key == 'Name' }.value | |
format(' %-25s %-20s %-20s %-22s %s', launch_time.localtime, name, state.name, image_id, instance_id) | |
} | |
}.flatten.sort | |
puts | |
end | |
no_commands do | |
def profile | |
options[:profile] | |
end | |
end | |
end | |
Ec2Stat.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment