Prima client is causing Function App to give Error: Encountered an error (ServiceUnavailable) from host runtime.

Atin 20 Reputation points
2025-02-27T21:27:07.14+00:00

Hi,
I am deploying a nodejs app on Azure functions.
It works well, but then I need to integrate Prisma for DB interactions

As soon as i add code related to that:

import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient()

It starts giving error as Encountered an error (ServiceUnavailable) from host runtime.

I am not able to see in logs what is the exact reason of Error, as the docker container do not even start.

I have tried enabling and looking at Docker logs, can't find any real reason.

Here is the schema.prima, all standard stuff:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["multiSchema"]
  binaryTargets   = ["native", "debian-openssl-3.0.x"]
}

The only thing i see in docker logs is, which is not very helpful:

2025-02-27T00:17:04.042557826Z WEBSITES_INCLUDE_CLOUD_CERTS is set to true.
2025-02-27T00:17:04.158384433Z Updating certificates in /etc/ssl/certs...
2025-02-27T00:17:08.665364473Z 7 added, 0 removed; done.
2025-02-27T00:17:08.667196024Z Running hooks in /etc/ca-certificates/update.d...
2025-02-27T00:17:08.673442550Z done.
2025-02-27T00:17:08.862182625Z Starting OpenBSD Secure Shell server: sshd.

I am using publish command to deploy it:

func azure functionapp publish {functionname}

Obviously in this case I cannot run prisma generate on server, so I am running that locally and making sure we have prisma client files are in node_modules/.prisma/client folder

Can you help me troubleshoot it.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pravallika Kothaveeranna Gari 955 Reputation points Microsoft External Staff Moderator
    2025-03-03T09:50:01.37+00:00

    Hello @Atin

    Thanks for using Q & A forum.

    Follow below steps:

    1. Create Typescript Http Triggered Azure function.

    Run the commands:

    
    npm install @prisma --save-dev  
    
    npm install @prisma/client
    
    npx prisma init
    
    

    .env_ and the schema.prisma file gets created.

    schema.prisma:

    
    generator client {
    
      provider = "prisma-client-js"
    
    }
    
    datasource db {
    
      provider = "postgresql"
    
      url      = env("DATABASE_URL")
    
    }
    
    model todo {
    
      completed Boolean? @default(false)
    
      id        Int      @default(autoincrement()) @id
    
      todo      String
    
    }
    
    
    1. Code snippet:
    
    import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
    import { PrismaClient } from '@prisma/client';
    
    const prisma = new PrismaClient();
    
    interface TodoRequestBody {
       todo: string;
    }
    
    export async function httpTrigger1(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
    
        context.log(`HTTP function processed request for URL "${request.url}"`);
        try {
            const body = await request.json() as TodoRequestBody;        
    
            if (body && body.todo) {
                const todo = body.todo;
                const item = await prisma.todo.create({
                    data: { todo }
                });
    
                return {
                    status: 200,
                    jsonBody: item
                };
    
            } else {
                return {
                   status: 400,
                    body: "Please pass a todo in the request body"
                };
    
            }
    
        } catch (error) {
            context.log(error);
            return {
                status: 500,
                body: "An error occurred on the server"
            };
        }
    }
    
    app.http('httpTrigger1', {
        methods: ['POST'],
        authLevel: 'anonymous',
        handler: httpTrigger1
    });
    
    
    1. Provide the correct connection string of postgres database in .env file:
    DATABASE_URL="postgresql://username:password@hostname:port/database?schema=public"
    
    1. Run npx prisma generate to generate Prisma client libraries in a separate folder named Prisma.

    Able to run locally:

    
    Functions:
    
            httpTrigger: [GET,POST] http://localhost:7071/api/httpTrigger
    
    For detailed output, run func with --verbose flag.
    [2025-03-03T07:18:26.898Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
    [2025-03-03T07:18:59.023Z] Executing 'Functions.httpTrigger' (Reason='This function was programmatically called via the host APIs.', Id=48eb753d-52a1-42d9-b925-6575546a06f6)
    [2025-03-03T07:18:59.150Z] Http function processed request for url "http://localhost:7071/api/httpTrigger"
    [2025-03-03T07:18:59.248Z] Executed 'Functions.httpTrigger' (Succeeded, Id=48eb753d-52a1-42d9-b925-6575546a06f6, Duration=263ms)
    
    

    enter image description here

    Add DATABASE_URL=<Postgres_connection_string> in the Function App's App Settings.

    Deployed to Azure:

    
    C:\Users\uname\nodefn>npx func azure functionapp publish kpfn3
    
    Getting site publishing info...
    [2025-03-03T07:20:53.062Z] Starting the function app deployment...
    Creating archive for current directory...
    Uploading 80.91 MB [##############################################################################]
    Upload completed successfully.
    Deployment completed successfully.
    
    [2025-03-03T07:22:28.282Z] Syncing triggers...
    Functions in kpfn3:
    
        httpTrigger - [httpTrigger]
    
            Invoke url: https://function.azurewebsites.net/api/httptrigger
    
    

    Portal:

    enter image description here

    
    2025-03-03T07:24:28Z   [Verbose]   AuthenticationScheme: WebJobsAuthLevel was successfully authenticated.
    2025-03-03T07:24:28Z   [Verbose]   Authorization was successful.
    2025-03-03T07:24:28Z   [Information]   Executing 'Functions.httpTrigger' (Reason='This function was programmatically called via the host APIs.', Id=171c936f-08af-4bdd-bc94-9235c8a194e8)
    2025-03-03T07:24:28Z   [Verbose]   Sending invocation id: '171c936f-08af-4bdd-bc94-9235c8a194e8
    2025-03-03T07:24:28Z   [Verbose]   Posting invocation id:171c936f-08af-4bdd-bc94-9235c8a194e8 on workerId:edd86f41-6037-4de3-b4af-7945f3354afe
    2025-03-03T07:24:29Z   [Information]   Http function processed request for url "https://function.azurewebsites.net/api/httpTrigger"
    2025-03-03T07:24:29Z   [Information]   Executed 'Functions.httpTrigger' (Succeeded, Id=171c936f-08af-4bdd-bc94-9235c8a194e8, Duration=171ms)
    
    

    enter image description here Hope this helps.

    If the Answer is helpful, please click Accept Answer and Up-Vote, so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.