Peeling back the
lambda layers
Who am I
Javascript developer
Forsite -> Accordo
Now learning ML
Working from home
• Layers 🍰
• Lambda functions
• Containers
• Lambda Layers
• Case study: PDF generation on Lambda
• The problem
• The solution
• Adding more layers to the solution
• Conclusions 🤔
Faas: Functions as Service
• Pick a runtime
• Write your code
• Upload code + dependencies
The metaphor
Lambda functions have layers
Lambda function lifecycle
• Cold start
• Starts a new container
• AWS downloads your code & Extracts it to dir /var/task
• AWS bootstraps the runtime ( nodejs, python etc)
• Hot start
• Runs your handler code
Lambda Layers
Lambda Layers
• Another way to add code to your function
Lambda Layers
• Another way to add code to your function
Lambda Layers
• 2 stages
• Create layer/s
• Use layer in your lambda function
Lambda Layers
• Upload a zip, just like a function bundle
• This creates a Layer ARN
• Create a layer
Lambda Layers
• Use a layer
• Reference a layer ARN in function definition
• When your function runs
• Layer code is downloaded to /opt/<layer_name>
• Your function has access to any files in the layer
serverless.yml
Lambda Layer
Limits
• Layers count toward max unzipped limit of 250mb
• Max 5 layers per function
• Layers are uploaded for a region and a runtime
• e.g us-west-2/node:8.11.1
Lambda Layer
• What's nice?
• Simple way to reuse dependencies in your functions
• Separate your dependencies into layers
• Smaller bundles to deploy each time
• What’s not nice?
• No semantic versioning, just increments witch each deploy 1,2,3,4,5.
Not so bad
• Now your function isn’t Bring Your Own Code, doesn’t own all its
dependencies
Stepping outside the
Runtime
PDF generation on Lambda
• Backend service on EC2 (
Beanstalk )
• Generated pdf reports for
customers
• Multi language support - Non
English fonts!
• New Features were planned
PDF generation on Lambda
Original solution
• Node express app
• Non Node external lib for HTML -
> PDF generation: wkhtmltopdf
• Multi language fonts: Thai, Jap,
Korean
• To render fonts with wkhtmltopdf
we need to install system fonts
with fontconfig
PDF generation on Lambda
• Tech Debt to be paid
• Sometimes the EC2 instance would
restart and lose the font cache !
• To fix
• Ssh into live instance ( dev, uat,
prod ) 😰
• Manually install the fontconfig fonts
from the terminal 😭
• Non standard deployment model, only
EC2 instance
Migrate to Lambda
• New Features planned
• Good candidate
• It is stateless web-server
• Not too many requests, probably
save money
• Devops
• Doesn’t follow the standard
deployment model
• Teams know serverless
Migrate to Lambda
• What we need to solve:
• Node Express app -> handler function
• Wrap It with npm package
• don’t want to rewrite the whole thing
• External Binary: wkhtmltopdf
• Need to have the correct binary for lambda container OS!
• What distro is closest to amazon linux?
• System Fonts: Fontconfig
• Wkhtmltopdf relies on system fonts
• Linux uses fontconfig lib to render
• Need to build the correct font files!
• How will we test it?
A note on Testing
• Lambda function testing is usually great!
• Exact runtime version as lambda
• Event mocking libraries e.g. lambda-local
• Our integration tests give great confidence
• What if we are
using non
supported
binaries?
Implementation
• Wrap Express app -> function
• Mimic lambda container runtime:
• Check our binary executes
• Build our fonts and check they render
• Local testing
• Mock a lambda event and run our function from the
lambda container
Implementation
• Wrap Express app -> function ✅
• Use an npm package to wrap our server
Lambda docker container
• Mimic lambda container runtime:
• Check our binary executes ✅
• Community made container that aims to mimic the runtime
• Run docker container and Check Binary
• We can test if it works locally!
• You could also build from source…
Implementation
• Mimic lambda container runtime:
• Build our fonts and check they render
Lambda docker container
• Build our fonts in this
container
• Way better than
manual
• Reproducible 🙌
• Reference the location
of our new fonts
Implementation
• Wrap Express app -> function ✅
• Mimic lambda container runtime:
• Check our binary executes ✅
• Build our fonts and check they render ✅
• Local testing
• Mock a lambda event and run our function from the
lambda container
Implementation
• Local testing
• Mount all our code, font files, dependencies in docker
container
• /var/task/
• Use lambda-local package to mock a lambda event
• it works 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 ( sorry no demo )
Deploy
• Our bundle
• Node Code
• Fontconfig files
• Binary: wkhtmltopdf
• Works in live
* Generated from lambda function 🙌
The end
What about Lambda Layers?
• It already works, we could stop here…
• We added a lot of complexity
• Could reuse these bespoke lambda dependencies
• They change infrequently just add to bundle size
What about Lambda Layers?
What our function
looks like now
What about Lambda Layers?
What our function
could look like with
lambda layers
Build our layers
• build files
• Upload with
serverless
Add layers to Function
• Get the layer ARN
• Add to function definition
• Add env vars to reference
layer code
Add layers to Function
Separate
Local Testing
Local Testing
• Layers only accessible in LIVE
• Our function doesn’t include all its dependencies
locally
• Now our local testing doesn’t have the layer code??
• Local lambda mocking libraries support layers?
Local Testing
• We can hack a solution together
• download layers with aws-cli
• mount them in our local lambda container like AWS
does
• /opt/<layer_name>
• Run like before
CONCLUSIONS
Container to serverless
• Deployed non supported binary 🌈
• Replicated lambda env on our local for testing
• Possibilities infinite and bounded ( 250mb max size
unzipped)
CONCLUSIONS
• What did we achieve with lambda layers?
• Share our layer code
• Share to other functions super easy
• standard dependencies across services . e.g. standardised for security
• Separated our concerns
• Decreased our bundle size
• Added local testing overhead ( but already have
extra with non supported binaries )
A healthy compromise
• Between
• Container orchestration overhead
• managing the added complexities of non standard
dependencies
The complexity doesn’t go away, but it might be worth it
for your teams
A healthy compromise

Peeling back the Lambda layers

  • 1.
  • 2.
    Who am I Javascriptdeveloper Forsite -> Accordo Now learning ML Working from home
  • 3.
    • Layers 🍰 •Lambda functions • Containers • Lambda Layers • Case study: PDF generation on Lambda • The problem • The solution • Adding more layers to the solution • Conclusions 🤔
  • 4.
    Faas: Functions asService • Pick a runtime • Write your code • Upload code + dependencies
  • 5.
  • 8.
    Lambda function lifecycle •Cold start • Starts a new container • AWS downloads your code & Extracts it to dir /var/task • AWS bootstraps the runtime ( nodejs, python etc) • Hot start • Runs your handler code
  • 9.
  • 10.
    Lambda Layers • Anotherway to add code to your function
  • 11.
    Lambda Layers • Anotherway to add code to your function
  • 12.
    Lambda Layers • 2stages • Create layer/s • Use layer in your lambda function
  • 13.
    Lambda Layers • Uploada zip, just like a function bundle • This creates a Layer ARN • Create a layer
  • 14.
    Lambda Layers • Usea layer • Reference a layer ARN in function definition • When your function runs • Layer code is downloaded to /opt/<layer_name> • Your function has access to any files in the layer serverless.yml
  • 15.
    Lambda Layer Limits • Layerscount toward max unzipped limit of 250mb • Max 5 layers per function • Layers are uploaded for a region and a runtime • e.g us-west-2/node:8.11.1
  • 16.
    Lambda Layer • What'snice? • Simple way to reuse dependencies in your functions • Separate your dependencies into layers • Smaller bundles to deploy each time • What’s not nice? • No semantic versioning, just increments witch each deploy 1,2,3,4,5. Not so bad • Now your function isn’t Bring Your Own Code, doesn’t own all its dependencies
  • 17.
  • 18.
    PDF generation onLambda • Backend service on EC2 ( Beanstalk ) • Generated pdf reports for customers • Multi language support - Non English fonts! • New Features were planned
  • 19.
    PDF generation onLambda Original solution • Node express app • Non Node external lib for HTML - > PDF generation: wkhtmltopdf • Multi language fonts: Thai, Jap, Korean • To render fonts with wkhtmltopdf we need to install system fonts with fontconfig
  • 20.
    PDF generation onLambda • Tech Debt to be paid • Sometimes the EC2 instance would restart and lose the font cache ! • To fix • Ssh into live instance ( dev, uat, prod ) 😰 • Manually install the fontconfig fonts from the terminal 😭 • Non standard deployment model, only EC2 instance
  • 21.
    Migrate to Lambda •New Features planned • Good candidate • It is stateless web-server • Not too many requests, probably save money • Devops • Doesn’t follow the standard deployment model • Teams know serverless
  • 22.
    Migrate to Lambda •What we need to solve: • Node Express app -> handler function • Wrap It with npm package • don’t want to rewrite the whole thing • External Binary: wkhtmltopdf • Need to have the correct binary for lambda container OS! • What distro is closest to amazon linux? • System Fonts: Fontconfig • Wkhtmltopdf relies on system fonts • Linux uses fontconfig lib to render • Need to build the correct font files! • How will we test it?
  • 23.
    A note onTesting • Lambda function testing is usually great! • Exact runtime version as lambda • Event mocking libraries e.g. lambda-local • Our integration tests give great confidence • What if we are using non supported binaries?
  • 24.
    Implementation • Wrap Expressapp -> function • Mimic lambda container runtime: • Check our binary executes • Build our fonts and check they render • Local testing • Mock a lambda event and run our function from the lambda container
  • 25.
    Implementation • Wrap Expressapp -> function ✅ • Use an npm package to wrap our server
  • 26.
    Lambda docker container •Mimic lambda container runtime: • Check our binary executes ✅ • Community made container that aims to mimic the runtime • Run docker container and Check Binary • We can test if it works locally! • You could also build from source…
  • 27.
    Implementation • Mimic lambdacontainer runtime: • Build our fonts and check they render
  • 28.
    Lambda docker container •Build our fonts in this container • Way better than manual • Reproducible 🙌 • Reference the location of our new fonts
  • 29.
    Implementation • Wrap Expressapp -> function ✅ • Mimic lambda container runtime: • Check our binary executes ✅ • Build our fonts and check they render ✅ • Local testing • Mock a lambda event and run our function from the lambda container
  • 30.
    Implementation • Local testing •Mount all our code, font files, dependencies in docker container • /var/task/ • Use lambda-local package to mock a lambda event • it works 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 ( sorry no demo )
  • 31.
    Deploy • Our bundle •Node Code • Fontconfig files • Binary: wkhtmltopdf • Works in live * Generated from lambda function 🙌
  • 32.
  • 33.
    What about LambdaLayers? • It already works, we could stop here… • We added a lot of complexity • Could reuse these bespoke lambda dependencies • They change infrequently just add to bundle size
  • 34.
    What about LambdaLayers? What our function looks like now
  • 35.
    What about LambdaLayers? What our function could look like with lambda layers
  • 36.
    Build our layers •build files • Upload with serverless
  • 37.
    Add layers toFunction • Get the layer ARN • Add to function definition • Add env vars to reference layer code
  • 38.
    Add layers toFunction
  • 39.
  • 40.
  • 41.
    Local Testing • Layersonly accessible in LIVE • Our function doesn’t include all its dependencies locally • Now our local testing doesn’t have the layer code?? • Local lambda mocking libraries support layers?
  • 42.
    Local Testing • Wecan hack a solution together • download layers with aws-cli • mount them in our local lambda container like AWS does • /opt/<layer_name> • Run like before
  • 43.
    CONCLUSIONS Container to serverless •Deployed non supported binary 🌈 • Replicated lambda env on our local for testing • Possibilities infinite and bounded ( 250mb max size unzipped)
  • 44.
    CONCLUSIONS • What didwe achieve with lambda layers? • Share our layer code • Share to other functions super easy • standard dependencies across services . e.g. standardised for security • Separated our concerns • Decreased our bundle size • Added local testing overhead ( but already have extra with non supported binaries )
  • 45.
    A healthy compromise •Between • Container orchestration overhead • managing the added complexities of non standard dependencies The complexity doesn’t go away, but it might be worth it for your teams
  • 46.