AWS Lambda: how to share code in a monorepo

Written by theburningmonk | Published 2019/06/30
Tech Story Tags: aws | aws-lambda | serverless | monorepo | cloud | coding | programming | latest-tech-stories

TLDR Yan Cui is an AWS Serverless Hero and author of Production-Ready Serverless. He has run production workload at scale in AWS for nearly 10 years. Check out his new course, Complete Guide to AWS Step Functions: How to use Step Functions effectively in a monorepo. Get 40% off the face price with the code ytcui. Get your copy here. You can also get your copy of the book here. It's also available on Amazon.com and Lumigo.com.via the TL;DR App

A while back, a client asked me “how can I share business logic between services in a Node.js monorepo?”. So here it is!
Encapsulate the shared business logic into modules, and put them in a separate folder.
In the Lambda handler functions, reference the shared modules using relative paths.
Use webpack to resolve and bundle them into the deployment package. If you use the Serverless framework, then check out the serverless-webpackplugin. For example:
Deploy every service on every commit.You can do this with a simple script like the following.
To see how everything fits together, check out this demo repo. It has CI/CD set up already through drone.io and you can see a recent deployment of all the services in the monorepo here.
But wait! How do I choose between having a monorepo vs a repo per service?
Don’t worry, I’ve got you covered for that too ;-) You can read about my thoughts on the two approaches here.
Hi, my name is Yan Cui. I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.
You can contact me via EmailTwitter and LinkedIn.
Check out my new course, Complete Guide to AWS Step Functions.
In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.
Get your copy here.
Come learn about operational BEST PRACTICES for AWS Lambda: CI/CD, testing & debugging functions locally, logging, monitoring, distributed tracing, canary deployments, config management, authentication & authorization, VPC, security, error handling, and more.
You can also get 40% off the face price with the code ytcui.
Get your copy here.
Originally published at https://theburningmonk.com on June 29, 2019.

Written by theburningmonk | AWS Serverless Hero. Independent Consultant. Developer Advocate at Lumigo.
Published by HackerNoon on 2019/06/30