Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nestjs run on lambda function without creating an actual server to combine with AWS API Gateway?

I have 5 HTTP microservices written with NestJS. I have to convert them into lambda function where each service will have its own lambda function. The purpose of this is to completely turn my service to serverless.

I am using API Gateway to map requests to the right lambda by the given request path.

Now creating an MVC pattern from scratch that receives an URL path and resolves and controller & function needed (including url params, and such) is something that has already been done by both express and nestjs.

Is there a way to implement nesstjs's abstraction functionality without the actual server listening? So I can simply pass nestjs the URI and request data and it will work upon it?

Any other solutions for running an MVC serverless process on lambda?

like image 894
Ben Beri Avatar asked Dec 18 '25 07:12

Ben Beri


1 Answers

After researching the network and looking at these great answers, I thought of collecting that information all together and bring something more detailed, things that I was misunderstanding from these articles described and the ones I found.

How to create a serverless NestJS application using Lambda function AWS provider

Let's take a look at this repository article: https://github.com/rdlabo/serverless-nestjs

Thanks to him, he basically packed a ready-to-go NestJS project configured with the Serverless framework.

I am not experienced enough to say a lot about the serverless framework, but I can explain how it works in our case.

First of all, there is a serverless.yml as explained the the github repository article, basically here you describe the name of your lambda function, what to exclude in the package and the trigger http events.

functions:
  index:
    handler: dist/index.handler
    
    events:
    - http:
        cors: true
        path: '/'
        method: any
    - http:
        cors: true
        path: '{proxy+}'
        method: any

If we take a look at this yml file, you can see that we have 2 HTTP events, one for root path and one for proxy path.

A question that I asked myself when reading through it, so what does this event part do?

This part of the yml basically creates your endpoints in the API Gateway service of AWS (if you are using AWS as a provider). API Gateway is a service made by AWS that lets you map requests into other services of AWS, such as a lambda function.

When you run the command sls deploy after entering your credentials using sls config credentials the serverless framework will create OR modify if exists a new lambda function based on the config you set up, and set up API Gateway endpoints linked to that lambda.

After deploying you will receive a link that activates that lambda. The example I use basically uses an express serverless solution that someone created, basically a proxy code that knows how to receive API Gateway request object and transform it to express and activate it without having a server running.

Note: Serverless is using CloudFormation to create a stack in order to upload and deploy the lambda function, I think with this way you can upload more than 250mb unzipped, because my project currently is 450mb unzipped, I am not sure about this but when I tried uploading a bigger zip, my lambda started overflowing, as in saying that it is missing some modules, I guess because of the size.

Or maybe serverless really optimizes the modules so the uploaded package is much smaller than what you expect. If someone knows about this, +1!

like image 100
Ben Beri Avatar answered Dec 20 '25 21:12

Ben Beri