If you have used AWS Lambda to create an API using API Gateway before, then you must know the pain of creating and managing them with frequent updates. This was the same for me, especially with many functions it was hard to manage, tough to debug and update frequently. As there are many frameworks for managing Lambda, I tried the Serverless framework and everything changed for me. It simply blew my mind to see how powerful and easy it is to manage and track everything. This helps to develop and deploy Lambda functions, along with managing AWS infrastructure resources. This helped me to limit my focus on writing code and not worry about infrastructure management.

Just to give an intro of serverless computing, it is a cloud computing model which aims to abstract infrastructure management, provide an inherently scalable design with a pay-per-use model. This helps provide availability and fault tolerance which helps developers focus on writing business logic and not worry about other things.

In serverless, we have a function which is an independent unit of deployment, just code which is executed on the cloud. Anything which triggers these functions is known as an event. Resources (infrastructure) needed to execute these functions are managed by cloud providers. In terms of serverless framework, we need to create a service which is like a project that contains functions, events that trigger these functions and resources required by them.

In this article, we are going to create a NodeJs function which can be deployed on AWS Lambda using the serverless framework. To do this, we first need to install the serverless framework using ‘npm’. Serverless gives us two commands ‘serverless’ and ‘sls’ which are equivalent to each other. We are going to use ‘serverless’ command in this article.

npm install -g serverless

To start working on serverless, we need to create a service where we need to pass our NodeJs runtime with a service name which acts as the name of the directory in which our code resides.

serverless create --template aws-nodejs --path lambdaService Response of this command: Serverless: Generating boilerplate...

Serverless: Generating boilerplate in "/path/to/lambda/lambdaService"

_______ __

| _ .-----.----.--.--.-----.----| .-----.-----.-----.

| |___| -__| _| | | -__| _| | -__|__ --|__ --|

|____ |_____|__| \___/|_____|__| |__|_____|_____|_____|

| | | The Serverless Application Framework

| | serverless.com, v1.37.1

-------' Serverless: Successfully generated boilerplate for template: "aws-nodejs"

We have two files created inside the service directory named serverless.yml and handler.js. serverless.yml contains the configuration of our serverless code which defines the provider where the service is deployed, function details, any custom plugins used, events which trigger each function, resources required by the functions etc.

# Welcome to Serverless!

#

# This file is the main config file for your service.

# It's very minimal at this point and uses default values.

# You can always add more config options for more control.

# We've included some commented out config examples here.

# Just uncomment any of them to get that config option.

#

# For full config options, check the docs:

# docs.serverless.com

#

# Happy Coding! service: lambdaService provider:

name: aws

runtime: nodejs8.10

stage: dev

region: us-east-1

memorySize: 512 plugins:

- serverless-offline custom:

serverless-offline:

port: 4000 functions:

ourFunction:

handler: handler.ourFunction

description: Used to as dummy lambda function

events:

- http:

path: /

method: get

The handler.js file contains our lambda function code for the project. Each function defined in serverless.yml will point to a function in handler.js. We are going to write all our code in this file inside a function which is going to be invoked.

'use strict'; module.exports.ourFunction = async (event, context) => {

return {

statusCode: 200,

body: JSON.stringify({

message: 'Function executed successfully!',

input: event,

}),

};

};

We are going to create an API which will invoke our function. This is going to trigger the creation of an API Gateway route which invokes Lambda on request. We just added API related details in our serverless.yml file and we are good to go.

ourFunction:

handler: handler.ourFunction

description: Used to as dummy lambda function

events:

- http:

path: /

method: get

Once we are done with writing our business logic in the handler function, we need to deploy this code to AWS Lambda. When we run the command to deploy all functions, events, and resources, they are created using the AWS CloudFormation template.

serverless deploy Response of this command:

Serverless: Excluding development dependencies...

Serverless: Creating Stack...

Serverless: Checking Stack create progress...

.....

Serverless: Stack create finished...

Serverless: Uploading CloudFormation file to S3...

Serverless: Uploading artifacts...

Serverless: Uploading service lambdaService.zip file to S3 (1.36 MB)...

Serverless: Validating template...

Serverless: Updating Stack...

Serverless: Checking Stack update progress...

...........................

Serverless: Stack update finished...

Service Information

service: lambdaService

stage: dev

region: us-east-1

stack: lambdaService-dev

resources: 9

api keys:

None

endpoints:

GET -

functions:

ourFunction: lambdaService-dev-ourFunction

layers:

None Serverless: Packaging service...Serverless: Excluding development dependencies...Serverless: Creating Stack...Serverless: Checking Stack create progress........Serverless: Stack create finished...Serverless: Uploading CloudFormation file to S3...Serverless: Uploading artifacts...Serverless: Uploading service lambdaService.zip file to S3 (1.36 MB)...Serverless: Validating template...Serverless: Updating Stack...Serverless: Checking Stack update progress..............................Serverless: Stack update finished...Service Informationservice: lambdaServicestage: devregion: us-east-1stack: lambdaService-devresources: 9api keys:Noneendpoints:GET - https://1hntg29euyg.execute-api.us-east-1.amazonaws.com/dev/ functions:ourFunction: lambdaService-dev-ourFunctionlayers:None

One thing we need to make sure before deploying this service is that we create and store the required AWS credentials. Details can be found here. You can even use below command to set AWS credentials for serverless:

serverless config credentials --provider aws --key AWS_ACCESS_KEY --secret AWS_SECRET

We can invoke our lambda function using “invoke” command from the local system and get logs in response from lambda.

serverless invoke -f ourFunction -l

One of the best things about the serverless framework is its plugins which can be easily downloaded via npm and added using serverless.yml. Here we are going to use serverless-offline to create a local API route which can be used for local debugging.

npm install serverless-offline

We can change the port of offline application by changing the config in the serverless.yml file. To run lambda offline, we just need to run below command and we are good to go for local debugging.

serverless offline start Response of command: Serverless: Starting Offline: dev/us-east-1. Serverless: Routes for ourFunction:

Serverless: GET / Serverless: Offline listening on http://localhost:4000

Once everything is done and we don’t need this function, then we can simply remove it by executing remove command.

serverless remove Response of this command: Serverless: Getting all objects in S3 bucket...

Serverless: Removing objects in S3 bucket...

Serverless: Removing Stack...

Serverless: Checking Stack removal progress...

........

Serverless: Stack removal finished...

All this makes our life simple enough that using Lambda is no more painful experience.

PS: If you liked the article, please support it with claps 👏. Cheers