prisma: Query engine binary not found while deploying Next.js App to AWS using Serverless Framework

Bug description

I’m using the serverless component for next.js to deploy my app to AWS. Unfortunately, things didn’t go smoothly as expected. While testing the cloudfront distribution will try to execute a query from the nextjs api routes through the lambda.

The cloudfront will throw the following error:

503 ERROR
The request could not be satisfied.
The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
Generated by cloudfront (CloudFront)
Request ID: 63bKSGDPOK1vnJwOxPuvkR9dTEsqgqBFpeXpx3KKCiGMAeXfPtBt4w==

Looking inside the lambda logs I found out that the query engine binary was not found and the database couldn’t be reached. here’s the error thrown by the lambda:

        "stack": [
            "Error: Query engine binary for current platform \"rhel-openssl-1.0.x\" could not be found.",
            "This probably happens, because you built Prisma Client on a different platform.",
            "(Prisma Client looked in \"/query-engine-rhel-openssl-1.0.x\")",
            "",
            "Searched Locations:",
            "",
            "  /.prisma/client",
            "  /home/user/Documents/workspace/project/node_modules/@prisma/client",
            "  /",
            "  /var/task/node_modules/.prisma/client",
            "  /var/task",
            "  /var/task/node_modules/.prisma/client",
            "",
            "You already added the platforms \"native\", \"rhel-openssl-1.0.x\" to the \"generator\" block",
            "in the \"schema.prisma\" file as described in https://pris.ly/d/client-generator,",
            "but something went wrong. That's suboptimal.",
            "",
            "Please create an issue at https://github.com/prisma/prisma-client-js/issues/new",
            "    at PrismaClientFetcher.request (/var/task/pages/api/auth/[...nextauth].js:155205:15)"
        ]

What I tried

Following https://github.com/prisma/docs/issues/841 I tried to copy the relevant files during the deployment. I tried also to change the default location of the binary following https://github.com/serverless-nextjs/serverless-next.js/issues/843 but nothing worked.

How to reproduce

Steps to reproduce the behavior:

  1. Create a nextjs App with next-auth and prisma for passwordless login
  2. Deploy to aws using serverless. The exact component is '@sls-next/serverless-component@1.18.0'
  3. Test the login with the production environment
  4. See error

Expected behavior

The query engine binary will be correctly generated and be able to query the database without errors.

Prisma information

I think the following is the only relative information for the issue.

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "rhel-openssl-1.0.x"]
}

Environment & setup

  • OS: Linux Mint 20 Cinnamon
  • Database: PostgreSQL
  • Node.js version: v12.15.0
  • Prisma: 2.14.0
@prisma/cli          : 2.14.0
@prisma/client       : 2.14.0
Current platform     : debian-openssl-1.1.x
Query Engine         : query-engine 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/query-engine-debian-openssl-1.1.x)
Migration Engine     : migration-engine-cli 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine : introspection-core 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary        : prisma-fmt 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Studio               : 0.332.0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 27 (6 by maintainers)

Most upvoted comments

Thanks @janpio you are right that I needed to switch it to PRISMA_QUERY_ENGINE_LIBRARY. Not sure how I mixed that up. Thanks.

@janpio Sorry for my late reply 🥲 Here are my serverless.yml codes below. It referenced #5392 (comment), -> https://github.com/millsp/prisma-serverless-nextjs

It has a difference from the build command, here some script meaning build process in my case. “clean”: “rm -rf dist”, “build”: “yarn clean && yarn generate && next build”, “generate”: “yarn generate:prisma && yarn generate:nexus”, “generate:prisma”: “prisma generate”, “generate:nexus”: “ts-node --transpile-only -P tsconfig.nexus.json ./graphql/schema/index.ts”,

Additionally in my cases, GraphQL Apollo server + Nexus + Prisma in Next.js (e.g. /api/graphql.ts)

goodocstore:
  component: "@sls-next/serverless-component"
  inputs:
    runtime: "nodejs14.x"
    timeout: 30
    memory: 2048
    minifyHandlers: true
    useServerlessTraceTarget: true
    build:
      cmd: 'yarn'
      args: ['build']
      postBuildCommands:
        - PDIR=node_modules/.prisma/client/;
          LDIR=.serverless_nextjs/api-lambda/;
          if [ "$(ls -A $LDIR)" ]; then
          mkdir -p $LDIR$PDIR;
          cp "$PDIR"query-engine-rhel-* $LDIR$PDIR;
          cp "$PDIR"schema.prisma $LDIR$PDIR;
          fi;
        - PDIR=node_modules/.prisma/client/;
          LDIR=.serverless_nextjs/default-lambda/;
          if [ "$(ls -A $LDIR)" ]; then
          mkdir -p $LDIR$PDIR;
          cp "$PDIR"query-engine-rhel-* $LDIR$PDIR;
          cp "$PDIR"schema.prisma $LDIR$PDIR;
          fi;
    bucketName: 'goodocstore-assets'
    bucketRegion: 'ap-northeast-2'
    cloudfront:
      distributionId: MASKING
    name:
      defaultLambda: 'goodocstore-lambda'
      apiLambda: 'goodocstore-api-lambda'

This can be solved by adding a script that triggers before upload. I have set a template up for prisma + serverless-next.js which adds a postBuildCommands (serverless.yml) script to do just that.

using nextjs + prisma + serverless. having similar issue.

Serverless: Typescript compiled.
Serverless: Optimize: starting engines
Serverless: Optimize: accounts-dev-main
 
  Syntax Error -------------------------------------------
 
  SyntaxError: Unexpected token (19074:12) while parsing /Users//services/node_modules/@prisma/client/runtime/index.js while parsing file: /Users//services/node_modules/@prisma/client/runtime/index.js
      at DestroyableTransform.end [as _flush] (/Users//services/node_modules/insert-module-globals/index.js:114:21)