SlideShare a Scribd company logo
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
Peeling back the Lambda layers
Peeling back the Lambda 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

More Related Content

PPT
JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript
PDF
Deliver docker containers continuously on aws
PDF
Jazoon2013 type script
PDF
9th docker meetup 2016.07.13
PDF
RESTFul Tools For Lazy Experts - CFSummit 2016
PDF
CBDW2014 - Down the RabbitMQ hole with ColdFusion
PDF
Understanding Akka Streams, Back Pressure, and Asynchronous Architectures
PDF
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript
Deliver docker containers continuously on aws
Jazoon2013 type script
9th docker meetup 2016.07.13
RESTFul Tools For Lazy Experts - CFSummit 2016
CBDW2014 - Down the RabbitMQ hole with ColdFusion
Understanding Akka Streams, Back Pressure, and Asynchronous Architectures
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02

What's hot (20)

PDF
Ruby and Rails short motivation
KEY
Scala and Lift
PPTX
Coordinating Micro-Services with Spring Cloud Contract
PDF
Rethinking the debugger
PDF
meetPHP#8 - PHP startups prototypes
PDF
Kafka as a message queue
PDF
Stackato v6
PPTX
ITB2015 - Go Commando with CommandBox CLI
PDF
Indroduction to Web Application
PPTX
Ruby on Rails All Hands Meeting
PDF
PDF
Introduction to Scala for Java Developers
PPTX
Moving Gigantic Files Into and Out of the Alfresco Repository
PPTX
What's the "right" PHP Framework?
PPTX
Real world Scala hAkking NLJUG JFall 2011
PDF
Core FP Concepts
PDF
The future of paas is serverless
PPT
Web development basics (Part-5)
PDF
Ractor's speed is not light-speed
PPTX
Best Practices for Running Kafka on Docker Containers
Ruby and Rails short motivation
Scala and Lift
Coordinating Micro-Services with Spring Cloud Contract
Rethinking the debugger
meetPHP#8 - PHP startups prototypes
Kafka as a message queue
Stackato v6
ITB2015 - Go Commando with CommandBox CLI
Indroduction to Web Application
Ruby on Rails All Hands Meeting
Introduction to Scala for Java Developers
Moving Gigantic Files Into and Out of the Alfresco Repository
What's the "right" PHP Framework?
Real world Scala hAkking NLJUG JFall 2011
Core FP Concepts
The future of paas is serverless
Web development basics (Part-5)
Ractor's speed is not light-speed
Best Practices for Running Kafka on Docker Containers
Ad

Similar to Peeling back the Lambda layers (20)

PDF
AWS Lambda How to upload and load gems fast
PDF
Serverless in Java Lessons learnt
PDF
Serverless in java Lessons learnt
PDF
PPTX
Building serverless app_using_aws_lambda_b4usolution
PDF
TheFutureIsDynamic-BoxLang-CFCamp2024.pdf
PPTX
Fast Deployments to Multiple Golang Lambda Functions
PDF
aws lambda & api gateway
PDF
Building serverless apps with MongoDB Atlas and AWS Lambda
PDF
The Architect Way - JSCamp.asia 2012
PDF
ITB2024 - Keynote Day 1 - Ortus Solutions.pdf
PPTX
Container Orchestration for .NET Developers
PDF
Getting Started with AWS Lambda & Serverless Cloud
PDF
Aws-What You Need to Know_Simon Elisha
PDF
Lessons learned from building Eclipse-based add-ons for commercial modeling t...
PDF
Kafka Summit SF 2017 - Kafka and the Polyglot Programmer
PPTX
What's new in ASP.NET vNext
PPTX
Node.js Development with Apache NetBeans
PDF
AWS Lambda Functions A Comprehensive Guide
PPTX
Serverless design considerations for Cloud Native workloads
AWS Lambda How to upload and load gems fast
Serverless in Java Lessons learnt
Serverless in java Lessons learnt
Building serverless app_using_aws_lambda_b4usolution
TheFutureIsDynamic-BoxLang-CFCamp2024.pdf
Fast Deployments to Multiple Golang Lambda Functions
aws lambda & api gateway
Building serverless apps with MongoDB Atlas and AWS Lambda
The Architect Way - JSCamp.asia 2012
ITB2024 - Keynote Day 1 - Ortus Solutions.pdf
Container Orchestration for .NET Developers
Getting Started with AWS Lambda & Serverless Cloud
Aws-What You Need to Know_Simon Elisha
Lessons learned from building Eclipse-based add-ons for commercial modeling t...
Kafka Summit SF 2017 - Kafka and the Polyglot Programmer
What's new in ASP.NET vNext
Node.js Development with Apache NetBeans
AWS Lambda Functions A Comprehensive Guide
Serverless design considerations for Cloud Native workloads
Ad

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Cloud computing and distributed systems.
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
KodekX | Application Modernization Development
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Big Data Technologies - Introduction.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced methodologies resolving dimensionality complications for autism neur...
Cloud computing and distributed systems.
Spectral efficient network and resource selection model in 5G networks
Chapter 3 Spatial Domain Image Processing.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
MYSQL Presentation for SQL database connectivity
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
Understanding_Digital_Forensics_Presentation.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Big Data Technologies - Introduction.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The Rise and Fall of 3GPP – Time for a Sabbatical?
The AUB Centre for AI in Media Proposal.docx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf

Peeling back the Lambda layers

  • 2. Who am I Javascript developer 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 as Service • Pick a runtime • Write your code • Upload code + dependencies
  • 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
  • 10. Lambda Layers • Another way to add code to your function
  • 11. Lambda Layers • Another way to add code to your function
  • 12. Lambda Layers • 2 stages • Create layer/s • Use layer in your lambda function
  • 13. Lambda Layers • Upload a zip, just like a function bundle • This creates a Layer ARN • Create a layer
  • 14. 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
  • 15. 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
  • 16. 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
  • 18. PDF generation on Lambda • Backend service on EC2 ( Beanstalk ) • Generated pdf reports for customers • Multi language support - Non English fonts! • New Features were planned
  • 19. 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
  • 20. 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
  • 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 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?
  • 24. 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
  • 25. Implementation • Wrap Express app -> 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 lambda container 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 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
  • 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 🙌
  • 33. 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
  • 34. What about Lambda Layers? What our function looks like now
  • 35. What about Lambda Layers? What our function could look like with lambda layers
  • 36. Build our layers • build files • Upload with serverless
  • 37. Add layers to Function • Get the layer ARN • Add to function definition • Add env vars to reference layer code
  • 38. Add layers to Function
  • 41. 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?
  • 42. 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
  • 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 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 )
  • 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