mysql2-lambda Gem with Active Record in a lambda function

Viewed 169

I'm trying to use the mysql2-lambda gem alongside ActiveRecord inside a lambda function and receiving the following error:

{
  "errorMessage": "Error loading the 'mysql2' Active Record adapter. Missing a gem it 
depends on? Could not find 'mysql2' (~> 0.5) among 365 total gem(s)\nChecked in 
'GEM_PATH=/var/task/vendor/bundle/ruby/2.7.0:/opt/ruby/gems/2.7.0:/var/runtime' , 
execute `gem env` for more information",
  "errorType": "Init<LoadError>",
  "stackTrace": []
}

I have created 2 layers (one which loads the activerecord dependency and seems to be working correctly, and one for mysql2-lambda which doesn't). Both zip files have an identical structure:

ruby
  - gems
    - 2.7.0
      - build_info
      - cache
        - ...
      - doc
      - extensions
      - gems
        - <GEM>
          - lib
      - plugins
      - specifications
        - ...

I created the dependency zips inside a docker container with the command:

sudo docker run --rm -v $PWD:/var/layer -w /var/layer amazon/aws-sam-cli-build-image-ruby2.7 bundle install --path=ruby

I then moved some stuff around to create the directory structure above.

The Gemfile used to install the mysql2-lambda dependency from inside docker container is:

# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'mysql2-lambda', require: 'mysql2'

My app.rb is:

require "active_record"
require "mysql2"

ActiveRecord::Base.establish_connection(
  adapter:  'mysql2',
  database: '*****',
  username: '*****',
  password: '*****',
  host:     '*****.*****.eu-west-2.rds.amazonaws.com'
)

def handle(event:, context:)
  {
    statusCode: 200,
    body: {}.to_json
  }
end

Any help would be appreciated, as I'm confused about why the activerecord dependency is loading but not mysql2.

0 Answers
Related