Class: ActiveAws::Ec2
Class Method Summary collapse
- .client ⇒ Object
- .find(instance_id) ⇒ Object
- .find_by_name(name) ⇒ Object
-
.where(**args) ⇒ Object
Usage: Ec2::where( :“tag:Role” => “web” ) Ec2::where( :“instance-type” => “t2.micro” ).
Instance Method Summary collapse
- #create_image!(name: nil, description: nil, no_reboot: true, block_device_mappings: nil) ⇒ Object
- #name ⇒ Object
- #reload ⇒ Object
-
#wait_until(waiter_name = :instance_running, &block) ⇒ Object
‘waiter_name` can be checked with the command below.
Methods inherited from Base
configure, inherited, #initialize, load_config!, #to_h
Constructor Details
This class inherits a constructor from ActiveAws::Base
Class Method Details
.client ⇒ Object
16 17 18 |
# File 'lib/active_aws/ec2.rb', line 16 def client Aws::EC2::Client.new( **configure.default_client_params ) end |
.find(instance_id) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/active_aws/ec2.rb', line 20 def find( instance_id ) response = client.describe_instances({ instance_ids: [instance_id], }) return nil unless response.reservations[0] new( **response.reservations[0].instances[0].to_h ) end |
.find_by_name(name) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/active_aws/ec2.rb', line 28 def find_by_name( name ) response = client.describe_instances({ filters: [{ name: "tag:Name", values: [name] }], }) return nil unless response.reservations[0] new( **response.reservations[0].instances[0].to_h ) end |
.where(**args) ⇒ Object
Usage: Ec2::where( :“tag:Role” => “web” ) Ec2::where( :“instance-type” => “t2.micro” )
39 40 41 42 43 44 45 46 |
# File 'lib/active_aws/ec2.rb', line 39 def where( **args ) filter_params = args.map{|k, v| { name: k, values: Array.wrap(v) }} response = client.describe_instances({ filters: filter_params, }) instance_params = response.reservations.map{|r| r.instances }.flatten instance_params.map{|i| new( **i.to_h )} end |
Instance Method Details
#create_image!(name: nil, description: nil, no_reboot: true, block_device_mappings: nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/active_aws/ec2.rb', line 59 def create_image!( name: nil, description: nil, no_reboot: true, block_device_mappings: nil ) name ||= "#{self.name}-#{Time.current.strftime('%Y%m%d%H%M')}" description ||= name block_device_mappings ||= [{ device_name: '/dev/xvda', ebs: { volume_size: 8, }, }] self.class.client.create_image({ block_device_mappings: block_device_mappings, description: description, instance_id: instance_id, name: name, no_reboot: no_reboot, }) end |
#name ⇒ Object
49 50 51 52 53 |
# File 'lib/active_aws/ec2.rb', line 49 def name name_tag = .detect{|t| t[:key].to_s == "Name"} return nil unless name_tag name_tag[:value] end |
#reload ⇒ Object
55 56 57 |
# File 'lib/active_aws/ec2.rb', line 55 def reload self.class.find( instance_id ) end |
#wait_until(waiter_name = :instance_running, &block) ⇒ Object
‘waiter_name` can be checked with the command below. ActiveAws::Ec2.client.waiter_names
Usage: ec2.wait_until :instance_running do |i|
i.max_attempts = 5
i.delay = 5
end
85 86 87 |
# File 'lib/active_aws/ec2.rb', line 85 def wait_until( waiter_name=:instance_running, &block ) self.class.client.wait_until(waiter_name, instance_ids: [instance_id], &block) end |