Deploy your existing Nodejs APIs in serverless — AWS Lambda
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
npm install serverless-offline --save
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')
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
Now 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.