Serverless Framework in a Development Environment

Sudheer Kumar
AWS Tip
Published in
4 min readMar 20, 2021

--

Photo by Nick Fewings on Unsplash

In the previous articles, we already mentioned why we need a Framework, and now let us get on to get our hands dirty with some code. Here I am going to concentrate on the aspects of working with the Framework in a practical real development environment, rather than the actual business logic.

Prerequisite

Ths tool set assmed are:

  • VS Code editor
  • Install NodeJS

Installing Serverless (SLS)

>npm install -g serverless

Creating First SLS Project

You can see the following commands to see all the commands of SLS

>sls -help

Now lets create a simple project using a AWS-NodeJS template:

>sls create -template aws-nodejs -path sls-hello-world

To see all the other options of creating a template, see the help

>sls create — template -help

Ok.. now you can see that it has created a project with file : handler.js — where you have a default function — hello — implementation and serverless.yml where you have your service configuration.

Let's leave the handler as it is. Now open the serverless.yml, remove all comments and add the following to deploy our function to dev environment using a specific profile.

So your profiles are locally listed on a text file and you can find it here: %USERPROFILE%\.aws\credentials. (<Windows>+R and run the above command). So here I am using credentials of an IAM user which I am usign for deploying my Lambda function. You can list the credentials of the user manually in that file or add from the command line: >aws configure --profile newAccount

You can find more options on multiple profile usage from there: https://serverless-stack.com/chapters/configure-multiple-aws-profiles.html

Now before deploying this Lambda, let's create an API Gateway entry point to our function.

Local Testing

You can invoke the function locally from the command prompt like this. NOTE that you have to execute this command from the SERVICE FOLDER on the terminal window.

~\SLS_Samples\sls-hello-world>sls invoke local -f hello

Running API locally

Now we should be able to run the API locally and we need the help of plug-ins to do that. To help with our local activities, we need to install node packages for the plug-ins locally. FIrst run the following command to add package.json to our local project.

>npm init -y && npm install

Now install with local debugging, install the plug-

>npm install serverless-offline -D

Now add the plug-in to the project in .yml file at the root:

No you can run the API locally with the help of the plug-in as follows. As usual make sure that you are running this commnad from the root of the SERVICE FOLDER. If you run this command from the root of your Node project, you will get the error: Serverless command “offline” not found, but the actual problem is that you are not running it from the root of your service.

~\SLS_Samples\sls-hello-world>sls offline

You will see the details of local API as follows and you can browse it locally.

Debugging Locally

You can debug the function locally.Refer the 2 methods mentioned in these articles:

https://medium.com/@OneMuppet_/debugging-lambada-functions-locally-in-vscode-with-actual-break-points-deee6235f590

https://hackernoon.com/running-and-debugging-aws-lambda-functions-locally-with-the-serverless-framework-and-vs-code-a254e2011010

Unit Testing

You need to install the plug-in :

>npm i serverless-mocha-plugin -D

Now you can add a test to the function you created.

>sls create test -f hello

Now it adds a test folder under the service fodler and also adds a default test.

Now test the function individually or run all test cases like this:

~\SLS_Samples\sls-hello-world>sls invoke test

~\SLS_Samples\sls-hello-world>sls invoke test -f hello

Just add another test and run the test again.

it(‘hello should have a nvalid reponse’, async () => {

const response = await wrapped.run({});

expect(response.statusCode).equals(200)

expect(JSON.parse(response.body).message).equals(“Go Serverless v1.0! Your function executed successfully!”)

});

We will discuss deeper aspects in testing like dependency injection and mocking in another post.

Now we are done with our development and time to move to deployment and CI/CD. More about that in the upcoming post…

--

--

Experienced Cloud Architect, Infrastrcuture Automation, Technical Mentor