Why not to deploy Express JS REST API using Serverless...
Tags: javascript, serverless, node, aws
Published: 2023-12-29 09:54:34

> Many developers deploy their existing express JS application on AWS Lambda as serverless application. However, this approach has some drawbacks and limitations that may affect the performance, scalability and cost of the applications. In this article, I will explain why using express JS on serverless is not a best practice and how to design a serverless application that leverages the benefits of the platform.

AWS Serverless Application flow:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vg5g7f7ft4aivjhtkdo5.png)

Express JS Application flow:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyj25d5i19wkuro4qkpi.png)

> When deploying an Express JS Application as a Lambda function, we overlay the actual AWS Serverless Application flow with Express JS. Consequently, the entire Express JS is initiated to handle every single HTTP request repeatedly. Although Express JS is popular for handling multiple requests at one time on a server.

Let's take an example function configuration of ExpressJS serverless app in `serverless.yml`:

```yaml
functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'\
```

In this approach, we redirect all the domain traffic to our application and delegate the routing logic completely to Express. This has an advantage—I can avoid setting up all my routes and functions manually. I can also reduce the effect of cold-starts on routes that are not frequently used.

However, we sacrifice some of the advantages of the serverless architecture. I can separate my pieces of logic into distinct functions and obtain a good view of my application from standard metrics. 

If each route is handled by a different Lambda function, then I can see:

- Each function can be independently developed and deployed
- Express JS library is overhead for each function as Routing, Authorization can be handled by AWS API Gateways and other AWS solutions very well.
- Decrease in AWS Lambda billing cost
- How many times each route is invoked
- How many errors I have for each route
- How long each route takes (and how much money I could save if I made that route faster)

# Some of the demerits of deploying express JS Application using serverless:

### Cold Starts
Express.js applications face significant delays during cold starts, impacting the responsiveness of serverless functions. The framework's initialization time is a notable bottleneck in a serverless environment.

### Resource Consumption
Express.js, with its extensive features like middleware support and routing, consumes unnecessary resources in a serverless context. Serverless functions should be lightweight and task-focused, avoiding the complexities of a full web framework.

## AWS API Gateway: The Key Player

### Request Handling
API Gateway forwards client requests to the associated AWS Lambda function, which contains the business logic.

### Response Handling
Upon execution, the Lambda function returns a response to API Gateway, which then sends it back to the client. API Gateway handles tasks like authentication and authorization.

### Event Triggers
Lambda functions can be triggered by various events, with HTTP requests being a common trigger in the context of an API.

## Lambda Function and API Gateway Integration

1. **Function Configuration:** Define a Lambda function with the required business logic.

2. **API Gateway Setup:** Create an API, configure endpoints, and link them to the Lambda function. API Gateway provides a URL endpoint for client access.

3. **Event Mapping:** Configure API Gateway to trigger the Lambda function based on HTTP request methods.

4. **Security and Authorization:** API Gateway enables secure API access through authentication and authorization mechanisms.

## Express.js Unnecessary for Routing

- **Streamlined Responsibilities:** Lambda functions focus on specific tasks, allowing optimal performance without managing route intricacies.

- **Serverless Philosophy:** Embrace the serverless paradigm by building small, focused functions that scale independently, aligning with optimal resource utilization.

## Conclusion

While Express.js excels in traditional web applications, its use in serverless architectures introduces complexities and overhead. Opt for AWS Lambda functions and API Gateway to create scalable, responsive, and cost-effective serverless REST APIs. By leveraging the strengths of serverless computing and offloading routing to API Gateway, you ensure an efficient and streamlined serverless application.