module Meroku module Infrastructure def self.spawn credentials = Aws::Credentials.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET']) ec2_client = Aws::EC2::Client.new(region: 'us-east-1', credentials: credentials) ec2_client.run_instances( image_id: 'ami-cd0f5cb6', min_count: 1, max_count: 1, key_name: 'meroku.id_rsa', instance_type: 't2.micro', ).instances.first end def self.despawn resp = nil credentials = Aws::Credentials.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET']) ec2 = Aws::EC2::Resource.new(region: 'us-east-1', credentials: credentials) running_instance = ec2.instances.detect { |i| i.exists? && i.state.code!=48 } resp = running_instance.terminate if running_instance resp end end end