Voiced by Amazon Polly |
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.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
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.
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.
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 –
1 |
npm install –g serverless |
Here, you will get a prompt to provide SLS config credentials –
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 –
1 |
npm install serverless-http |
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
{ "name": "aws-node-project", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "express": "^4.18.2", "serverless-http": "^3.1.1", "serverless-offline": "^12.0.4" } } |
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
const serverless = require("serverless-http"); const express = require("express"); const app = express(); app.get("/api", (req, res) => { res.send("Hello in App"); }); app.post("/api/getback", (req, res) => { res.send({ ...req.body }); }); app.listen(3000, () => { console.log("connected"); }); module.exports.handler = serverless(app); |
Step 5: To deploy this application, let’s create a serverless.yml in our working directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
service: aws-node-project provider: name: aws runtime: nodejs14.x stage: dev region: ap-south-1 memorySize: 128 functions: app: handler: app.handler events: - http: path: / method: ANY cors: true - http: path: /{proxy+} method: ANY cors: true |
Step 6: Deploy:
Now deploy your application using the command
1 |
serverless deploy |
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 –
1 |
npm install --save-dev serverless-offline |
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
1 2 3 4 5 6 |
plugins: - serverless-offline custom: serverless-offline: noPrependStageInUrl: true |
Then use the command ‘serverless-offline start’ to start in your local development environment on port 3000. The URL is – http://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
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
Click to Comment