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.
AWS Lambda
AWS Lambda is a computing 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 1: Launching EC2 instance
Launch an EC2 instance and connect to it.
Start installing Serverless
Serverless is written in node js, so we need to install nodejs first. Refer to the link to install node-js
Step 2: Update the APT
sudo apt update
Step 3: Install Node.js 14 on Ubuntu 22.04|20.04|18.04
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
Once the repository is added, you can begin the installation of Node.js 14 on Ubuntu Linux
sudo apt -y install nodejs
Verify the version of Node.js and NPM installed.
$ node -v
$ npm -v
Step 4: Install Serverless module via NPM
sudo npm install -g serverless
With the above npm will install the serverless globally (-g) in the system.
Serverless pair-up with aws Lamba as they are technology partners. To access the AWS console we need to install aws cli and configure it
Step 5: Installing AWS CLI
sudo apt install awscli
aws --version
Refer the link
Step 6: Signup to Serverless and create an account.
- Back to the terminal and create a folder
$ mkdir projects
$ cd projects
# Starting serverless
$ serverless --org=rjthapaa
Use the arrow keys to select which kind of project we want to build.
We will be testing with HTTP API today, select it and hit enter
Provide the name for the project and make sure to create/configure the IAM USER with access keys to the AWS console through AWS CLI (Policy Admin access)
Type yes to deploy to create our first serverless deployment
Navigate to Cloud formation and S3 you will find the serverless project
You go inside the serverless project and check all the resources the project is using by clicking on resources.
The S3 bucket maintains all the versions of the code, packages and compiles as Zip file.
An API Gateway also will be created and you can use the invoke URL to access the application
Get back to the terminal and check the serverless project files like index.js and serverless.yaml
You can make the changes to the index.json file and use serverless deploy to deploy the application
$ nano index.json
Use the endpoint from terminal
You can also access the application from API Getaway