joshuaflanagan / serverless-ruby-package

serverless plugin to package ruby gems
39 stars 11 forks source link

Getting pg gem to install with serverless-ruby-package #26

Closed nodanaonlyzuul closed 4 years ago

nodanaonlyzuul commented 4 years ago

Thanks

Firstly - thanks so much for serverless-ruby-package.

It's saved the day for me and the orgs I work for time-and-time-again and has opened the door for us to use Ruby for our lambdas (which has...in turn...saved many days).

The Problem

I imagine using serverless-ruby-package to deploy Lambdas that use the pg gem is a well-trod path. But I'm running into problems at build time. I have a Lambda that uses pg (1.2.3) I get the following error on running serverless invoke local with the --docker flag.

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /var/task/vendor/bundle/ruby/2.5.0/gems/pg-1.2.3/ext
/var/lang/bin/ruby -I /var/lang/lib/ruby/site_ruby/2.5.0 -r
./siteconf20201011-1-2uaspy.rb extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** 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.
...SNIP

I understand the heart of this problem is with lambci/lambda:build-ruby2.5 either not including the postgresql-devel and postgresql-libs packages or putting them in an unexpected (for bundler) location.

I've thought of a few different way's it could be solved but each is a slippery slope.

For example...

I could create my own docker image image FROM: lambci/lambda:build-ruby2.5 which yum installs the appropriate libs, but then serverless-ruby-package is hardwired to use lambci/lambda:build-ruby2.X, then I'd have to fork serverless-ruby-package. That's enough work to make me imagine I'm working too hard. Hah.

Do you know a way to install the pg gem and stay on the serverless ==> serverless-ruby-package path?

Example App

You can re-produce this problemby running:

serverless invoke local --docker --function hello --path ./resources/fake-lambda-message.json

In the with-native-extensions branch of nodanaonlyzuul/kinesis-lambda-example

apsoto commented 4 years ago

are you saying that using a custom image as shown in the config example does not work?

https://github.com/joshuaflanagan/serverless-ruby-package#configuration

I'm guessing you're going to need to add a lambda layer or other means to add the native libs to your deployment package

apsoto commented 4 years ago

https://github.com/DrLuke/postgres-libpq-aws-lambda-layer

nodanaonlyzuul commented 4 years ago

Oh @apsoto - I'm such a dope.

It's super easy for me to make my own image, FROM: lambci/lambda:build-ruby2.X which yum installs. I totally missed the documentation (and source!) about customizing the build image via this.config.dockerImage. Chalk that up to being awake way too late.

I'm sure it works fine (haven't had the chance to run it) That's probably the shortest path for my case. But I really appreciate the layer example you provided.

  1. Thanks for a kinder and more patient reply than the question deserved.
  2. I'm going to close this. I'll re-open if this.config.dockerImage is busted for me. (It won't be).