Project - Deploying serverless application using serverless framework
HTTP API with Node.js running on AWS Lambda and API Gateway using the Serverless Framework.
Table of contents
Serverless Architecture-scale your applications in a cost-efficient way(reducing infr coset)
Generally, developers can write and deploy code, while a cloud provider provisions servers to run their applications, databases, and storage systems at any scale.
Servers allow users to communicate with an application and access its business logic, but managing servers takes considerable time and resources. Teams have to maintain the server hardware, take care of software and security updates, and create backups in case of failure. By adopting serverless architecture, developers can offload these responsibilities to a third-party provider, enabling them to focus on writing application code.
Let's say I have one to-do application. It will perform write, delete, update, and display operations/functions. To run this application for 1 hour let's say it will take Rs.100. If it runs continuously for 24 hours then it will take Rs.2400. Here we no need to run the application for 24 hours. We just need 1 or 2 hours. But it is getting charged for the rest of the hours as well.
Here comes the serverless architecture where the application only runs whenever we need it and charge for only that period.
In the above diagram, the developer pushes code to the cloud. The function will be defined that triggers the application. Only when it is triggered application will be running.
Event-Based
Only when an event triggers, the service will run.
Serverless Services
Cloud Formation
Infrastructure as a code By cloud formation, you can setup, model, have, provision, and manage all the services in one place so that you can manage to spend less time managing these resources and focus on the runtime of your applications.
AWS Lambda
This will make your infrastructure robust.
The code that you run in lambda is called a lambda fucntion.
In lambda , instead of VMs , we use functions(no servers to manage) scaling is automated here have integration with whole services in aws Event driven-->inkoved when needed lambda container image must implement the lambda runtime API.
AWS API Gateway
AWS lambda will be integrated with AWS API Gateway. Handles scalability. Creates endpoint of the application.
To run serverless app you need a server. π¬
Just to make application we will be using EC2( not serverless)
Project
A serverless framework is a popular tool for building and deploying serverless applications.
Let's install a serverless framework on our machine ( I am using ubuntu installed on my machine. You can use an EC2 instance as well)
Serverless framework is built in node so install node first.
π How To Install Node.js 14 on Ubuntu 22.04|20.04|18.04 | ComputingForGeeks
sudo apt update
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
sudo apt -y install nodejs
node -v
Steps to install serverless
π Setting Up Serverless Framework With AWS
sudo npm install -g serverless
π Now let's create a directory where we will be building our serverless project.
We can make different types of serverless projects as shown below.
π We are going to make an HTTP API of Node js.
When we give hello in API-endpoint, it should give hello world.
choose AWS - Node.js - HTTP API
Our project directory will be created where you can find the below files already persists.
index.js: API will be written inside this file. It means it will receive the request and passes the response.
π This is a javascript code that uses the AWS lambda function.
π module.exports.handler = async (event) = module.exports.handler is the entry point of the Lambda function. event is the input parameter that is passed and invokes the lambda function.
π return returns the response. statusCode is an HTTP status code. 200 is the code which means the function executed successfully.
π It gives the success message if the function is executed successfully and gives the input data.
πThis Lambda function returns a successful response with a message and the input data passed to it.
serverless.yml:
This is a configuration file.
πThis serverless.yml file defines a service called first-serverless-hello-api and specifies that the framework version used is 3
π Cloud provider we are using is AWS. The application will use the Node.js version 18.x runtime on AWS Lambda.
As we are using AWS, we need to connect to the AWS account using IAM User Access Keys.
Connecting serverless with the access keys of a user :
Give admin access as of now not to miss any services.
Make sure you installed was cli in your machine. Add these credentials in your machine using below command.
aws configure
Now we connected with our was account. If we deploy our application then it will deploy into our connected was account.
Automatically it will create lambda function, API endpoint.
I added region in our serverless.yml file so that in this region our services will be created.
Now lambda function should be triggered. We need one endpoint URL. If we click that URL, then the function will be triggered.
For that API Gateway comes into picture.
Let's talk about API Gateway little bit π
It has 4 functions.
GET: you are getting some response
PUT: update
POST: Insert
DELETE: delete
To access all these functions you need an end-point.
Endpoint is like you have a URL and function attached to it.
here aasifa.com becomes URL and the endpoint is viewblog. If we hit that endpoint function will be triggered and you get response.
Let's deploy the application now.
Before that login to your serverless dashboard as below.
Now deploy the application using the server.
I got an error saying the signature expired.
try
sudo apt install ntp
sudo apt install ntpdate
sudo ntpdate ntp.ubuntu.com
serverless deploy now.
Our app ran successfully
I removed input data from index.js file and deployed again
To deploy in our serverless app we can use
serverless --org=aasifa
I added path in serverless.yml file as endpoint
When i hit it i can see the below dashboard.
Lets look at our AWS console where automatically our services were created.
S3 - our code will be stored in S3.
API Gateway
Lambda
Just by one click the process will be automated using a serverless application.
--- Thank you for your time. Lets meet in the next blog for part -2 where I will be using database as well π--