Search code examples
ruby-on-railsrubyamazon-web-servicesfog

Retrieving details from Amazon using fog in Ruby on Rails


I am currently working in a project with Rails, and I have faced the need to import the existing server details from Amazon using the fog library.

I have tried some initial code to get the access to AWS, and at this point I have got the connection with the credentials.

The issue is that when I continue to get that instance details, it does not return anything.

require 'fog'
aws_credentials = {
:aws_access_key_id => "ACCESS ID"
:aws_secret_access_key "SECRET ID"
}
conn2 = Fog::Compute.new(aws_credentials.merge(:provider => 'AWS'))
conn2.servers.all.each do |i|
puts i.id
end

Could anyone please help me fixing this behavior?


Solution

  • The Amazon provider in fog defaults to using the us-east-1 region. It could be that your servers are in another region. To specify a different region by passing the :region into your Fog::Compute constructor. Valid regions include ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-west-1', 'sa-east-1', 'us-east-1', 'us-west-1', 'us-west-2'.

    So for instance if you are using region ap-northeast-1, your code would look like the following:

    require 'fog'
    aws_credentials = {
    :aws_access_key_id => "ACCESS ID"
    :aws_secret_access_key "SECRET ID"
    }
    conn2 = Fog::Compute.new(aws_credentials.merge(:provider => 'AWS', :region => 'ap-northeast-1' ))
    conn2.servers.all.each do |i|
    puts i.id
    end