def create_instance(credentials, image_id, opts={})
ec2 = new_client(credentials)
instance_options = {}
instance_options.merge!(:user_data => opts[:user_data]) if opts[:user_data]
instance_options.merge!(:key_name => opts[:keyname]) if opts[:keyname]
instance_options.merge!(:availability_zone => opts[:realm_id]) if opts[:realm_id]
instance_options.merge!(:instance_type => opts[:hwp_id]) if opts[:hwp_id] && opts[:hwp_id].length > 0
instance_options.merge!(:group_ids => opts[:security_group]) if opts[:security_group]
instance_options.merge!(
:min_count => opts[:instance_count],
:max_count => opts[:instance_count]
) if opts[:instance_count] and opts[:instance_count].length!=0
safely do
new_instance = convert_instance(ec2.launch_instances(image_id, instance_options).first)
tag_instance(credentials, new_instance, opts[:name])
if opts[:load_balancer_id] and opts[:load_balancer_id]!=""
lb = lb_register_instance(credentials,
{'id' => opts[:load_balancer_id],
'instance_id' => new_instance.id})
end
new_instance
end
end