Serverless platforms are, by design, lightweight. This makes the functions that run on them fast and efficient,...
but they also present a host of new challenges related to application design and deployment, including how you manage dependencies.
According to the second step in the 12-factor methodology for application development, teams should explicitly declare and isolate app dependencies. And while developers generally accomplish this through the use of packaging systems in most modern programming languages, it's not as straightforward with serverless.
Here's a look at why app dependencies can pose a challenge on serverless platforms and how to overcome them.
This problem becomes increasingly difficult with lower-level languages, such as C. In this instance, every package is a native module, which means developers need to work out a way to use any dependency. Many cloud providers suggest developers compile native modules on a machine that closely resembles the architecture of the serverless environment, or -- even better -- use a package that relies on simple source files rather than platform-specific binaries. Either way, you'll have to jump through a few hoops to solve this particular issue.
Sometimes, the deployment of serverless functions feels like you're going against the grain. For example, to deploy a function to AWS Lambda, you'll need to create a deployment package.
A deployment package is a zip file that contains the function's code and any of its dependencies. This means that, rather than rely on the dependency manager during a typical atomic deployment process, a serverless deployment package requires you to download and configure all dependencies before delivery to the server.
For smaller projects with serverless platforms, this shouldn't be too frustrating, but for frequent deployments, this process could be more time-consuming than it's worth. To deal with this more efficiently, establish a well-defined CI/CD process to handle functions with app dependencies. Rather than manually compose the deployment package prior to each release, automate this process, and treat it like any other application development workflow.
In some instances, the built-in NPM isn't enough to deal with dependencies in a serverless environment. This is where tools like webpack come into play.
Rather than prebundle and upload a zip -- either manually or within a CI/CD pipeline --developers can use these webpack plug-ins to build the code, install dependencies, package the app and deploy it to the serverless platform with minimal overhead. This provides a clean and consistent way to manage dependencies in more complex applications.
Lighten the load
While it's not always possible to reduce the number of required app dependencies, developers should attempt to isolate the necessary dependencies within each function on a serverless platform. This allows each function to be lean and avoids the bloat that can come with monolithic applications. The fewer dependencies a project has, the better it performs.
And the less code you write, the more maintainable your code base is. Not every package is a required dependency, so boil it down to simple and performant code. This will go a long way toward having a maintainable serverless project.