Custom serverless template

Clouddj28
4 min readJul 17, 2021

--

On our previous guide, getting started with serverless, we were able to create a basic API with just few commands.

We’ll be creating our new service or function again, but this time we will be adding another parameter on our command:

serverless create — template aws-nodejs — path user

This will create the folder named user, it would still contain the same files as what we did from the previous tutorial. We’ll open our project in VSCode to explore the current structure:

We’ll also install our serverless-offline plugin and add it on our serverless.yml for us to test later our function.

npm install serverless-offline — save-dev

Our use case would be a typical create user. We’ll create a folder inside user and name it create then move our handler.js to that folder, this will already isolate the function for creating a user. Then we modify our serverless.yml pointing to the handler.js that we moved.

Already removed the comments in order for us to have a clear view on the configuration. We now execute the same command to run the application in offline mode.

At this point, we still have the same output but this time our http method is a POST, we’ll be using postman to test our endpoint.

Now that we already have our base codes working, we’ll be creating our own template, this is to avoid the repetitive plugins that we install as well as common structure when generating a serverless function.

We’ll be creating a template folder and another inside it aws-nodejs from our main project folder.

We’ll go inside our aws-nodejs folder and initialize our nodejs package and supply all needed values, in our case, we’ll just accept the default values.

npm init

The generated package.json will serve as a common reference for our libraries and plugins i.e. serverless-offline.

We’ll copy the following resources from user folder to our aws-nodejs:

We should now have an exact copy from user to aws-nodejs.

We’ll now use the template that we created by issuing the following command:

sls create — template-path template/aws-nodejs — name accounts

Since we now have our service created, will get inside the accounts folder and install the node modules:

npm i

And execute the same steps we did for running the offline mode. This will lessen the setup and configuration when creating your serverless project, on our next article, we would be discussing on how we can deploy our application to AWS. Codes can be found here.

--

--

Clouddj28
Clouddj28

Written by Clouddj28

Cloud evangelist and music lover

No responses yet