Deploy your existing Nodejs APIs in serverless — AWS Lambda

Photo by Pankaj Patel on Unsplash

When i first tried to deploy my existing nodejs rest APIs in Serverless it took quite long time to figure out the way. So i’m here to share the process which i figure out to deploy our existing apis in Serverless. Lets get into the process..

Open your terminal and go to the project folder, Install serverless globally by typing the below command.

npm install -g serverless

Config your AWS credentials in your local system

sls config credentials --provider aws --key your-key --secret your-secret

Install serverless npm packages

npm install --save serverless-http

Note : serverless-offline plugin is basically for offline testing.

Create a service

serverless create --template aws-nodejs

You can see two new files called handler.js and serverless.yml files in your project folder

Now we can remove handler.js file instead we will be using server.js file. Change your server.js file like below

const sls = require('serverless-http')

Replace the code in serverless.yml with below code

service: demo-rest-apis # NOTE: update this with your service name

By default it will deploy in us-east-1 region, if you wanted to specify particular region add it in the provider. Please find the below example..

provider:

you are all set to deploy your application in serverless. It’s time to deploy now….

It will give us endpoints, Go to browser and paste the end point, you can see that your project is running successfully and also you can see the uploaded file in your AWS Lambda like below

See the logs tails using below command

serverless logs -f app -t

Yeyyy…. Here you go, your APIs deployed in AWS Lambda successfully.

Click here to find demo project github repo. If you have any questions, please leave a comment below. By the way this is my first post.

Javascript full-stack lead dev | Micro-services architect | Pencil sketch artist | Optimist

Javascript full-stack lead dev | Micro-services architect | Pencil sketch artist | Optimist