Extend Serverless with plugins
Serverless itself delivers many functionality right out of the box. But what if you need an additional functionality?
Serverless has you covered as it offers ways to extend the common command line interface with the help of plugins. Let’s take a look at how they work and how you can add plugins to your Serverless projects.
Plugins
Serverless is a command line utility which makes it easy to manage and deploy your serverless application on the AWS cloud. You can manage endpoints, functions, events, stages, regions, and much more. But what if you e.g. want to deploy your static website which connects to your Serverless application? You can setup everything by hand with the help of tutorials and docs. But this task is very common and you may want to reuse it for your other projects.
This is a great use case for a Serverless plugin.
The core of Serverless is written in a modular way and makes it easy for you as a developer to extend it as you need it.
But you don’t need to modify the Serverless core.
Serverless offers a way to develop plugins which are standalone and can be added to the Serverless project.
How do I find Plugins?
Serverless plugins are NPM packages and are therefore published on NPM. A quick search for “serverless” show us available Serverless plugins (although there is some noise because other non Serverless related packages might also pop up).
We’ve developed and maintain a searchable plugin registry where you can search for available Serverless plugins. Additionally you might want to check out the “Awesome Serverless” list which also includes a section about plugins.
Install the “Serverless client S3” plugin
Let’s add a plugin to our project so that we can deploy our static website which connects to our Serverless application with the help of a S3 bucket.
The Serverless client S3 plugin helps us here.
cd into the root of your serverless project and run npm install — save serverless-client-s3. This adds the NPM package as a dependency to our Serverless project.
Next up we need to tell Serverless that we want to use this plugin. We can do this by adding the name of the plugin to our plugins array in the s-project.json file of our project:
{
"name": "our-serverless-project",
"custom": {},
"plugins": [
"serverless-client-s3"
]
}
That’s it. Now Serverless knows that a new plugin is available and displays it in our Serverless command line. Run serverless and you should see the section “client” with the deploy functionality.
Deploying our static website
Now we only need to create two folders which are required by our previously added plugin. Create the folders client/dist in the root of the Serverless project.
After that we simply need to copy and paste the static content of the website in the dist folder.
Now we run serverless client deploy and our plugin does it’s thing.
A S3 bucket gets created and setup for static website hosting. A few seconds later we get a URL which we can copy and paste to access our website.
Done!
I want to develop a plugin
Great! It’s not that hard to develop a plugin. You can run serverless plugin create to create a template which you can use for your plugin development. This plugin template is well documented / commented which makes it possible to get up and running in a matter of hours.
Another tip is to look though the source code of existing plugin.
Pick a plugin form the plugin list and read the source.
Conclusion
Plugins gives us a great way to extend the Serverless core with the functionality we want. We can share plugins easily with other developers by publishing them on NPM. Serverless makes it very easy to get started with plugin development and extend the ecosystem in a way you want it.