I am building a serverless function using the serv...
# prisma-client
k
I am building a serverless function using the serverless framework. However im having an issue with running it locally prisma/schema.prisma
Copy code
generator client {
    provider      = "prisma-client-js"
    binaryTargets = ["native", "rhel-openssl-1.0.x"]
}
serverless.ts
Copy code
package: {
        individually: true,
        patterns: [
            "!node_modules/.prisma/client/libquery_engine-*",
            "node_modules/.prisma/client/libquery_engine-rhel-*",
            "!node_modules/prisma/libquery_engine-*",
            "!node_modules/@prisma/engines/**",
        ],
    },
Copy code
npx prisma generate && npm install
Copy code
sls invoke local -f main
✖️ Error: ENOENT: no such file or directory, open ''/.esbuild/.build/node_modules/.prisma/client/schema.prisma'
What am i doing wrong here? attempting to follow this example: https://github.com/prisma/prisma-examples/tree/latest/deployment-platforms/aws-lambda • note: i am using the serverless
aws-nodejs-typescirpt
template which uses serverless-esbuild and not serverless-webpack
m
Hi @Kay Khan I was facing the same situation, avoided it with something like including the files needed for local, and excludend them for deployment, also created a lambda layer for prod.
Copy code
inject: ['../../prisma/schema.prisma',
'../../node_modules/.prisma/client/libquery_engine-rhel-openssl-1.0.x.so.node',
'../../node_modules/.prisma/client/libquery_engine-debian-openssl-3.0.x.so.node',
 ],
  assetNames: './[name]',
  loader: { '.prisma': 'file', '.node': 'file' },
this is what I added in my custom esbuild config for local, and, of course when deploying, not considering it.
k
@Marcos Iglesias hmm i dont understand, can i see your entire serverless.ts?
are you following some guide?
m
nop, just trial after trial, searched info in esbuild page and in prisma docs also
can you show me your "custom", "esbuild" object in your serverless.ts ?? it will be easier
k
custom esbuild object?
m
sorry, like this one
k
I dont touch that
Copy code
custom: {
        esbuild: {
            bundle: true,
            minify: false,
            sourcemap: true,
            exclude: ["aws-sdk"],
            target: "node16",
            define: { "require.resolve": undefined },
            platform: "node",
            concurrency: 10,
        },
    },
m
Try to add my lines, something like this:
Copy code
custom: {
  esbuild: {
      bundle: true,
      minify: false,
      sourcemap: true,
      exclude: ["aws-sdk"],
      target: "node16",
      define: { "require.resolve": undefined },
      platform: "node",
      concurrency: 10,
      inject: [
        '../../prisma/schema.prisma',
        '../../node_modules/.prisma/client/libquery_engine-rhel-openssl-1.0.x.so.node',
        '../../node_modules/.prisma/client/libquery_engine-debian-openssl-3.0.x.so.node',
      ],
      assetNames: './[name]',
      loader: { '.prisma': 'file', '.node': 'file' },
  },
},
you will need to resolve the correct paths and where the "assets" are going to be injected accoding to your project structure of course
you can avoid the ones injecting the debian-openssl file, because that's for my OS
k
I need it myu error message tells me i need that
Copy code
Query engine library for current platform "debian-openssl-1.1.x" could not be found.
You incorrectly pinned it to debian-openssl-1.1.x
I think i found the solution tho
1 moment
My initial error was missing schema.prisma so i included it in the patterns , then the second error was missing the debian-openssl-1.1.x
Copy code
package: {
        individually: true,
        patterns: [
            "!node_modules/.prisma/client/libquery_engine-*",
            "node_modules/.prisma/client/libquery_engine-rhel-*",
            "node_modules/.prisma/client/schema.prisma",
            "node_modules/.prisma/client/libquery_engine-debian-*",
            "!node_modules/prisma/libquery_engine-*",
            "!node_modules/@prisma/engines/**",
        ],
    },
Now i have them in the build
Copy code
esbuild/.build/node_modules/.prisma/client   master ±  ls

libquery_engine-debian-openssl-1.1.x.so.node  
schema.prisma
libquery_engine-rhel-openssl-1.0.x.so.node
and it works
m
in the "inject" you have to point the path to the file wich is going to be "copied", in the "assetsName" put it like that, but preceding the folder you need it. like "./node_modules/[name]" and the loader is for handling the files as what they are, we need them to be copied
k
i guess your solution is doing something similar except injecting it after?
m
yes, correct
because for locally i need them, but for the deploy no.
k
really?
Ive not deployed my application yet so im not sure whats going to happen
1
m
if you include them in the deployment the lamba size will be huge
k
right they are 80mb total
m
huge
k
So are you telling me if i inject in the custom.esbuild they dont show up in deployment?
if thats the case how does it work in deployment if you need these locally?
m
no i didn't told you that, since we are in a .ts file I added a conditional with 2 esbuild custom configs, one with the inject for local, and the other one "clean", for aws deploy
also I build a lambda layer which haves the files needed for prisma to work
k
If i need to include these "engines" in the output build if i want to run
sls invoke local -f main
when this is deployed why does it not require thsese engines?
m
so many lambdas are using that layer
k
oh~ thats just sounds like a pain 😕 still not quite understanding why it would not need them when its deployed
m
sorry I don't know the answer to your last question. If sls invoke uses the aws lambda then you are including them in the final package, right?
k
right
m
so that's why they are not need while they are deployed
k
?
m
they are included, and that's why your lambda is 80mb, right?
k
No~ i have not deployed this yet
m
ahh
k
You said you have 2 builds, one for local and 1 for production. You are saying your production build does not include these "engines"
If you need these engines locally to run, im asking why would you not need them in production deployment? sls invoke local -f vs sls deploy
ultimately why would i go through the hastle of creaitng seperate local/prod builds with layers ( i dont know what the heck this stuff is) when my asssumption right now is that these binary targets need to deployed in production? https://www.prisma.io/docs/guides/deployment/deployment-guides/deploying-to-aws-lambda#package-pattern-in-serverlessyml
m
oh, yes, you/we need them. I included them in a lambda layer!
k
right
m
you can still have them in the deployment build, but the lambda size is huge! like you point
k
Then im fine with just this, hopefully
Copy code
package: {
        individually: true,
        patterns: [
            "!node_modules/.prisma/client/libquery_engine-*",
            "node_modules/.prisma/client/schema.prisma",
            "node_modules/.prisma/client/libquery_engine-debian-*",
            "!node_modules/prisma/libquery_engine-*",
            "!node_modules/@prisma/engines/**",
        ],
    },
m
yeah, maybe
k
You can't get around the size of it
You need one of these binary targets
i dont think there is a way to avoid that, the whole purpose of the patterns from my understanding is that it strips away everything else in the prisma node_modules only containing the binary target required
m
I already avoided it. And my lambdas weight less than 2MB
k
really?
can i see your entire serverless.ts?
m
But I did the trick after a lot of researching, fail & trial, and finally with lambda layers SHARING these binaries
k
can you point me to some documentation you researched
I hope this links helps you
k
thanks i will read these
hopefully i will better understand what youre trying to explian
m
sure no worries
take a look at those links.. It's my first time dealing with all of these so maybe I'm explaining myself badly, i dont know haha
k
@Marcos Iglesias your final deployment on aws for this lambda function is how many mb?
m
less than 2MB
others also smallers, and I'm trying to build a layer with the node_modules so I can decrease that more
k
are you sure your other layer is not keeping the size of the binary?
Copy code
Fantastic! Our function's artifact size is down to ~4.3mb! But look at the size of that layer! 💥 We just moved the bulk from one place to another...

A layer has the same size constraints as a full function does, so this deployment is going to fail.
so i woner if in your case, 1 layer has 2 mb but the other layer has more?
m
yes, one layer is bigger than the other
k
how big is that layer?
m
can't tell rn, apologies, but I think is something like 20MB
k
okay
mine turned out to be 16mb
but i found when deployed to aws, it is looking for a different engine compared to what i have locally rhel-openssl-1.0.x\ (guessing becuase the OS is different)
i could include both or just write some condition to say that only include this pattern if..
m
most of that 16mb should be from the lib engine file...
k
yep
m
and if you include another one... i dont want to imagine lol
k
i think those layers was wayu to much of a complicated solution, compared to what i needed
I only need 1 binary target
k
if you use layers , each layer still need to conform to the size limits of a lambda function
m
for how many lambdas?
k
I have a single lambda function, no layers
layers is good like the docs mention if you have "multiple" prisma clients
m
in that case... because that guys was separatin every model with 1 client...
k
I ony ever have 1 schema file and 1 prisma client
m
but the other article took a different approach
then you are good to continue dev
more than good