Apps Development, AWS, Cloud Computing

4 Mins Read

A Guide to Deploy NodeJS Applications on AWS Serverless

Overview

In today’s rapidly evolving technology landscape, serverless architecture has gained immense popularity due to its scalability, cost-effectiveness, and ease of deployment. AWS (Amazon Web Services) provides a robust serverless platform, offering services like AWS Lambda, API Gateway, and DynamoDB, which allow you to deploy and run your Node.js applications without the need to manage the underlying infrastructure. 

Introduction

In this blog post, we will learn about AWS Serverless, AWS Lambda, and how to build NodeJS APIs using AWS Lambda and NodeJS.

AWS Serverless:

AWS Serverless is a cloud computing model where the provider manages the underlying infrastructure and automatically provisions and scales resources based on demand without requiring the user to manage servers or infrastructure.

It allows developers to focus on building and deploying code rather than managing servers or infrastructure.

AWS Lambda:

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. You upload your code, and Lambda runs it, scaling automatically to handle requests. It supports multiple programming languages, including Node.js, and is commonly used for building serverless applications. With AWS Lambda, you only pay for the compute time you consume, making it a cost-effective solution for running applications with variable workloads.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Step-by-Step Guide

Note: In this application, I will be using VS Code Editor. You can choose the IDE or Code Editor of your choice.

Step 1: Create your project directory, open the terminal, and navigate to the project directory. In your terminal, give the command npm init -y. It will generate a boilerplate for our node application.

Step 2: Next, we will install Express.JS using the command npm i express in our application.

Step 3: To get started, firstly, we need to configure AWS credentials in our environment, and after that, we need to install AWS Serverless Framework. Use the below command –

Here, you will get a prompt to provide SLS config credentials –

node

You have to provide your PUBLIC_KEY and SECRET_KEY.

We are installing other packages included in dependencies along with express. For that, use the command in your terminal –

Serverless-http:

The serverless-http package is a Node.js module that provides a simple way to deploy a Node.js web application to a serverless environment. It allows you to create a serverless function that can be run on services like AWS Lambda or Google Cloud Functions and responds to HTTP requests.

The package works by wrapping your existing Node.js HTTP server or Express application and transforming it into a format that serverless functions can use. This allows you to leverage the power and scalability of serverless computing while still using your familiar Node.js application code.

Finally, our package.json will look like this –

Step 4: Now, our environment is completely set. We will create an express http endpoint. Create an app.js file in the current directory. It should contain API code. It should look like this –

Step 5: To deploy this application, let’s create a serverless.yml in our working directory.

Step 6: Deploy:

Now deploy your application using the command

The Serverless Framework will now wrap everything into a nice bundle, create a CloudFormation file from the serverless.yml and shoot it off to AWS S3. Once the resources are created and the code is deployed, you’ll see an endpoint sent to you in the terminal.

Thus, our app is deployed to AWS Lambda.

Serverless Offline:

We can also start our application in a local development environment. For this, we need to install the serverless-offline plugin.

This plugin helps to emulate the API Gateway environment for local development. For this, use the command –

The serverless-offline package is a Node.js module that allows you to simulate the behavior of a serverless environment on your local machine. It enables you to run and test your serverless functions locally without deploying them to a cloud provider.

The serverless-offline package works by emulating the API Gateway and Lambda environment locally. It provides a local HTTP server to handle requests and pass them to your serverless functions. This allows you to test your application logic and API endpoints as you would in a real serverless environment, but with the convenience of running everything locally on your machine.

Then add this plugin to your serverless.yml file

Then use the command ‘serverless-offline start’ to start in your local development environment on port 3000. The URL ishttp://localhost:3000.

Conclusion

Deploying a Node.js API on AWS Lambda using the Serverless framework offers numerous benefits and simplifies building scalable and cost-effective applications. Adopting a serverless architecture lets you focus on developing your API’s business logic without worrying about infrastructure management.

Making IT Networks Enterprise-ready – Cloud Management Services

  • Accelerated cloud migration
  • End-to-end view of the cloud environment
Get Started

About CloudThat

CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding AWS Serverless, NodeJS and I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.

FAQs

1. How can I manage my AWS Lambda functions?

ANS: – You can easily list, delete, update, and monitor your Lambda functions using the dashboard in the AWS Lambda console. You can also use AWS CLI and AWS SDK to manage your Lambda functions.

2. Does AWS Lambda support versioning?

ANS: – Yes. By default, each AWS Lambda function has a single, current version of the code. Clients of your Lambda function can call a specific version or get the latest implementation.

WRITTEN BY Satyam Dhote

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!