The popular Serverless Framework is built to deploy only files inside the directory in which the serverless.yml file is contained.
This limitation is probably by design — all the external code your Lambda needs can be usually included with a package manager.
In Node.js, you run npm install and upload the node_modules directory. In Python, the popular serverless-python-requirements command can be used to add the dependencies using pip. A similar solution exists for the other runtimes as well.
But what if we need to deploy some external code, saved locally on our machine, which can not be added using a package manager? Our simple plugin serverless-package-external was created just for that!
Let’s see an example. Suppose we have this following structure inside the library:
In order to include the common library what we can use the following serverless.yml file:
In this case, the common directory will be deployed as if it was inside the service1 directory. The handler code can now regularly import the included common library:
How does it work? The plugin only adds a symbolic link to the external directory path, naming it the same as the directory name (both relative and absolute paths are welcome). Therefore, the content of the external directory is deployed as if it was just included as a subdirectory. After the deployment is finished, the symlink will be deleted by the plugin. Easy and simple!
When is this plugin useful? The most familiar use cases are when you need to deploy a common code but you want to save the overhead of packing it into a package, or you just want to deploy a version of a package which is not distributed – for example, if it’s still under development.
It’s all in the GitHub repository: https://github.com/epsagon/serverless-package-external