Deploy your existing Nodejs APIs in serverless — AWS Lambda

Sasi Rekha
Mar 9, 2019 · 3 min read
Image for post
Image for post
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-httpnpm install serverless-offline --save

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

Create a service

serverless create --template aws-nodejs
Image for post
Image for post

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

Image for post
Image for post

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')const app = require('./config/lib/app')module.exports.run = sls(app)

Replace the code in serverless.yml with below code

service: demo-rest-apis # NOTE: update this with your service name    provider:       name: aws       runtime: nodejs8.10   functions:       app:          handler: server.run          events:            - http:                path: /                method: ANY                cors: false            - http:                path: /{proxy+}                method: ANY                cors: false    plugins:          - serverless-offline

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:name: aws
runtime: nodejs8.10
region: ap-south-1

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

Image for post
Image for post

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store