How to deploy nestjs app on azure function via serverless framework using serverless deploy

127 Views Asked by At

I want to deploy NestJS app on azure function via serverless framework. Need help to setup handler and serverless.yml for node 18 version

I tried below solution but didn't help.

import { Context, HttpRequest } from '@azure/functions';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

let cachedServer;

async function bootstrapServer(): Promise<any> {
  const app = await NestFactory.create(AppModule);
  await app.init();
  return app.getHttpAdapter().getInstance();
}

export async function handler(context: Context, req: HttpRequest): Promise<void> {
  if (!cachedServer) {
    cachedServer = await bootstrapServer();
  }

  context.res = {
    status: 200,
    body: 'Serverless NestJS on Azure Functions!',
  };

  await cachedServer(context.req, context.res);
}
1

There are 1 best solutions below

2
Pravallika KV On

I have created a NestJs Azure function with below commands:

1. nest new <project_name>
2. cd <project_name>
3. npm i @schematics/angular@^13.0.0
4. nest add @nestjs/azure-http-func

enter image description here

C:\Users\uname\nestjsapp\nest-app>npm run build && func host start
> [email protected] prebuild
> rimraf dist

> [email protected] build
> nest build

Azure Functions Core Tools
Core Tools Version:       4.0.5455 Commit hash: N/A  (64-bit)
Function Runtime Version: 4.27.5.21554

Warning: Proxies are not supported in Azure Functions v4. Instead of 'proxies.json', try Azure API Management: https://aka.ms/AAfiueq
[2024-02-12T12:12:08.356Z] Worker process started and initialized.

Functions:

        main:  http://localhost:7071/api/{*segments}

For detailed output, run func with --verbose flag.
[2024-02-12T12:12:12.620Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-02-12T12:12:15.869Z] Executing 'Functions.main' (Reason='This function was programmatically called via the host APIs.', Id=ab45f8eb-08ee-47d9-bbb5-7352e0bc5459)
[2024-02-12T12:12:16.376Z] [Nest] 28028  - 02/12/2024, 5:42:16 PM     LOG [NestFactory] Starting Nest application...
[2024-02-12T12:12:16.392Z] [Nest] 28028  - 02/12/2024, 5:42:16 PM     LOG [InstanceLoader] AppModule dependencies initialized +52ms
[2024-02-12T12:12:16.403Z] [Nest] 28028  - 02/12/2024, 5:42:16 PM     LOG [RoutesResolver] AppController {/api}: +12ms
[2024-02-12T12:12:16.407Z] [Nest] 28028  - 02/12/2024, 5:42:16 PM     LOG [RouterExplorer] Mapped {/api, GET} route +4ms
[2024-02-12T12:12:16.412Z] [Nest] 28028  - 02/12/2024, 5:42:16 PM     LOG [NestApplication] Nest application successfully started +4ms
[2024-02-12T12:12:16.642Z] Executed 'Functions.main' (Succeeded, Id=ab45f8eb-08ee-47d9-bbb5-7352e0bc5459, Duration=798ms)

Local Response:

enter image description here Run npm run build before deploying the application to Azure.

  • Create NodeJS Azure function with Consumption Plan.
  • Deployed the NestJS project to Azure using visual studio code.

enter image description here

5:49:44 PM kpnextfn: Creating zip package...
5:52:00 PM kpnextfn: Zip package size: 9.47 MB
5:52:01 PM kpnextfn: Zip package size: 9.47 MB
5:52:32 PM: Error: Deployment 'latest' not found.
5:52:28 PM kpnextfn: Updating submodules.
5:52:28 PM kpnextfn: Preparing deployment for commit id '952fd6bb88'.
5:52:29 PM kpnextfn: Skipping build. Project type: Run-From-Zip
5:52:29 PM kpnextfn: Skipping post build. Project type: Run-From-Zip
5:52:29 PM kpnextfn: Triggering recycle (preview mode disabled).
5:52:30 PM kpnextfn: Deployment successful.
5:52:46 PM kpnextfn: Started postDeployTask "npm install (functions)".
5:52:57 PM kpnextfn: Syncing triggers...
5:53:01 PM kpnextfn: Querying triggers...
5:53:09 PM kpnextfn: HTTP Trigger Urls:
  main: https://kpnextfn.azurewebsites.net/api/%7B*segments%7D

Portal:

enter image description here

References:

Deploy NestJS Serverless Apps to Azure Functions - Trilon Consulting