'SQLite in an AWS Lambda Ruby function

I am attempting to use SQLite within an AWS Lambda Ruby function, but having trouble with the sqlite3 gem. Has anyone been able to do this successfully?


I have a simple Gemfile:

source "https://rubygems.org"

gem "activerecord", "~> 7.0"

gem "sqlite3", "~> 1.4"

After a few false starts trying to run the function on Lambda itself, I looked at this question on Ruby gems with native extensions on AWS Lambda, so am trying to package dependencies using a Lambda-like Docker image. bundle install fails at the sqlite3 native extension step. When I try to install the gem by itself:

docker run --rm -v "$PWD":/var/task lambci/lambda:build-ruby2.7 gem install sqlite3

I see:

Building native extensions. This could take a while...
ERROR:  Error installing sqlite3:
    ERROR: Failed to build gem native extension.

    current directory: /var/runtime/gems/sqlite3-1.4.2/ext/sqlite3
/var/lang/bin/ruby -I /var/lang/lib/ruby/site_ruby/2.7.0 -r ./siteconf20220430-1-n5ip9q.rb extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (the
location where your sqlite3 shared library is located).

The docs for the Docker image seem to indicate that sqlite-devel has been installed, though. When I ask which -a sqlite3, it says:

/usr/bin/sqlite3
/bin/sqlite3

When I try to build --with-sqlite3-dir=/bin/sqlite3 or --with-sqlite3-dir=/usr/bin/sqlite3:

Building native extensions with: '--with-sqlite3-dir=/bin/sqlite3'
This could take a while...
ERROR:  Error installing sqlite3:
    ERROR: Failed to build gem native extension.

    current directory: /var/runtime/gems/sqlite3-1.4.2/ext/sqlite3
/var/lang/bin/ruby -I /var/lang/lib/ruby/site_ruby/2.7.0 -r ./siteconf20220430-1-1rgbak9.rb extconf.rb --with-sqlite3-dir\=/bin/sqlite3
checking for sqlite3.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/var/lang/bin/$(RUBY_BASE_NAME)
    --with-sqlcipher
    --without-sqlcipher
    --with-sqlite3-config
    --without-sqlite3-config
    --with-pkg-config
    --without-pkg-config
    --with-sqlcipher
    --without-sqlcipher
    --with-sqlite3-dir
    --with-sqlite3-include
    --without-sqlite3-include=${sqlite3-dir}/include
    --with-sqlite3-lib
    --without-sqlite3-lib=${sqlite3-dir}/lib
/var/lang/lib/ruby/2.7.0/mkmf.rb:471:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /var/lang/lib/ruby/2.7.0/mkmf.rb:613:in `try_cpp'
    from /var/lang/lib/ruby/2.7.0/mkmf.rb:1177:in `block in find_header'
    from /var/lang/lib/ruby/2.7.0/mkmf.rb:971:in `block in checking_for'
    from /var/lang/lib/ruby/2.7.0/mkmf.rb:361:in `block (2 levels) in postpone'
    from /var/lang/lib/ruby/2.7.0/mkmf.rb:331:in `open'
    from /var/lang/lib/ruby/2.7.0/mkmf.rb:361:in `block in postpone'
    from /var/lang/lib/ruby/2.7.0/mkmf.rb:331:in `open'
    from /var/lang/lib/ruby/2.7.0/mkmf.rb:357:in `postpone'
    from /var/lang/lib/ruby/2.7.0/mkmf.rb:970:in `checking_for'
    from /var/lang/lib/ruby/2.7.0/mkmf.rb:1176:in `find_header'
    from extconf.rb:68:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /var/runtime/extensions/x86_64-linux/2.7.0/sqlite3-1.4.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /var/runtime/gems/sqlite3-1.4.2 for inspection.
Results logged to /var/runtime/extensions/x86_64-linux/2.7.0/sqlite3-1.4.2/gem_make.out

This is where I am stuck. I note the message:

/var/lang/lib/ruby/2.7.0/mkmf.rb:471:in 'try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.

But not sure how to make headway on that. I'm also pretty new to Docker and have been having trouble trying to read mkmf.log; I am getting No such file or directory when I try to cat it.


I found some questions/answers that indicate people have gotten SQLite working with Node and Python Lambda functions, but I don't see any discussion of Ruby functions. I also found this layer for adding SQLite support in Python functions. But didn't find any such layer for Ruby.

Is a Ruby layer necessary, or is there something else I am missing? Thanks very much!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source