feral icon indicating copy to clipboard operation
feral copied to clipboard

(JS) Expose handler function type to allow manual exporting of Lambda handler

Open hugo-vrijswijk opened this issue 3 years ago • 6 comments

Related to #246

Needs some more docs, maybe some further (manual) testing, and possibly an entirely different implementation. But I wanted to open this PR to have an example and get the discussion flowing.

With this, you can implement and export your lambda as follows:

object Lambda extends IOLambda[String, String] {

  override def handler: Resource[IO, LambdaEnv[IO, String] => IO[Option[String]]] = ???

  @JSExportTopLevel("handler")
  def impl: HandlerFn = handlerFn
}

hugo-vrijswijk avatar Jul 23 '22 17:07 hugo-vrijswijk

If you're really brave I guess ideally we would have some integration tests against the actual AWS JS runtime that verify it works for both CommonJS and ESModules.

I have no idea how I'd even start on this 😅

hugo-vrijswijk avatar Jul 31 '22 13:07 hugo-vrijswijk

I have no idea how I'd even start on this

If you want to do this 😁 start by making a new project with feral and figure out how to run it against SAM locally. Then, write those commands into an sbt task. Then, we make that into a scripted test.

Some of my old work here might be helpful: https://github.com/ChristopherDavenport/js-test/tree/serverless

Anyway just driving by, sorry, will come back later with another review pass :) thanks for all your work!

armanbilge avatar Jul 31 '22 15:07 armanbilge

I wonder if instead of dealing with SAM it would be easier to work with the Node.js runtime directly. https://www.npmjs.com/package/aws-lambda-ric https://github.com/aws/aws-lambda-nodejs-runtime-interface-client

armanbilge avatar Aug 01 '22 08:08 armanbilge

Cool, I did some investigation. It seems the trick is to install aws-lambda-ric and and aws-lambda-rie.

Then we start the lambda like this:

$ aws-lambda-rie aws-lambda-ric app.handler

where app.js contains

exports.handler = async (event, context) => {
  return 'Hello World!';
}

which can be invoked like:

$ curl -XPOST "http://localhost:8080/2015-03-31/functions/function/invocations" -d '{}'
"Hello World!"

No pressure to take this on :) but given my previous hiccups with exports I would like to add this integration test just to be sure that everything works.

armanbilge avatar Aug 01 '22 09:08 armanbilge

Hmm, this is confusing.

  • https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/44

armanbilge avatar Aug 01 '22 09:08 armanbilge

Ah, instead of installing the ric and rie seems we can directly invoke the docker image. https://hub.docker.com/r/amazon/aws-lambda-nodejs

armanbilge avatar Aug 01 '22 10:08 armanbilge