AWS-Serverless Project (NodeJS API Deployment)

Kritika ShawKritika Shaw
7 min read

The Serverless Framework is an open-source framework that simplifies the deployment and management of serverless applications. Serverless computing is a cloud computing execution model where cloud providers automatically manage the infrastructure, and users only need to worry about the code and its functionality.

The Serverless Framework supports multiple cloud providers, including AWS (Amazon Web Services), Azure, Google Cloud Platform, and more. It abstracts away the complexities of infrastructure management and allows developers to focus on writing code that runs in response to events, such as HTTP requests, database changes, file uploads, etc.

The Serverless Framework supports multiple programming languages, including Node.js, Python, Java, and Go. It also provides a plugin architecture, allowing developers to extend its functionality and integrate it with other services.

Go to the serverless framework and create an account by signing up.

Serverless Framework Installation

You can check my previous blog on Installation for more details.

Step-1(Installation of NodeJS on Ubuntu)

Serverless Framework is made on NodeJS hence, before installing Serverless Framework we need to have NodeJS installed on our system to support it.

After the system update, install Nodejs 14 on Ubuntu 22.04|20.04|18.04 by first installing the required repository:

curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -

Once the repository is added, you can begin the installation of Nodejs 14 on Ubuntu Linux:

sudo apt -y install nodejs

Now the installation has been completed and you can check it by:

node -v
npm -v

Step-2(Installing Serverless Framework)

After installing NodeJS we are proceeding to install serverless.

Here, npm which is the NodeJS package manager will be used to install serverless globally as below:

sudo npm install -g serverless

Creating and Deploying an API with Serverless

Once all the above installation processes are completed we can deploy a project using the serverless. To do so, we have to check what all kinds of projects are we allowed to deploy using Serverless Framework. To do so, simply command serverless which will list all the projects it supports to be deployed.

Now once you decided on the project that needs to be created it will download all the templated required for that particular project as below:

Here I have chosen AWS - Nodejs - HTTP API as my project so, the Serverless has downloaded all its predefined templates and files required for deployment as below where we have index.html and serverless.yml files. Here serverless-nodejs-api is the name of the project.

index.js

Here you can see index.js file has been downloaded as part of our project. This index.js is an API function written in javascript that looks like the below:

serverless.yaml

serverless.yaml is a configuration file used by the Serverless Framework to define the structure and resources of a serverless application. This file is written in YAML format and contains information such as the AWS Lambda functions, events, and resources that make up the application.

serverless.yaml is a powerful tool for defining the structure and resources of a serverless application and enables easy deployment and management using the Serverless Framework.

Below is the YAML file that we need to create for deployment if we use lambda from the AWS console. However, here we are using the Serverless Framework that's why this has been created by the framework itself.

This is a YAML configuration file for an AWS Lambda function using the HTTP API event type. However, this configuration includes an additional section under provider that specifies the AWS region where the function will be deployed. In this case, the region is set to ap-south-1.

Setting the region is important because it determines the location where the function will be executed and where the data associated with the function will be stored. It's recommended to choose a region that is geographically close to where the majority of your users or resources are located to minimize latency.

The handler property specifies the entry point of the Lambda function, which in this case is index.handler. This suggests that the code for the function is in a file named index.js which we have shown in index.js section above and that the handler function within that file is exported as the entry point for the Lambda function.

The events property specifies the events that trigger the Lambda function. In this case, the event is an HTTP API event that is triggered when an HTTP GET request is made to the root path /. The path property specifies the endpoint that the API will be listening on, while the method property specifies the HTTP method that the API will be listening for.

Overall, this configuration sets up a serverless API that responds to HTTP GET requests at the root path, using a Nodejs 18.x runtime, and deploys to the ap-south-1 region.

API Gateway

API Gateway is a fully managed service provided by Amazon Web Services (AWS) that allows developers to create, deploy, and manage APIs for their applications. It acts as a front door for applications to access data, business logic, or functionality from backend services, such as AWS Lambda, AWS Elastic Beanstalk, or EC2 instances, and makes it easy to expose those services through a public or private API.

API Gateway supports RESTful APIs and WebSocket APIs and provides features like request/response transformation, caching, throttling, security, and monitoring. It also integrates with other AWS services like AWS Identity and Access Management (IAM), AWS Lambda, Amazon S3, and Amazon DynamoDB, making it easier to build and scale serverless applications.

    events:
      - httpApi:
          path: /
          method: get

The above snippet from the main serverless.yml file will create an API using AWS API Gateway.

Deployment of the API(serverless deploy / sls deploy )

serverless deploy

serverless deploy is a command used in the Serverless Framework to deploy a serverless application to a cloud provider like AWS, Azure, or Google Cloud Platform.

When you run serverless deploy, the Serverless Framework packages your application code and any necessary dependencies, uploads them to the specified cloud provider, and sets up the necessary resources to run the application, such as AWS Lambda functions, API Gateway endpoints, and IAM roles.

The deploy command also creates a CloudFormation stack that manages the resources created by the Serverless Framework. This stack includes all the necessary configurations and permissions needed to run your application on the cloud provider.

The Serverless Framework uses a YAML configuration file called serverless.yml to define your serverless application, including the functions, events, and resources needed to run your application. This configuration file is used by the deploy command to deploy your application to the cloud provider.

Overall, serverless deploy simplifies the process of deploying and managing serverless applications, allowing developers to focus on building and testing their code without worrying about the underlying infrastructure.

On Deployment Completion

After the successful completion of the deployment, a GET API has been generated by AWS API Gateway

In the below image, you can see that our API has been registered via API Gateway without even touching it manually. It is all made possible by the Serverless Framework.

Now when we are sending the request to the above endpoint we are getting the response as below:

Using Postman

Updating the API response

Now we have made some changes to the index.js file and eliminated the complete event logging as below:

Now we will redeploy the application and see the changes. Here it is just updating the stack with the latest changes we have made to our API code.

Now we are getting a much simpler message as below:

Resources used for this Deployment

As we have discussed, the Serverless Framework has implemented AWS CloudFormation to deploy the entire API. Here are the resources that have been created throughout this deployment which are:

  • AWS Cloud Formation( To provision the Infra)

  • IAM Role( For getting access to AWS resources)

  • Lambda( For serverless deployment)

  • API Gateway(To generate the API which enables us to trigger the event)

  • S3 Bucket( To store the project resources )

  • Deployment Diagram

    Conclusion

    Here we have used a tool called Serverless to provision our infrastructure by integrating it with our Cloud Provider AWS.

    This tool uses several AWS-managed services and helps us to deploy our Application.

Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Happy Learning!

~kritika :)

Connect with me: LinkedIn

0
Subscribe to my newsletter

Read articles from Kritika Shaw directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kritika Shaw
Kritika Shaw

🛠️ Exploring DevOps tools: Docker, Kubernetes, Jenkins, Terraform, and more. 🌐Let's connect, learn, and grow together.