Accessing Jets models in simple Functions


#1

Is there a way to access my ActiveRecord models in a Jets simple Function.

In this use case I just want to write a Lambda function that can access my models, but not an API gateway and all that stuff. I tried require '../models/my_model' but it didn’t work. Any ideas?


#2

You would have to require ActiveRecord and set it up. Similar to using ActiveRecord outside of Rails. This may help: https://stackoverflow.com/questions/1643875/how-to-use-activerecord-in-a-ruby-script-outside-rails

You may consider doing this in a Jets Job though. It does a full Jets boot and will have access.


#3

Thanks for replying, @tung.

So I got it to work by defining my function like this in app/functions:

require 'bundler/setup'
require 'jets'
Jets.once  # runs once in lambda execution context

def my_function(event:, context:)
  ...
end

This works if I edit it directly in the Lambda console and test. However I get errors on deploy if I try to deploy from Jets cli. The error seems weird:

Traceback (most recent call last):
	36: from /Users/nate/.rbenv/versions/2.5.3/bin/jets:23:in `<main>'
	35: from /Users/nate/.rbenv/versions/2.5.3/bin/jets:23:in `load'
	...
	 2: from /Users/nate/.rbenv/versions/2.5.3/lib/ruby/2.5.0/psych.rb:497:in `load_file'
	 1: from /Users/nate/.rbenv/versions/2.5.3/lib/ruby/2.5.0/psych.rb:497:in `open'
/Users/nate/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/jets-2.3.0/lib/jets/lambda/function_constructor.rb:38:in `module_eval': No such file or directory @ rb_sysopen - /private/tmp/jets/konnected-cloud/stage/code/handlers/data.yml (Errno::ENOENT)

Any thoughts on what’s going on here?


#4

I see. Think when there are only simple functions in the app, it doesn’t generate the handlers/data.yml because there are no shims to be generated at all.

During the jets deploy process a typical Jets app generates the handler shims and the handlers/data.yml file for later referencing the s3 bucket. But guessing your app has only simple functions.

From what can tell from that stack trace, it’s probably bombing here:

So thinking a fix is to maybe not run the TmpLoader for this use-case.

Took a quick look at the Jets code after that point. The rack server seems to already have checks to not run any more logic. So think that may be it. Will dig into it.


#5

Thanks for the pointer! You were right. I was able to get my function to work as expected by changing my code in app/functions/my_function.rb to this:

require 'bundler/setup'
require 'jets'
Jets.boot
Jets.override_lambda_ruby_runtime

def my_function(event:, context:)
  ...
end

This lets me access all of my models, gems and libraries in this simple function and doesn’t error on deploy.

In this particular case, I just wanted a Lambda function without the extra “stuff” that comes along with a Controller or Job. My use case is that this Lambda function is called by an AWS IoT Authorizer, so it does not need an API endpoint or any scheduling capabilities. The IoT Authorizer passes a token to the function, which I then use to look-up and authenticate a device via custom logic to AWS IoT.

Thanks!


#6

Fixed it so that Jets.once works with a simple function now: https://github.com/tongueroo/jets/pull/386