How To Write Your First Plugin For The Serverless Framework — Part 1
Tags: serverless, plugin, javascript, tutorial
Published: 2022-05-18 15:19:42

Originally posted at [Serverless](https://www.serverless.com/blog/writing-serverless-plugins/) on January 4th, 2017
>  ***Update: the Serverless Framework documentation now contains a complete reference on writing custom plugins. Read more in the [“Creating plugins” documentation](https://www.serverless.com/framework/docs/guides/plugins/creating-plugins).***

Hi, I’m [Anna Doubkova](https://github.com/lithin), a software engineer at [Red Badger](https://red-badger.com/) in London. If you’re reading this post, you probably know some basics of **the Serverless Framework**, like deploying Lambdas and creating API endpoints. But maybe you’ve hit a block where you find doing something with the framework is either difficult, impossible, or simply too repetitive.

**You can ease your pain by using plugins.** Some are already built for the most common problems (check them out in the official [Serverless Plugins Github repo](https://github.com/serverless/plugins)), but there are always some project-specific issues that plugins can help you resolve. Fortunately, writing a plugin for the Serverless Framework is easier than you might think.

In this tutorial, **you’ll learn by examples how to write your own plugins.** We’ll start from very simple examples and build upon them to get all the way to writing useful plugins that could help you with your everyday deployments.

![](https://cdn-images-1.medium.com/max/2048/0*aGqRiDf8T1VQhI4F.png)

## Create Your First Plugin

The Serverless Framework is an incredibly well-built open source platform. It’s nearly indefinitely extensible, and allows you to add new features with surprising ease. **Let’s see how you can add a plugin in the simplest way possible.**

Somewhere in a new directory run:

    serverless create — template plugin

This command will create an index.js. When you open this file, you’ll see a class with a constructor and a few simple functions. Let’s have a look at it step by step.

## Plugin as a Class
{% gist https://gist.github.com/richardgrantserverless/79c6198f65c4328bd10d20fade39ede0.js %}
**Every serverless plugin is a class.** This class gets instantiated with a serverless object and a bunch of options. We’ll get to them in more detail in a little while, but for now it’s enough to say that these will help you *do* things in your plugin.

## Define Your Commands‍
{% gist https://gist.github.com/richardgrantserverless/df2f8c94ba3d7269e39c706516bfc5c3.js %}
The next thing to notice in the constructor is the **definition of commands** that your plugin introduces. In the boilerplate we initialised using the Serverless CLI helper, command serverless welcome is added.

### Usage — or Help

In the usage section, you can specify **a hint how the command should be used.** This hint will appear when you run serverless — help in the list of commands and their descriptions.

### Lifecycle Events

The crucial bit in any command is the lifecycleEvents array. **Lifecycle events allow you to define the steps that are most likely to be taken while executing the command.** For example, these are the lifecycle events for serverless deploy command:

From cleanup to deploy, we have a list of tasks we should fulfill on our way to deploying a service. We don’t write any implementation yet, though. **We’re only describing the process** in a very general way that doesn’t include any implementation details.

The advantage of this approach is that it doesn’t tie us into one way of handling deployment (or executing any other command), which in turn helps us avoid vendor lock-in.

As such, you can **see the command definition as a guideline** that you can use later on to write your code. This makes for more readable, **self-documented code.**

### Options — or Flags

You might notice that in the plugin template we also have the options section alongside usage and lifecycleEvents.
{% gist https://gist.github.com/richardgrantserverless/921e35b3d93ef9100db8aa4c6672e186.js %}
The options section can be used to **describe which flags can be used in CLI with your command.** In this definition, the — message option is required, and has a shortcut -m so that you can equally write serverless welcome — message “Hello!” and serverless welcome — m “Hello!”.

As with the command itself, we have a usage description which appears when asking for help in the CLI serverless welcome — help.

### Requirements for Defining a Command

The snippet discussed above is defining lifecycle events, help description, and a flag. For the flag, we have again a help description section, information whether the flag is required and a flag shortcut. As you could guess, **not all of them are required.** A very minimalistic approach would be:
{% gist https://gist.github.com/richardgrantserverless/1721cf77df6ec2a346babf8f54240a18.js %}
This will work in a very similar way to the definition above. However, it won’t provide you with automatic checking that the required option is passed to the command, or any help information.

From this perspective, **I’d suggest spending the time with writing up usage, requirements and shortcuts.** It will make it significantly easier for the users of your plugin to figure out how to actually use it.

## Hook Into Events

Defining the commands and their lifecycle events is useful to describe what the plugin does. **Hooks describe *how *the plugin does it.**

You can find them right under commands in the constructor:
{% gist https://gist.github.com/richardgrantserverless/7778f469c1f37c0ce40210444e103c64.js %}
‍**Hooks help us define the implementation of each step.** In the code above, you can discern the command name welcome and the two lifecycle events we defined for it: hello and world.

Hook welcome:hello defines what to do at step hello of command welcome; before:welcome:hello describes what to do before the first step. Similarly, after:welcome:world defines what to do after the last step.

This gives us a very fine-grained control over definition of **what the command does at each step, and enter specific actions before and after each step.**

Remember you don’t have to define every step. Maybe in your particular implementation, you only care about world and not hello, and that’s absolutely fine.

Later on someone else can come with their plugin and define their own implementation of the welcome plugin based on the steps you specified in the command. It’s an open-ended world, allowing (nearly) endless extensions.

## Implementation

After having defined the command, its lifecycle events and how we hook into them, **all that’s left is the actual implementation.** The details are obviously specific to the plugin you’re writing, which platform you’re targeting it at, etc.

In the template, we’re only logging out greetings and the message we’ve passed it:
{% gist https://gist.github.com/richardgrantserverless/ba51d497b51bddb34f84e8aa60daa74e.js %}
‍That, however, *might not be quite enough* in a real world project.

You’ll learn **how to write implementations** of plugins, what the **serverless object** is all about, and how you can approach writing plugins in **multiple ways** — in [How To Write Your First Serverless Plugin — Part 2](https://serverless.com/blog/writing-serverless-plugins-2/).

[![](https://cdn-images-1.medium.com/max/2712/1*coRpboSHyAtv5UPafeHzbw.png)](https://console.serverless.com/?view=register&utm_campaign=Console%20Signup&utm_source=dev.to&utm_medium=post&utm_content=How%20To%20Write%20Your%20First%20Plugin%20For%20The%20Serverless%20Framework%20%E2%80%94%20Part%201%0A)

*Originally published at [https://www.serverless.com](https://www.serverless.com/blog/writing-serverless-plugins/).*