Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout Problem with Ruby Fog when bootstrapping AWS servers

I've been trying for a little while now to provision a small instance on AWS with the fog library. I've been somewhat successful (in that an instance does spool up when I run this code), but I keep getting timeout errors during the SSH portion, and when I dug deeper I found that they're consistently "AuthentitcationFailed" problems.

The failing code is as follows:

require 'rubygems'
require "fog"

connection = Fog::Compute.new({
  provider:              "AWS",
  aws_secret_access_key: SECRET_KEY,
  aws_access_key_id:     ACCESS_KEY
})

server = connection.servers.bootstrap({
  private_key_path:  "~/.ssh/id_rsa", 
  public_key_path:   "~/.ssh/id_rsa.pub",
  username: "ubuntu"
})

Much reading has told me that sometimes this is just because the instance takes too long to spool up, but this is very consistent (it happens every time I try it). Does anyone see what I'm doing wrong?

like image 279
Ethan Vizitei Avatar asked Jun 24 '26 09:06

Ethan Vizitei


1 Answers

I had the same problem some days ago and actually found the problem for my case and submitted it to the Fog issue tracker.

A colleague of mine was using connection.bootstrap() with the same AWS credentials but different SSH keys. So the "fog_default" public key had already been registered and the attempt to log in with my key pair failed.

If you're experiencing similar problems, check with connection.key_pairs.get('fog_default') if fog_default was registered before.

If this is actually the case, you have three options to get around this problem:

  • Delete the fog_default by running: connection.key_pairs.get('fog_default').destroy and register your new public key via bootstrap()
  • Manually register your custom key under a custom name
  • Set Fog.credential to a custom name so bootstrap() uses this name instead of "default" to register your public key

Solution two looks like this:

Fog.credentials = Fog.credentials.merge({
  :private_key_path => "./keys/my_custom_key",
  :public_key_path => "./keys/my_custom_key.pub"
})

if connection.key_pairs.get('my_custom_key').nil?
  public_key = IO.read('./keys/my_custom_key.pub')
  connection.import_key_pair('my_custom_key', public_key)
end

server = connection.servers.bootstrap(
  :key_name =>  'my_custom_key',
  ...
)

Solution three, which I prefer because the only change I need to make is to set Fog.credential, looks like this:

Fog.credential = :my_custom_key

connection.servers.bootstrap(
  :private_key_path => './keys/my_custom_key',
  :public_key_path => './keys/my_custom_key.pub',
  ...
)
like image 98
pfleidi Avatar answered Jun 26 '26 04:06

pfleidi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!