module Meroku module Aws # Logic related to the ec2 instances we use as servers module Ec2 def tag { resource_type: 'instance', tags: [ { key: 'Name', value: 'node' } ] } end def make_instance ec2_client.run_instances( image_id: 'ami-841f46ff', min_count: 1, max_count: 1, key_name: 'meroku.id_rsa', instance_type: 't2.micro', tag_specifications: [tag] ).instances.first end def ec2_client ::Aws::EC2::Client.new( region: 'us-east-1', credentials: ::Aws::Credentials.new( ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET'] ) ) end def self.ec2_client ::Aws::EC2::Client.new( region: 'us-east-1', credentials: ::Aws::Credentials.new( ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET'] ) ) end def self.associate_address(eipalloc, id) retries ||= 0 ec2_client.associate_address(allocation_id: eipalloc, instance_id: id) rescue ::Aws::EC2::Errors::InvalidInstanceID sleep 1 puts 'retrying associate_address()' retry if (retries += 1) < 15 end end end end