Skip to content

Instantly share code, notes, and snippets.

@erikh
Created December 13, 2012 02:19
Show Gist options
  • Save erikh/1c393c51784a6f684b8f to your computer and use it in GitHub Desktop.
Save erikh/1c393c51784a6f684b8f to your computer and use it in GitHub Desktop.
# from the docs --
# returns an Array on multiple servers and a
# AWS::EC2::Instance for a single server.
instances = aws_ec2.instances.create(
:count => @number_of_servers,
:image_id => ec2.ami,
:security_groups => ec2.security_groups,
:key_name => ec2.ssh_key
)
p instances.class # Actually an AWS::Core::Data::List in the multiple case
# box it if it's not an array. AWS::Core::Data::List is a kind of Array.
instances = [instances] unless instances.kind_of?(Array)
#
# There are instances where AWS won't acknowledge a created instance
# right away. Let's make sure the API server knows they all exist before
# moving forward.
#
unresolved_instances = instances.dup # Returns new Array for Array, returns self for AWS::Core::Data::List
until unresolved_instances.empty?
instance = unresolved_instances.shift
unless (instance.status rescue nil)
if_debug(3) do
$stderr.puts "API server doesn't think #{instance.id} exists yet."
end
sleep 0.3
unresolved_instances.push(instance)
end
end
p instances # empty for AWS::Core::Data::List
# by my best calculation this bug cost the company around $150 to find, and that's long before my salary comes into play.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment