DEV Community

Cover image for How to Deploy NodeJS APIs on AWS
Lovepreet Singh
Lovepreet Singh

Posted on

How to Deploy NodeJS APIs on AWS

πŸš€ Welcome guys to our another episode of Software Development tutorials. So, most of us people did build some cool web apps and never thought about taking it to the real world. Although, we do find real problems when we go to the real market but nothing to worry. One of that problem we'll cover today. And that is:-

πŸ“ How to make/deploy our NodeJS App on AWS so that it can handle/manage the traffic in real time and we can only focus on development.

Note:- We will also cover "How to reduce AWS Cost by 90% in our next Blog". Stay Tuned....

AWS Meme

πŸ“ Components that we will use

We're going to use AWS Lambda which runs only when triggered and we pay cost only for that (No of requests and duration of request we send). And Yes, πŸ₯² Don't worry AWS do provide some free criteria which is way more than enough for learning.

Note:- While creating your AWS account, add your debit/credit card but it no balance will be deducted for testing/learning our deployment.

πŸš€ We'll use NodeJS to make our API and Serverless framework to facilitate the deployment.

πŸ“ Generate your access key and secret Key

Go to AWS, Create your account and add your debit/credit card to activate the AWS Account. (πŸ™ƒ Don't worry you will not be charged in free tier).

  • Go to security credentials

AWS Secret Key

  • Generate Access key and Secret Access key and note it down

πŸ“ The Code

  • Run commands in order in our project directory (After creating your project using npm init)
npm init
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo npm install -g serverless
Enter fullscreen mode Exit fullscreen mode
  • Now, run the command
serverless config credentials --provider aws --key <Access-Key> --secret <Secret-Key>
Enter fullscreen mode Exit fullscreen mode
  • 😎 Now it is time to setup the Serverless template
serverless create -t aws-nodejs 
Enter fullscreen mode Exit fullscreen mode
  • It will give you serverless.yaml file

  • Our serverless.yaml file looks like:-

service: serverless-nodejs-app
provider:
  name: aws
  runtime: nodejs14.x
  stage: dev
  region: eu-central-1
functions:
  app:
    handler: app.server # reference the file and exported method
    events: # events trigger lambda functions
      - http: # this is an API Gateway HTTP event trigger
          path: /
          method: ANY
          cors: true
      - http: # all routes get proxied to the Express router
          path: /{proxy+}
          method: ANY
          cors: true
Enter fullscreen mode Exit fullscreen mode
  • app.js file looks like
const express = require('express')
const sls = require('serverless-http')
const app = express()
app.get('/', async (req, res, next) => {
  res.status(200).send('Hello World!')
})
module.exports.server = sls(app)
Enter fullscreen mode Exit fullscreen mode

πŸš€ Note:- Install these dependencies to run the project

npm i serverless-http
npm i express
npm i cors
Enter fullscreen mode Exit fullscreen mode

😎 Now, You all set. Our Project directory looks like this:-

NodeJS Project

πŸ§‘β€πŸ’» Now, run sudo sls deploy to deploy your project

Serverless Application deployment

πŸš€ Run the command (Hidden in the CLI in above image) and you'll be able to hit the endpoint.

Oldest comments (16)

Collapse
 
vaibhav68849256 profile image
Vaibhav Khandare

Nice article very helpful cleared all doubt thanks for sharing your knowledge with us

Collapse
 
light_seekern profile image
Taha Syed

Delighted to read

Collapse
 
mohmmadaslam profile image
Mohmmad Aslam

Great work Lovepreet!

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Thanks Aslam 😊😊

Collapse
 
yk_dhawan_519349b771d0dba profile image
YK DHAWAN

Good Article, Easy to read.

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Thanks bruhh

Collapse
 
cjreads665 profile image
Shahid Alam

a great article for a beginner like me. Thanks

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Thanks a lot buddy. Share with your friends ☺☺

Collapse
 
anurag_vishwakarma profile image
Anurag Vishwakarma

Hey, Lovepreet Singh are you interested in publishing these type of contents on my blog ? Just started few months ago. I would love if you join us.. We are trying to help beginners.

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Will look more into it.

Collapse
 
anurag_vishwakarma profile image
Anurag Vishwakarma

Ok, Waiting for your response.

Thread Thread
 
lovepreetsingh profile image
Lovepreet Singh

Just email me the details at lpsk5713@gmail.com so that i can decide if to publish there or not

Thread Thread
 
anurag_vishwakarma profile image
Anurag Vishwakarma

βœ… Done!

Collapse
 
hartajsinghdev profile image
Hartaj-Singh-Dev

My man , always putts out πŸ”₯ best content

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Thanks Hartaj.

Collapse
 
mrexamples profile image
Mrexamples

really very helpful indeed, keep sharing the valuable content like this. Sharing is caring and we have firm believe in it, that is why we have a platform called Mrexamples feeling proud to help the newbies to built theirs's first website by our tutorials.