Building / bundling Bucklescript for AWS Lambda


#1

Hi,

I’ve been trying to build a simple AWS Lambda function loosely based on the examples in this repository.

In the examples in the repo, it looks like they use the Serverless framework for bundling but I would like to setup a minimal example which did not rely on that framework.

I am not very familiar with the world of Node (or Javascript in general!) but, after some vague googling, I came across an example using Webpack to do for plain JS code.

Do I need to use an additional tool for bundling or is there something within the Bucklescript / ReasonML ecosystem that I can use?

Thanks

Michael


#2

I should also mention that if anyone has experience with using Bucklescript as part of AWS serverless applications (particularly with build and deployment) and is open to contracting opportunities, I would pleased to hear from you via direct message.

Michael


#3

The example is using serverless mostly to deal with the aws administration (create the lambda function, set the params, …) IIRC. It should not be needed to do the bundling. This part is done by webpack. And serverless simply takes care or uploading the js file generated by webpack to aws.


#4

Thank you for your reply, this repo and your blog post have been really helpful!

Ah, ok - I incorrectly thought serverless-webpack might be doing some additional steps. I tried bundling with webpack (with the serveless dependency removed) but when I tested the lambda I was getting an error which indicating the argument to the function was of the wrong type - it looks like this was unrelated.

Could I ask a couple of follow up questions:

  1. Have you tried the approach of including dependencies as Lambda Layers?
  2. Are you still using Bucklescript with Lambda in a professional capacity and do you have any ‘lessons learned’ so far?

Thanks,

Michael


#5

Going from BuckleScript + Node.js to native Reason/OCaml will be a big leap, but if you feel like it there’s https://github.com/anmonteiro/aws-lambda-ocaml-runtime/ by @anmonteiro with some nice examples written in Reason


#6

I don’t know about lambda layers, sorry.

I stopped using lambdas with bucklescript just a few weeks ago after more than a year running without a need for a new deployment. It’s not because of a technical choice but because the project was retired. If I had to try again today, I’d try to use some native ocaml though. The important things I learnt during the project are more related to aws than bucklescript. Like every queue/messaging operation can fail, the lambdas can take forever to be started. Sometimes the lambdas fail and are restarted. So it’s more about writing an application which fits the aws way of thinking.


#7

I love ReasonML but if I were to reach for a typed language to write lambda functions I would still reach for golang just for the better support from Amazon.

I (personally) would avoid layers because the code generated by Bucklescript can be coupled to the particular compiler version, and I would want my CI/CD pipeline testing against my code with fully bundled dependencies built by the same bs-platform / toolchain.

My understanding is also that there are still other reasons bundling your lambda JS is desirable: https://medium.com/capital-one-tech/applying-minification-and-uglification-to-aws-lambda-functions-dbc7ad75241

Also, pretty much /signed for all of @Khady’s points and experiences. My mental model of lambda settled quickly but I still feel like I’m learning effective SNS/SQS practices.


#8

I did some experiments with ReasonML & Lambdas about a year ago. Personally really enjoyed it, but didn’t have the time or energy to push it through at work, so I didn’t get much further than validating it’s doable.

Biggest downside that was quite obvious back then, was the bundle size and the fact that some Lambda scenarios has size restrictions, especially Lambda@Edge if I remember correctly.

Anyways enough ramblings, here are a couple of related projects I started on, heavily dependent on @Khady’s work:

Maybe there’s some valuable tricks in there that helps you out.


#9

I’m doing some experiments with bucklescript and lambdas (using serverless), I don’t know what changed since then, but I have bundle sizes of a few dozen kB (30kB with BS7.2.2 and decco) using serverless-webpack, versus 40 MB without bundler, it’s absolutely amazing!


#10

for those interested, I’ve published a demo repo of bundling BS for AWS Lambda with serverless webpack:


using bindings to AWS Lambda for BS > 7: