docs.nestjs.com icon indicating copy to clipboard operation
docs.nestjs.com copied to clipboard

Serverless deploy

Open kamilmysliwiec opened this issue 8 years ago • 43 comments

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request (new chapter/page)
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

No docs

Expected behavior

I would like to see GCP/AWS/Azure (basically serverless) documentation.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


For Tooling issues:
- Node version: XX  
- Platform:  

Others:

kamilmysliwiec avatar Jul 04 '18 11:07 kamilmysliwiec

+1

samdelagarza avatar Jul 04 '18 17:07 samdelagarza

google cloud, or aws lambda, azure functions...

samdelagarza avatar Jul 04 '18 17:07 samdelagarza

+1

simonpeters avatar Aug 07 '18 07:08 simonpeters

+1

dpapukchiev avatar Aug 18 '18 00:08 dpapukchiev

+1

rdlabo avatar Aug 18 '18 02:08 rdlabo

I created repository how to use nestjs in serverless framework. I hope this will help. https://github.com/nestjs/nest/issues/238

rdlabo/serverless-nestjs https://github.com/rdlabo/serverless-nestjs

rdlabo avatar Aug 22 '18 07:08 rdlabo

Cross post from #238, but don't want it to get buried in a closed issue...

For those of you looking to deploy to APIGateway (proxy+) + Lambda, I just figured it out (Thanks @brettstack for all your work). Took me some time to piece all the parts together, hope this helps someone else out.

lambda.ts:

require('source-map-support').install();

import { Context, Handler } from 'aws-lambda';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Server } from 'http';
import { createServer, proxy } from 'aws-serverless-express';
import { eventContext } from 'aws-serverless-express/middleware';
import express from 'express';

let cachedServer: Server;
const expressApp = require('express')();

// NOTE: If you get ERR_CONTENT_DECODING_FAILED in your browser, this is likely
// due to a compressed response (e.g. gzip) which has not been handled correctly
// by aws-serverless-express and/or API Gateway. Add the necessary MIME types to
// binaryMimeTypes below
const binaryMimeTypes: string[] = [];

async function bootstrapServer(): Promise<Server> {
  const nestApp = await NestFactory.create(AppModule, expressApp);
  nestApp.use(eventContext());
  await nestApp.init();
  return createServer(expressApp, undefined, binaryMimeTypes);
}

export const handler: Handler = async (event: any, context: Context) => {
  if (!cachedServer) {
    console.log('Bootstraping server');
    cachedServer = await bootstrapServer();
  } else {
    console.log('Using cached server');
  }
  return proxy(cachedServer, event, context, 'PROMISE').promise;
};

For extra credit, you can add the following to your package.json's scripts to reduce the size of your zip file prior to deploy. If someone knows of an npm module to only include the code in node_modules that typescript needs I'd appreciate it:

"package": "rm /tmp/lambda.zip; zip -r /tmp/lambda.zip dist node_modules -x 'node_modules/typescript/*' 'node_modules/@types/*'"

rynop avatar Aug 23 '18 03:08 rynop

@rynop, I'm exploring using AWS Lambda with Nestjs. Where does your code go? In each Lambda function? Or does code in Lambda call it from an EC2 server with each Lambda call? Or some other setup?

jmpreston avatar Nov 17 '18 18:11 jmpreston

I have build an API powered by NestJs, and serverless (https://github.com/serverless/serverless) hosted on lambda.

It runs in production.

What I have done :

A single one handler (endpoint) /api/v1/{proxy+} redirect to the nestjs app.

For performance reasons, the handler is caching :

  • express App
  • Nestjs app
  • Mongodb connection

To avoid preflight CORS requests, I choose to only accept only GET, HEAD, POST requests whith text/plain Content-Type.


I think I can improve performance by using fastify instead of express, but I don't manage it for the moment

neilime avatar Nov 30 '18 09:11 neilime

Lambda layers is the way to go now. I haven't spent time trying to figure out how this works yet. We need a good blog post on this. I'm not advanced enough for that.

jmpreston avatar Dec 13 '18 17:12 jmpreston

also interested in this

dylandechant avatar Jan 08 '19 04:01 dylandechant

@svstartuplab @dylandechant I built an open source project called https://github.com/rynop/aws-blueprint that is an easy to use CI/CD driven blueprint you can use to drive production grade applications.

In the examples section, check out the abp-single-lambda-api. It will do everything you need to run a NodeJs based app. While I don't have an explicit NestJs example, my comment above from 8/22 should be a copy/paste replacement for the typescript example handler (just replace contents of this file with my post above).

aws-blueprint does too many things to cover in this post, however I think once you give it a try and get over the small learning curve, you will see it really gives you alot (ex: @neilime everything you mention aws-blueprint gives you for "free" - plus MUCH more).

Last - if you need to talk to a RDS DB or need to communicate to resources in a VPC (ex Mongo cluster), I'd save yourself some time and NOT use Lambda with NestJs (or any API for that matter). The ENI attachments cause killer cold starts. Instead I'd use fargate - aws-blueprint has a nice blueprint for it (see abp-fargate in the Examples section).

rynop avatar Jan 08 '19 13:01 rynop

@rynop I'm a single dev and don't want to get into CI/CD or containers. Either just Ubuntu as setup on my dev machine and just deploy to AWS EC2 or API Gateway and Lambda. I already have Postgres setup on RDS. My current app won't have much traffic, at least for a while.

So Lambda Layers is interesting to me. I'm trying to figure out how to setup Layers with Nestjs. So far it is difficult to find a good blog post or docs on this. I have Nestjs / REST working well in dev but don't have the Lambda concept with it worked out yet. Also exploring GraphQL.

jmpreston avatar Jan 21 '19 20:01 jmpreston

I don't know much about Layers yet, but my initial read was it is overly complex. Hence I haven't set aside time to investigate.

rynop avatar Jan 21 '19 20:01 rynop

Updated rdlabo/serverless-nestjs to nestjs6, and add environment for swagger. I hope this will help.

rdlabo/serverless-nestjs https://github.com/rdlabo/serverless-nestjs

rdlabo avatar Mar 24 '19 06:03 rdlabo

I used Nestjs in a free time project I made. Its fully serverless with DynamoDB, DynamoDB Streams, S3 triggers etc... You can see my way of using Nestjs and deploying to AWS if you are looking for an example. Also includes Lambda with webpack

https://github.com/International-Slackline-Association/Rankings-Backend

related with nestjs/nest#238

Can-Sahin avatar Apr 01 '19 08:04 Can-Sahin

Please have a look into deploying nestjs to now.sh, would be great!

cctoni avatar Jul 16 '19 14:07 cctoni

I found this package for AWS Lambda/API Gateway, using methods above with nestjs fastify:

https://github.com/benMain/aws-serverless-fastify

vfrank66 avatar Jul 26 '19 02:07 vfrank66

For a few days I was trying to use @nestjs/graphql with the serverless approach, after a few tries I came up with a solution that worked well.

If you are looking for nestjs + graphql + code-first on aws lambda, this example might be useful for you. I am using in production 👍

Repository: https://github.com/lnmunhoz/nestjs-graphql-serverless

lnmunhoz avatar Jul 29 '19 09:07 lnmunhoz

We have recently published an article about Nest + Zeit Now deployment cc @MarkPieszak https://trilon.io/blog/deploying-nestjs-to-zeit-now

kamilmysliwiec avatar Jul 31 '19 14:07 kamilmysliwiec