Lambda Layers FTW!

At Serverless on Stage #16, I explored in depth the new AWS Lambda Layers feature.

Creating a Lambda Layer Link to heading

Bundling code and libraries into a Lambda Layer is very easy. Just create a ZIP file with the correct directory structure, libraries must reside in a pre-defined path to be referenced in Lambda code.

Once deployed to the Lambda container, a Layer is extracted to /opt. Default paths for import or require statements are language dependent:

  • Node.js: /opt/nodejs/node_modules or /opt/nodejs/node8/node_modules
  • Python: /opt/python or /opt/python/lib/python3.6/site-packages
  • Java: /opt/java/lib
  • Ruby: /opt/ruby/lib or /opt/ruby/gems/2.5.0
  • Binaries (PATH): /opt/bin or /opt/lib

This is how I created a Lambda Layer live during the presentation.

Bunding code in a Lambda Layer

Demo Link to heading

A presentation is nothing without a demo.

I prepared two demos:

  • two Node.js web services providing bike-sharing information. For example the status of BikeMI stations near Milan Duomo is:
$ curl 'https://slu579if63.execute-api.eu-west-3.amazonaws.com/Prod/bikemi?lat=45.460951&lng=9.188376' | jq .
[
  {
    "bikemi": [
      {
        "lng": 9.1868287,
        "lat": 45.462766,
        "name": "Santa Maria Beltrade",
        "bikes": {
          "standard": 8,
          "electric": 2,
          "babySeat": 0
        },
        "emptySlots": 13,
        "distance": 0.23514495567610313
      },
      {
        "lng": 9.1902244,
        "lat": 45.460017,
        "name": "Velasca",
        "bikes": {
          "standard": 18,
          "electric": 2,
          "babySeat": 0
        },
        "emptySlots": 4,
        "distance": 0.17767506585759438
      },
...

Serverless Days 2018 talks

Source code for both demos is available on Github