Hi guys, I’m trying to add prisma (<https://www.pr...
# help
m
Hi guys, I’m trying to add prisma (https://www.prisma.io/) to a lambda and trying to deploy it. I have an issue because prisma requires a binary be present in the lambda source. That is not included in the build and it errors out. Is there a way to add the required binary to the bundled package? With serverless fw. this can be achieved with a custom post-install script written in bash, bu t I’m unsure on how to tackle this with SST 🤔
r
Not sure of the 'right' approach, but you could add prisma into a lambda layer
f
Hey @Mitja O, Ross is right. Can you see if this code snippet help? https://github.com/serverless-stack/serverless-stack/issues/383#issuecomment-856267447
m
I’m a bit unsure on the snippet above, I’m fairly new to sst, is that code supposed to be part of a stack configuration?
f
Are you using the
sst.Api
construct to create an API endpoint? Let me know and I can shared some code to show to use the code referenced in the GitHub comment above.
m
I am yes, So its a super simple example that calls a lambda via an API gateway that then calls a prisma function. I cannot get it working on deploy because it says that the rhel binary for prisma is missing
f
In the stack you are creating the
sst.Api
, can you do something like:
Copy code
export class MainStack extends sst.Stack {

  constructor(scope: <http://sst.App|sst.App>, id: string, props?: sst.StackProps) {
    super(scope, id, props);

    const prismaData = buildPrisma();

    new sst.Api(this, "MyApi", {
      defaultFunctionProps: {
        layers: [...prismaData.layers],
        bundle: {
          externalModules: prismaData.externalModules,
        }
      },
      routes: {
        "GET /": "src/lambda.main",
      },
    });

  }

  buildPrisma() {
    const layer_root = ".build/layer"
    const layer_node = path.join(layer_root, "nodejs")
    const to_copy = [
      "node_modules/@prisma/client",
      "node_modules/@prisma/engines/migration-engine-rhel-openssl-1.0.x",
      "node_modules/@prisma/engines/query-engine-rhel-openssl-1.0.x",
      "node_modules/@prisma/engines/package.json",
      "node_modules/@prisma/engines/dist",
      "node_modules/prisma",
      "node_modules/.prisma",
    ]
    execSync(`rm -rf ${layer_root}`)
    for (let src of to_copy) {
      const parent = path.join(src, "../")
      execSync(`mkdir -p ${layer_node}/${parent}`)
      console.log(`Copying ${src} to ${layer_node}/${parent}`)
      execSync(`cp -a ${src} ${layer_node}/${parent}`)
    }

    const to_delete = [
      "node_modules/prisma/query-engine-rhel-openssl-1.0.x",
      "node_modules/prisma/query-engine-debian-openssl-1.1.x",
      "node_modules/.prisma/query-engine-rhel-openssl-1.0.x",
      "node_modules/.prisma/query-engine-debian-openssl-1.1.x",
    ]
    for (let src of to_delete) {
      execSync(`rm -rf ${src}`)
    }

    const layer = new lambda.LayerVersion(stack, "Prisma", {
      code: lambda.Code.fromAsset(layer_root),
      compatibleRuntimes: [lambda.Runtime.NODEJS_14_X],
    })
    execSync(`rm -rf ${layer_root}`)
    return {
      layers: [layer],
      externalModules: [
        "@prisma/client",
        "prisma",
        ".prisma/client",
        "@prisma/engines",
      ],
    }
  }

}
You call
buildPrisma()
to build the layer, and by setting it in
defaultFunctionProps
, all Lambda functions in the Api will be using this layer.
Let me know if that works for you.
m
that worked thanks!
a
Was having this issue too and worked great! thanks! I had to remove the following lines for some reason, i didn’t have them in the node_modules folder… not sure if it is a change in the latest version of prisma. 🤷
Copy code
"node_modules/@prisma/engines/migration-engine-rhel-openssl-1.0.x",
"node_modules/@prisma/engines/query-engine-rhel-openssl-1.0.x",