AWS Lambda:无法加载 'active_record/connection_adapters/postgresql_adapter'

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

我正在使用 AWS Lambda 运行 一个简单的 ruby 脚本,其中 github project 具有适合我需要的框架。

由于作为堆栈一部分的 Traveling Ruby 的限制,在遇到几个 ruby 和捆绑程序版本冲突之后。

我能够在本地成功 运行 脚本,但是当我打包它并更新 Lambda 函数时,我在测试它时在 AWS 控制台上收到一个奇怪的错误:

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

db/config.yml(它称为 config/database.yml,因为它应该默认为 rails 树)我已经设置了 adapter: postgresql 和所有其他必需的设置。

代码完全存储在 .zip 文件中,我已经解压它并能够找到 lib/vendor/ruby/2.1.0/gems/activerecord-4.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb,我猜它是在 Lambda 中的 /var/task 上提取的日志中显示的环境。

我唯一能想到的是,在 require 的那一刻,它在基本路径上迷路了,因此找不到要加载的文件。

对于我可能遗漏的任何内容或解决此问题的任何线索,我将不胜感激。

谢谢!

最终上面的错误具有误导性,因为 activerecord 实际上挽救了异常并引发了自定义错误消息,因此没有反映加载 [=12= 时堆栈中其他地方发生的真实情况] gem。实际错误是这样的:

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

真正的原因也隐藏在这个错误的背后。并不是找不到 pg_ext.so,因为它就在这条路径中。

经过更多研究,最后一个问题是 libpg.so.5 本身就是缺失的,为了解决这个问题,我必须将它添加到 Traveling Ruby 的库路径中。

希望以后能对别人有所帮助!