Search code examples
rubypostgresqlactiverecordaws-lambdabundler

AWS Lambda: Could not load 'active_record/connection_adapters/postgresql_adapter'


I'm using AWS Lambda to run a simple ruby script with a github project that has a framework that suits my needs.

After facing several ruby and bundler version conflicts due to the limitations of Traveling Ruby that is part of the stack.

I'm able to run the script locally successfully but then when I package it and update the Lambda function I get a weird error on the AWS console as I test it:

START RequestId: feab102b-5da5-11e7-bf18-739c73eb4ef0 Version: $LATEST
2017-06-30T15:08:39.144Z    feab102b-5da5-11e7-bf18-739c73eb4ef0    Starting process: ./app
2017-06-30T15:08:51.403Z    feab102b-5da5-11e7-bf18-739c73eb4ef0    stderr:
/var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
2017-06-30T15:08:51.403Z    feab102b-5da5-11e7-bf18-739c73eb4ef0    stderr:
: Could not load 'active_record/connection_adapters/postgresql_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile. (LoadError)
from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `block in require'
from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:240:in `load_dependency'
from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
from /var/task/lib/vendor/ruby/2.1.0/gems/pg-0.21.0/lib/pg.rb:4:in `<top (required)>'
from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `block in require'
from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:240:in `load_dependency'
from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
from /var/task/lib/vendor/ruby/2.1.0/gems/activerecord-4.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:17:in `<top (required)>'
from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `block in require'
from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:240:in `load_dependency'
from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
from /var/task/lib/vendor/ruby/2.1.0/gems/activerecord 4.2.9/lib/active_record/connection_adapters/connection_specification.rb:175:in `spec'
from /var/task/lib/vendor/ruby/2.1.0/gems/activerecord 4.2.9/lib/active_record/connection_handling.rb:50:in `establish_connection'
from /var/task/lib/app/app.rb:12:in `<main>'

2017-06-30T15:08:51.744Z    feab102b-5da5-11e7-bf18-739c73eb4ef0    {"errorMessage":"Process \"./app\" exited with code: 1"}
END RequestId: feab102b-5da5-11e7-bf18-739c73eb4ef0

In the db/config.yml(which it calls config/database.yml as it should be defaulted to rails tree) I have set adapter: postgresql and the all other required settings.

The code is entirely stored on a .zip file and I've unpacked it and was able to find the lib/vendor/ruby/2.1.0/gems/activerecord-4.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb which I guess is extracted on the /var/task within the Lambda environment as it shows in logs.

The only thing I can think of is that at the moment of the require it is getting lost on the base path and therefore not finding the file to load.

I'd appreciate any insights of anything I may be missing or any clues towards fixing this.

Thanks!


Solution

  • It ends up that the error above is misleading because activerecord actually rescues the exception and raises a customized error message and therefore does not reflect the reality happening somewhere else in the stack while loading the pg gem. The actual error is this:

    START RequestId: a9e88496-6005-11e7-924c-e7c37f2df3e0 Version: $LATEST
    2017-07-03T15:38:30.977Z    a9e88496-6005-11e7-924c-e7c37f2df3e0    Starting process: ./app
    2017-07-03T15:38:42.337Z    a9e88496-6005-11e7-924c-e7c37f2df3e0    stderr:
    /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
    2017-07-03T15:38:42.456Z    a9e88496-6005-11e7-924c-e7c37f2df3e0    stderr:
    : libpq.so.5: cannot open shared object file: No such file or directory - /var/task/lib/vendor/ruby/2.1.0/extensions/x86_64-linux/2.1.0-static/pg-0.21.0/pg_ext.so - libpq.so.5: cannot open shared object file: No such file or directory - /var/task/lib/vendor/ruby/2.1.0/extensions/x86_64-linux/2.1.0-static/pg-0.21.0/pg_ext.so - Could not load 'active_record/connection_adapters/postgresql_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile. (LoadError)
    from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `block in require'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
    from /var/task/lib/vendor/ruby/2.1.0/gems/pg-0.21.0/lib/pg.rb:4:in `<top (required)>'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `block in require'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activerecord-4.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:17:in `<top (required)>'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `block in require'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activerecord-4.2.9/lib/active_record/connection_adapters/connection_specification.rb:175:in `spec'
    from /var/task/lib/vendor/ruby/2.1.0/gems/activerecord-4.2.9/lib/active_record/connection_handling.rb:50:in `establish_connection'
    from /var/task/lib/app/app.rb:12:in `<main>'
    
    2017-07-03T15:38:42.676Z    a9e88496-6005-11e7-924c-e7c37f2df3e0    {"errorMessage":"Process \"./app\" exited with code: 1"}
    END RequestId: a9e88496-6005-11e7-924c-e7c37f2df3e0
    

    The real reason is also hidden behind this error. It is not that the pg_ext.so was not found as it is there in this very path.

    After some more research, the final issue is that libpg.so.5 itself is the one missing and to fix that I had to add it within Traveling Ruby's libraries path.

    Hope that can become helpful to others in the future!