English 中文(简体)
Monorepo NestJS API Application + Multiple AWS Lambdas using Serverless
原标题:
The bounty expires in 2 hours. Answers to this question are eligible for a +50 reputation bounty. jusimen wants to draw more attention to this question.

I m working on building a NestJS API application using a monorepo approach to keep all my endpoints organized in one codebase. However, I m facing a challenge when it comes to deploying this application to multiple AWS Lambdas, each representing a different module (like Auth, Users, Posts, etc.), and then mapping them to a unified API Gateway.

In my previous attempts, I ve created separate NestJS projects for each module, which resulted in a maintenance nightmare due to shared code complexities. Now, I m exploring the option of maintaining a single codebase for all modules. My goal is to deploy each module as an individual AWS Lambda function, while still keeping the codebase manageable.

So, here s my main question: Can I achieve this setup using the Serverless Framework? I want to have a single codebase for all the modules and deploy them to separate AWS Lambdas. Additionally, I m aiming to integrate these Lambdas under a single API Gateway to provide a unified API access point.

Any insights, advice, or examples on how to structure my Serverless Framework configuration, how to manage the shared codebase efficiently, and how to set up the API Gateway to work with these multiple Lambdas would be greatly appreciated. Thank you in advance for your help!

问题回答

Interesting. I m trying to achieve the same. What I did in my scenario with turborepo, was installing serverless-nest-monorepo which does pretty much what you want if you check the docs. Then you can just integrate the command in your CI to deploy the lambda or deploy on your machine. However, It doesn t seem to be working with nestjs 16 properly by reading the issues...

There are also other tools such as serverless-plugin-monorepo but I had a bug using it. It seems that dependencies of dependencies are not being loaded into the lambda function.

The major problem that I m having with trying to deploy multiple NestJS applications in a monorepo is that all their dependencies are inside the root node_modules and this means that using just serverless deploy doesn t work because it doesn t add all the dependencies necessary. And that is some of the reason why the 2 plugins I listed before exist.

You could also try using AWS CDK to manage these deploys, it might be easier that handling the serverless npm module to understand your monorepo.

Create two repos: one for your NestJS Application and one for the AWS infrastructure.

In your NestJS App, add your APIs (Auth, Users, Posts, etc) but do not implement them there. In those APIs, add calls to your AWS Lambdas. Your main logic will happen in Lambdas as you requested.

In the infrastructure package (I recommend using AWS CDK), create an API Gateway. For each API endpoint (Auth, User, etc), create a resource. For each resource, add the method (POST, GET) and attach a Lambda function.

This approach will be very manageable. You will have two code packages. One for the front-end and one for the back-end.

You can add as many APIs as you want and you will have a single API Gateway (unified) that will handle them all.


AWS CDK Examples for the unified API Gateway

// API Gateway
const api = new RestApi({});

// Add resources
const auth = api.addResource( auth );
const users = api.addResource( users );

// Attach Lambda to the endpoint
auth.addMethod( POST , AuthLambdaIntegration...)
users.addMethod( POST , UserLambdaIntegration...)




相关问题
Mount windows shared drive to MWAA in bootscript

In MWAA startup script sudo yum install samba-client cifs-utils -y sudo mount.cifs //dev/test/drop /mnt/dev/test-o username=testuser,password= pwd ,domain=XX Executing above commonds giving error - ...

How to get Amazon Seller Central orders programmatically?

We have been manually been keying Amazon orders into our system and would like to automate it. However, I can t seem to figure out how to go about it. Their documentation is barely there. There is: ...

Using a CDN like Amazon S3 to control access to media

I want to use Amazon S3/CloudFront to store flash files. These files must be private as they will be accessed by members. This will be done by storing each file with a link to Amazon using a mysql ...

unable to connect to database on AWS

actually I have my website build with Joomla hosted on hostmonster but all Joomla website need a database support to run this database is on AWS configuration files need to be updated for that I ...

Using EC2 Load Balancing with Existing Wordpress Blog

I currently have a virtual dedicated server through Media Temple that I use to run several high traffic Wordpress blogs. Both tend to receive sudden StumbleUpon traffic surges that (I m assuming) ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

热门标签