(JS) Expose handler function type to allow manual exporting of Lambda handler
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
}
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 😅
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!
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
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.
Hmm, this is confusing.
- https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/44
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