Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serverless deploy #96

Closed
kamilmysliwiec opened this issue Jul 4, 2018 · 43 comments
Closed

Serverless deploy #96

kamilmysliwiec opened this issue Jul 4, 2018 · 43 comments

Comments

@kamilmysliwiec
Copy link
Member

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:

@samdelagarza

This comment has been minimized.

@samdelagarza
Copy link

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

@simonpeters

This comment has been minimized.

2 similar comments
@dpapukchiev

This comment has been minimized.

@rdlabo

This comment has been minimized.

@rdlabo
Copy link

rdlabo commented Aug 22, 2018

I created repository how to use nestjs in serverless framework. I hope this will help. nestjs/nest#238

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

@rynop
Copy link
Contributor

rynop commented Aug 23, 2018

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/*'"

@jmpreston
Copy link

@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?

@neilime
Copy link
Contributor

neilime commented Nov 30, 2018

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

@jmpreston
Copy link

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.

@dylandechant
Copy link

also interested in this

@rynop
Copy link
Contributor

rynop commented Jan 8, 2019

@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).

@jmpreston
Copy link

@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.

@rynop
Copy link
Contributor

rynop commented Jan 21, 2019

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.

@rdlabo
Copy link

rdlabo commented Mar 24, 2019

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

@Can-Sahin
Copy link

Can-Sahin commented Apr 1, 2019

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

@cctoni
Copy link

cctoni commented Jul 16, 2019

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

@vfrank66
Copy link

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

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

@lnmunhoz
Copy link

lnmunhoz commented Jul 29, 2019

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

@kamilmysliwiec
Copy link
Member Author

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

@samdelagarza
Copy link

samdelagarza commented Jul 31, 2019 via email

@murraybauer
Copy link

I saw on Twitter a few days ago an annoucement for an offical Nest Schematic for Azure Functions:

Can add via nest add @nestjs/azure-func-http schematic

Uses azure-express behind the schemes:
https://github.com/nestjs/azure-func-http/blob/885fb33c109ed2a0b59c97fe0912bf412bf2228e/lib/azure-http.adapter.ts

@rynop
Copy link
Contributor

rynop commented Aug 1, 2019

I have a new blueprint that contains a working example of NestJS (using Fastify engine) with API Gateway to Lamba. CI/CD driven deploys. Local APIG+Lamba and DyanmoDB local support. https://github.com/rynop/abp-sam-nestjs

@kamilmysliwiec I think NestJS app running behind API Gateway and in Lambda is a pretty compelling/powerful use case. I'm happy to do the work to document how to do this but:

Is this something you want documented in the official docs? If yes: where and I'll work on a PR.

If no:

Not just trying to get notoriety for my repo. The concept is not super straight forward, and IMO should be an entity that evolves on its own - updating best practices as AWS evolves...

"not a good fit for nestjs docs" is a fine answer too - wont hurt my feelings.

@kop7
Copy link

kop7 commented Aug 26, 2019

any example how to deploy serverless nestjs with typeorm?

It should be simple???

I create CompanyModule in nest-serverless project with typeorm crud:

companyModule

@Module({
  imports: [TypeOrmModule.forFeature([Company])],
  controllers: [CompanyController],
  providers: [CompanyService],
})
export class CompanyModule {}

CompanyService

@Injectable()
export class CompanyService extends TypeOrmCrudService<Company> {
    constructor(
        @InjectRepository(Company)
        private readonly CompanyRepository: Repository<Company>) {
        super(CompanyRepository);
    }
}

CompanyEntity

@Entity('company')
export class Company {
    @PrimaryGeneratedColumn()
    id: number;
    @Column()
    name: string;
}

CompanyController

@Crud({model: {
        type: Company,
    }})
@Controller('api/company')
export class CompanyController implements CrudController<Company> {
    constructor(public readonly service: CompanyService) {}
}

AppModule:

@Module({
  imports: [
    TypeOrmModule.forRoot({
          type: 'mysql',
          host: 'x.x.x.x,
          port: 3306,
          username: 'xxxxx',
          password: 'xxxxx',
          database: 'xxxx',
          charset: 'utf8',
          logging: false,
          logger: 'advanced-console',
          entities: [__dirname + './../**/*.entity!(*.d).{ts,js}'],
          keepConnectionAlive: true,
          synchronize: true,
    }),
    CompanyModule,
  ],
  controllers: [ AppController],
  providers: [ AppService,],
})
export class AppModule { }

commad: rimraf dist && npm run build && sls deploy -s dev

Serverless: Optimize: starting engines
Serverless: Optimize: nestjs-restapi-dev-company
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service nestjs-restapi.zip file to S3 (6.71 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.........................................
Serverless: Stack update finished...
Service Information
service: nestjs-restapi
stage: dev
region: eu-central-1
stack: nestjs-restapi-dev
resources: 35
api keys:
  None
endpoints:
  ANY - https://xxxxxxxxxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/company

functions:
  company: nestjs-restapi-dev-company
layers:
  None
Serverless: Removing old service artifacts from S3...
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.

Getting errors from Aws:


�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[NestFactory] �[39m�[32mStarting Nest application...�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mTypeOrmModule dependencies initialized�[39m�[38;5;3m +91ms�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mPassportModule dependencies initialized�[39m�[38;5;3m +1ms�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mJwtModule dependencies initialized�[39m�[38;5;3m +1ms�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mAdsModule dependencies initialized�[39m�[38;5;3m +0ms�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mAppModule dependencies initialized�[39m�[38;5;3m +1ms�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mTypeOrmCoreModule dependencies initialized�[39m�[38;5;3m +77ms�[39m
�[31m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[ExceptionHandler] �[39m�[31mNo repository for "Company" was found. Looks like this entity is not registered in current "default" connection?�[39m�[38;5;3m +1ms�[39m
RepositoryNotFoundError: No repository for "Company" was found. Looks like this entity is not registered in current "default" connection?
at new RepositoryNotFoundError (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:179054:28)
at EntityManager.getRepository (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:177032:19)
at Connection.getRepository (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:153557:29)
at getRepository (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:10232:26)
at InstanceWrapper.useFactory [as metatype] (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:10241:20)
at Injector.instantiateClass (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:4017:55)
at callback (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:3812:41)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)

@kamilmysliwiec
Copy link
Member Author

We started working on articles:

More articles coming soon

@kop7
Copy link

kop7 commented Sep 16, 2019

If someone is going to need it, there's an example:serverless-nestjs-typeorm

@ripley
Copy link

ripley commented Sep 18, 2019

@rynop
Would you elaborate a bit on how to handle the aws event proxied to the cached nest server?

I'm trying to implement an authorizer lambda with nestjs, the auth token is passed in as "authorizationToken" field in the event object from AWS API gateway.

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
Copy link
Contributor

rynop commented Sep 19, 2019

@ripley check out https://github.com/rynop/abp-sam-nestjs mentioned in my #96 (comment) above. An example is in that repo @ https://github.com/rynop/abp-sam-nestjs/blob/master/src/apig-lambda.ts

@vadistic
Copy link

vadistic commented Oct 10, 2019

I've come up with something like this for Zeit Now @now/node builder.

Works with now GitHub CI / with cloud compilation - if I give up using tsconfig-paths

import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import express from 'express';
import { from } from 'rxjs';

import { AppModule } from './app.module';

async function bootstrap() {
  const instance = express();
  const adapter = new ExpressAdapter(instance);
  const app = await NestFactory.create(AppModule, adapter);

  await app.init();

  return instance;
}


// Observable for cold start
const server = from(bootstrap());

export default async function serverless(req, res) {
  const handler = await server.toPromise();

  return handler(req, res);
}

@vadistic
Copy link

Also did someone managed to get @nestjs/graphql on azure functions?

Zeit Now is blocked by this: vercel/vercel#3115, so I wanted to try azure. Rest functions/ rests parts of nest apps are working fine, but I'm getting silent 204/timeous on graphql requests (both in func start & deployed). Whole app inits and works - just nothing is returned from GraphQLModule. No exceptions either.

I've tried all permutation of:

  • disable cors on whole app

  • disable cors in GraphQLModule

  • useGlobalPrefix in GraphQLModule both ways

  • diable all fs related things (autoSchemaFile: false & typeDefs from string const)

  • disable/prune all subscription related things

  • and here https://www.apollographql.com/docs/apollo-server/deployment/azure-functions/ theyre recommending to use some magic $return token which does god knows what...

// function.json
   {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }

I'm runnig out of ideas. I would be glad to simply hear it's possible 😄

@jparneodo
Copy link

@vadistic Do you solved your problem ?
I am stuck also on same point.

To be sure of kinematic, I started with:
set DEBUG=* & npm run start:azure

In file node_modules/@nestjs/azure-func-http/dist/azure-http.adapter.js line 16
return handler(context, req);

Returns undefined

Not sure but it doesn't seems to be a good value.

@marekm1844
Copy link

marekm1844 commented Jan 8, 2020

'return proxy(cachedServer, event, context, 'PROMISE').promise;`

Today I had constant Error 502 from Api Gateway and could not figure out what is going on since Lambda logs were ok.

At the end the returning promise in proxy server saved the day and 502 Internal server error is gone !

@karocksjoelee
Copy link

I am wondering if someone thinks mapping NestJS controller to Serverless Lambda handler is a good idea ?

Basically I want local develop server can have Swagger document and can deploy each API end point as a Lambda function. Is this possible ?

@marekm1844
Copy link

I am wondering if someone thinks mapping NestJS controller to Serverless Lambda handler is a good idea ?

Basically I want local develop server can have Swagger document and can deploy each API end point as a Lambda function. Is this possible ?

You can split into multiple functions with serverless and using direct path to controller instead of {proxy} but you would still need whole nestjs instance for each lambda to use shared stuff like guards, models etc.

@vnenkpet
Copy link

Is there any advice on how to run it with Google Cloud function? I think it should be pretty straightforward, but I can't figure out how should the exported function for google cloud look.

This is a default google cloud function:

export const helloWorld = (request, response) => {
  response.status(200).send("Hello World 2!");
};

I suppose I need to extract something like HttpServer or HttpAdapter from nest app instance and pass the request/response parameters to it, but I'm not sure on how to exactly do it.

The only issue about this that I could find nestjs/nest#238 basically only answered for AWS for reasons I don't understand 😕

@vnenkpet
Copy link

vnenkpet commented Jan 12, 2020

Ok, I figured it out, for anyone who wants to run Nestjs with Google Cloud Functions (without Firebase), it's actually quire straightforward:

import { NestFactory } from "@nestjs/core";
import { ExpressAdapter } from "@nestjs/platform-express";
import { AppModule } from "./app.module";
import express from "express";

const server = express();

export const createNestServer = async expressInstance => {
  const app = await NestFactory.create(
    AppModule,
    new ExpressAdapter(expressInstance)
  );

  return app.init();
};

createNestServer(server)
  .then(v => console.log("Nest Ready"))
  .catch(err => console.error("Nest broken", err));

export const handler = server;

You can also add this to package.json to make the process a bit more straightforward:

  "main": "dist/handler.js",
  "scripts": {
    "gcp:start": "functions-framework --target=handler --port=8001",
    "gcp:watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run gcp:watch-ts\" \"npm run gcp:watch-node\"",
    "gcp:watch-node": "nodemon --watch dist --exec npm run gcp:start",
    "gcp:watch-ts": "nest build --watch",
    "gcp:deploy": "gcloud functions deploy handler --runtime nodejs10 --trigger-http"
 }

Of course you have to have concurrently, functions-framework etc. installed for this to run.
Then just

npm run gcp:watch

to run it locally, and

npm run gcp:deploy

to deploy.

@joffarex
Copy link
Contributor

joffarex commented Mar 2, 2020

Quick question: is it a good idea to run full api on lambda or any other similar service, which connects to db on basically every request?

@kukuandroid
Copy link

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

Hello, does the performance for monolith architecture is ok ? I'm thinking of implementing single-point access , do have you any examples ? Thank you

@TreeMan360
Copy link

TreeMan360 commented Apr 1, 2020

Hey all,

Since migrating to Nestjs 7 I am really struggling to get aws-serverless-express working. It was working absolutely fine on version 6 and it runs fine locally without the lambda.

I have tried an insane amount of different package version combinations but alas I still receive the following error:

ERROR TypeError: **express_1.default.Router is not a function at ApolloServer.getMiddleware** (/var/task/node_modules/apollo-server-express/dist/ApolloServer.js:81:42) at ApolloServer.applyMiddleware (/var/task/node_modules/apollo-server-express/dist/ApolloServer.js:76:22) at GraphQLModule.registerExpress (/var/task/node_modules/@nestjs/graphql/dist/graphql.module.js:119:22) at GraphQLModule.registerGqlServer (/var/task/node_modules/@nestjs/graphql/dist/graphql.module.js:103:18) at GraphQLModule.<anonymous> (/var/task/node_modules/@nestjs/graphql/dist/graphql.module.js:93:18) at Generator.next (<anonymous>) at fulfilled (/var/task/node_modules/tslib/tslib.js:110:62) at processTicksAndRejections (internal/process/task_queues.js:97:5)

I have wasted over a day on this now, so frustrating. Before going any further has anybody managed to get Nestjs 7 running on Lambda?

Thanks

@hbthegreat
Copy link

hbthegreat commented Apr 15, 2020

@TreeMan360 We have quite a few projects running on 6.9.0 and am yet to migrate to 7 but the error you are getting there looks it could be build related rather than nest version related. Are you using webpack by any chance?
If you are I ran into this same issue with postgres and a few other platforms and was able to solve it with serverless-webpack forceIncludes.
image

@TreeMan360
Copy link

@hbthegreat Actually this was a bug in Serverless Framework Enterprise with the bug residing in the SDK that is automatically included for enterprise/pro customers to do instrumentation.

https://github.com/serverless/serverless/issues/7543
https://forum.serverless.com/t/highly-confusing-express-router-issue/10987

Everything is now working absolutely fine on nestjs 7 :)

@hbthegreat
Copy link

@TreeMan360 Great to hear you got a resolution!

@danieldanielecki
Copy link

Ok, I figured it out, for anyone who wants to run Nestjs with Google Cloud Functions (without Firebase), it's actually quire straightforward:

import { NestFactory } from "@nestjs/core";
import { ExpressAdapter } from "@nestjs/platform-express";
import { AppModule } from "./app.module";
import express from "express";

const server = express();

export const createNestServer = async expressInstance => {
  const app = await NestFactory.create(
    AppModule,
    new ExpressAdapter(expressInstance)
  );

  return app.init();
};

createNestServer(server)
  .then(v => console.log("Nest Ready"))
  .catch(err => console.error("Nest broken", err));

export const handler = server;

You can also add this to package.json to make the process a bit more straightforward:

  "main": "dist/handler.js",
  "scripts": {
    "gcp:start": "functions-framework --target=handler --port=8001",
    "gcp:watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run gcp:watch-ts\" \"npm run gcp:watch-node\"",
    "gcp:watch-node": "nodemon --watch dist --exec npm run gcp:start",
    "gcp:watch-ts": "nest build --watch",
    "gcp:deploy": "gcloud functions deploy handler --runtime nodejs10 --trigger-http"
 }

Of course you have to have concurrently, functions-framework etc. installed for this to run.
Then just

npm run gcp:watch

to run it locally, and

npm run gcp:deploy

to deploy.

My setup was really similar for Firebase deployment, i.e.

import * as admin from 'firebase-admin';
import * as express from 'express';
import * as functions from 'firebase-functions';
import { ApplicationModule } from './app.module';
import { enableProdMode } from '@angular/core';
import { Express } from 'express';
import {
  ExpressAdapter,
  NestExpressApplication
} from '@nestjs/platform-express';
import { NestFactory } from '@nestjs/core';

enableProdMode(); // Faster server renders in production mode (development doesn't need it).
admin.initializeApp(); // Initialize Firebase SDK.
const expressApp: Express = express(); // Create Express instance.

(async () => {
  const nestApp = await NestFactory.create<NestExpressApplication>(
    ApplicationModule,
    new ExpressAdapter(expressApp)
  );
  nestApp.useStaticAssets(join(process.cwd(), 'dist/apps/my-app-browser'));
  nestApp.init();
})().catch(err => console.error(err));

// Firebase Cloud Function for Server Side Rendering (SSR).
exports.angularUniversalFunction = functions.https.onRequest(expressApp);

@nestjs nestjs locked and limited conversation to collaborators Jul 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests