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 nothin…


This content originally appeared on DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป and was authored by Lovepreet Singh

๐Ÿš€ 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
  • Now, run the command
serverless config credentials --provider aws --key <Access-Key> --secret <Secret-Key>
  • ๐Ÿ˜Ž Now it is time to setup the Serverless template
serverless create -t aws-nodejs 
  • 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
  • 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)

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

npm i serverless-http
npm i express
npm i cors

๐Ÿ˜Ž 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.


This content originally appeared on DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป and was authored by Lovepreet Singh


Print Share Comment Cite Upload Translate Updates
APA

Lovepreet Singh | Sciencx (2023-02-18T15:12:51+00:00) How to Deploy NodeJS APIs on AWS. Retrieved from https://www.scien.cx/2023/02/18/how-to-deploy-nodejs-apis-on-aws/

MLA
" » How to Deploy NodeJS APIs on AWS." Lovepreet Singh | Sciencx - Saturday February 18, 2023, https://www.scien.cx/2023/02/18/how-to-deploy-nodejs-apis-on-aws/
HARVARD
Lovepreet Singh | Sciencx Saturday February 18, 2023 » How to Deploy NodeJS APIs on AWS., viewed ,<https://www.scien.cx/2023/02/18/how-to-deploy-nodejs-apis-on-aws/>
VANCOUVER
Lovepreet Singh | Sciencx - » How to Deploy NodeJS APIs on AWS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/18/how-to-deploy-nodejs-apis-on-aws/
CHICAGO
" » How to Deploy NodeJS APIs on AWS." Lovepreet Singh | Sciencx - Accessed . https://www.scien.cx/2023/02/18/how-to-deploy-nodejs-apis-on-aws/
IEEE
" » How to Deploy NodeJS APIs on AWS." Lovepreet Singh | Sciencx [Online]. Available: https://www.scien.cx/2023/02/18/how-to-deploy-nodejs-apis-on-aws/. [Accessed: ]
rf:citation
» How to Deploy NodeJS APIs on AWS | Lovepreet Singh | Sciencx | https://www.scien.cx/2023/02/18/how-to-deploy-nodejs-apis-on-aws/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.