43

It is saying AWS is uninitialized. I am usign the aws-sdk-core gem.

I tried using the aws-sdk gem instead, and the problem was still there.

This is the initializers/aws.rb file:

AWS.config(:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
                      :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'])

s3 = AWS::S3.new
AVATAR_BUCKET = s3.buckets[ENV['AVATAR_BUCKET_NAME']]

When I try running the server or opening the console I get this error:

/initializers/aws.rb:1:in `': uninitialized constant AWS (NameError)

7 Answers 7

114

If you are receiving this error and you have the "aws-sdk" gem installed, you likely have upgraded to version 2 of the aws-sdk gem unintentionally. Version 2 uses the Aws namespace, not AWS. This allows version 1 and version 2 to be used in the same application.

See this blog post for more information.

2
  • 1
    If you plan to continue to use version one of the SDK do the following as prescribed by the v1 documentation. gem install aws-sdk-v1 and require aws-adk-v1`.
    – SJP
    Commented Feb 12, 2015 at 15:34
  • 2
    This answer is very likely the correct one for folks who currently experience this issue. The original question that I answered predates aws-sdk version 2. Commented Mar 3, 2015 at 0:21
24

You need to install/use the -v1 version of aws-sdk. Simply doing gem 'aws-sdk' or require 'aws-sdk' may use the 2.x version of aws-sdk instead.

To avoid any confusion, for scripts that require 1.x, use:

require 'aws-sdk-v1' # not 'aws-sdk'

And for scripts that require 2.x, use:

gem 'aws-sdk', '~> 2'

as the GitHub documentation indicates.

1
  • This answer was helpful even though I did not use it directly (correct gem version was enough in my case, without the added require line). A good reminder to update gems with caution.
    – maurice
    Commented Jun 28, 2016 at 0:26
17

You might getting this error, because you didn't define the correct aws sdk version in your Gemfile. This can happen while re-bundling old apps with version 1 or 2 installed.

Make sure which version you want to install:

aws-sdk version 3

gem 'aws-sdk', '~> 3'

# call sdk    
Aws.<whatever>

aws-sdk version 2

gem 'aws-sdk', '~> 2'

# call sdk    
Aws.<whatever>

aws-sdk version 1

# version constraint
gem 'aws-sdk', '< 2'

# or 

# use the v1 gem
gem 'aws-sdk-v1'

# call sdk    
AWS.<whatever>

v1 is scoped under AWS and v2 and v3 scoped under Aws => That allows you to run v1 and v2 side by side.

7

It sounds as though either the gem isn't present in your load path or it is not being required.

The entry in your Gemfile should be

gem 'aws-sdk'

This will implicitly do a require 'aws-sdk' as the application initializes, as long as you start the app with bundle exec rails server or bundle exec rails console.

Alternatively, if the above code was in a non-rails application, just place require 'aws-sdk' on the first line.

7
  • Thank you so much! Why did adding require'aws-sdk' on the first line seem to fix it?!? =c ...it was rails!
    – bezzoon
    Commented Apr 3, 2014 at 3:13
  • 1
    This is not the fix. If you have both 1.x and 2.x versions of aws-sdk installed, this line will not necessarily load the proper version. Commented Mar 2, 2015 at 20:44
  • 1
    You are correct, though OP asked this question before version 2 existed. Commented Mar 3, 2015 at 0:20
  • 1
    @cmaitchison can you please tell where is the gemfile located? I know where all gems are but from your answer it looks gemfile is some configuration or init file. Thanks
    – shaffooo
    Commented Apr 18, 2015 at 3:21
  • 2
    @shaffooo If you don't have a Gemfile in the root directory of your project, then Trevor Rowe's answer might be more relevant to your problem. A file named Gemfile is a requirement when you are using the Ruby dependency management tool named Bundler. Commented Apr 18, 2015 at 6:19
4

I encountered this problem in a Chef recipe, so the response below is decidedly Chef-centric.

Amazon released version 2 of the aws-sdk in early February 2015. Version 2 is not entirely backwards compatible with version 1.

So, you must make a decision - are you content with version 1 functionality, or do you want version 2 functionality?

If you are content with version 1, perhaps for the short term, it is necessary to have Chef explicitly load version 1, because by default, it appears to use the latest version. To do this you must specify the version attribute to load in the recipe that loads chef_gem aws-sdk. The modification looks like this (probably implemented in a default.rb for the cookbook in question):

chef_gem "aws-sdk" do
  action :nothing

  # Source:  https://aws.amazon.com/releasenotes/Ruby?browse=1
  version '1.62.0'  

end.run_action(:install)

Update the version in the cookbook's metadata, then upload the cookbook to your Chef server. Update the cookbook version in the environment, then upload the environment to your Chef server.

After convergence, run a gem list on your instance to see the gem versions:

On PowerShell PS C:\Users\Administrator> gem list | select-string aws-sdk

On Linux: gem list | grep -i aws-sdk

These are typical results:

aws-sdk (2.0.27, 1.62.0) 
aws-sdk-core (2.0.27) 
aws-sdk-resources (2.0.27) 
aws-sdk-v1 (1.62.0)

Note that the last one specifies aws-sdk-v1. Now, you must update your recipe to require the older version of aws-sdk. Change this:

require 'aws-sdk'

to this:

require 'aws-sdk-v1'

Update the version in the metadata.rb, upload the cookbook, update the version in the environment file, upload the environment, and you should be good to go after the next convergence.

This blog post contains more details and solutions to this problem: http://ruby.awsblog.com/post/TxFKSK2QJE6RPZ/Upcoming-Stable-Release-of-AWS-SDK-for-Ruby-Version-2

2

I was facing the same problem. One answer worked here without updating the gem.

Simply change wherever required [in th require statement in environment]

require 'aws-sdk'

to

require 'aws-sdk-v1'
0
1

I am not a Ruby expert, but I've solved the same issue by running the below commands.

To remove the installed AWS gems

gem list --no-version --local | grep aws | xargs gem uninstall -aIx

To install the v1 gem which was compatible with my Ruby script:

gem install aws-sdk -v 1.64.0

I agree this is not the recommended way as AWS recommends to use the latest version, but this should be useful for someone who does not want to modify their existing scripts.

Not the answer you're looking for? Browse other questions tagged or ask your own question.