Start, Create And Deploy A Serverless Application With NodeJS And AWS Lambda.

Engineering Jun 28, 2021

In today’s age, we have been saying the trend of cloud computing allows people to make serverless technologies.

The biggest benefit of a server less system is that you do not have to worry about the setting up, maintenance, and scaling of the servers.

This has proven a lot beneficial for all co-operations especially smaller to mid-sized ones. Top cloud providers like Microsoft, Amazon, and Google Cloud, have started to provide serverless services for applications.

You can start your own server application in a matter of minutes and this is what makes the whole technology so popular. In this article, we will be developing our own serverless application with Node.js and AWS lambda.

What is Serverless Architecture?

In simpler words, serverless works as a back-end on demand. For a backend of an application, you need to set up your servers and manage the whole infrastructure but when it comes to the serverless architecture, everything has already been done for you.

Your application runs on the servers but everything is done by the service providers. In layman’s terms, imagine you have an application to deploy and you do not have a server.

Your friend offers you to deploy it on his server and he will take all the responsibility for maintaining, updating, and scaling the server according to your application and in return, he will charge you based on resources you utilize.

This is the serverless architecture that is gaining popularity among businesses and developers.

Getting started

AWS Lambda is a service for serverless apps that are offered by Amazon which allows the running of code without using the servers.

To start with the project, we will be needing an AWS account with access to Lambda and Node.js installed locally on our system.

The first step is to install the packages we will be using to develop our serverless app.

npm i serverless-http
npm install -g serverless
npm i express

Serverless is a framework that works with Node.js and AWS lambda to create applications. We have also installed Express.js as well. The next step is to provide the relevant AWS keys to serverless

serverless config credentials — provider aws — key ACCESS_KEY ?secret SECRET_KEY

Now, create a new file and fill it with the following code to initialize the Express.js application.

const express = require("express");
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get("/hello", (req, res) => {
  res.send("Hello World");
});
app.post("/add", (req, res) => {
  res.send({ ...req.body });
});
app.listen(3000, () => console.log(`Listening port: 3000`));

This app has two API endpoints, GET for displaying Hello World on the client-side and POST for returning the body of our request. By running the code, we get the following output.

Start, Create And Deploy A Serverless Application With NodeJS And AWS Lambda.

Now that our basic hello world application has been created, it is time to move it to the cloud with the serverless framework. For this purpose, we will be adding new lines as follows.

const serverless = require('serverless-http');

const express = require("express");
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get("/", (req, res) => {
  res.send("Hello World");
});
app.post("/add", (req, res) => {
  res.send({ ...req.body });
});
app.listen(3000, () => console.log(`Listening port: 3000`));

module.exports.handler = serverless(app);

The serverless package provides us the handler function which wraps our application to shift it to AWS serverless environment.

To make it work, we require a serverless.yml file that has all the configurations required for the deployment. Create a serverless.yml in the same directory and add the following lines to it.

service: my_app

provider:
  name: aws
  runtime: nodejs14.00
  stage: dev
  region: us-east-1

functions:
  app:
    handler: app.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

With all this, it is time to deploy our express application. For deploying it just type the following command

sls deploy

Once the application is deployed with no errors, you can navigate to the endpoint provided to you as an output.

If the endpoint is providing the output as desired, Congratulations, you just have deployed your first ever serverless application with Node.js.

Conclusion

In this article, we have learned how you can start, create and deploy a serverless application with NodeJS and AWS Lambda.

With the AWS serverless environment, you can create actual Databases, do path-specific routing, and much more! Serverless applications solve a lot of problems and keep things simpler. We can easily say that serverless is the future of internet.

What’s the biggest thing you’re struggling with right now that we as a technology consulting company can help you with? Feel free to reach out to us at info@jalantechnologies.com. We hope our assistance will help!

Tags

JTC

Like puppies, we build software that everyone love

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.