@cosva-lab/serverless-esbuild-prisma is a Serverless plugin designed to integrate Prisma with the esbuild bundler, ensuring that the Prisma schema and engine files are correctly packaged within your deployment artifacts.
- Automatically embeds the Prisma schema and engine files into your Serverless deployment package.
- NEW: Support for AWS Lambda Layers to reduce package sizes and improve deployment speed.
- Supports multiple engines and runtime environments.
- Seamlessly integrates with the Serverless framework and esbuild bundler.
- Smart change detection for layers - only uploads when content changes.
Install the package via npm:
npm install @cosva-lab/serverless-esbuild-prisma --save-devAdd the plugin to your serverless.yml file:
plugins:
- '@cosva-lab/serverless-esbuild-prisma'You can customize the plugin's behavior by adding a custom block in your serverless.yml:
custom:
prisma:
prismaPath: ./path/to/your/prisma/schema # Optional: Specify the path to your Prisma schema
ignoreFunctions: # Optional: Specify functions to ignore
- functionName1
- functionName2
# NEW: Layer configuration
useLayer: true # Enable AWS Lambda Layers for Prisma engines
layerName: "my-service-dev-prisma-layer" # Optional: custom layer name (includes stage)
layerDescription: "Prisma engines for my service" # Optional: custom description
# NEW: Logging configuration
logging: "DEBUG" # ERROR, WARN, INFO, SUCCESS, DEBUG
debug: true # Enable debug logging (legacy)
esbuild:
outputDir: ./path/to/output/dir # Optional: Specify the output directory for esbuild| Option | Type | Default | Description |
|---|---|---|---|
useLayer |
boolean | false |
Enable/disable layer usage |
layerName |
string | {service}-{stage}-prisma-layer |
Custom layer name (includes stage) |
layerDescription |
string | Prisma engines layer for serverless functions |
Layer description |
| Option | Type | Default | Description |
|---|---|---|---|
logging |
string | INFO |
Log level: ERROR, WARN, INFO, SUCCESS, DEBUG |
debug |
boolean | false |
Enable debug logging (legacy) |
| Option | Type | Default | Description |
|---|---|---|---|
engines |
object | {} |
Manual engine configuration (overrides auto-detection) |
You can manually specify engine names for specific engines, while others are auto-detected:
custom:
prisma:
useLayer: true
engines:
queryEngineLibrary: "libquery_engine-rhel-openssl-3.0.x.so.node"
# migrationEngine, introspectionEngine, prismaFmt will be auto-detectedHybrid Configuration: The plugin auto-detects all engines by default, but you can override specific ones:
- ✅ Auto-detected: Engines not specified in config
- ✅ User-configured: Engines specified in config override auto-detection
- ✅ Partial config: Only specify the engines you want to customize
This plugin supports using AWS Lambda Layers for Prisma engines, which can significantly reduce deployment package sizes and improve deployment speed.
When useLayer: true is set:
- Schema Only: Only the Prisma schema is included in function packages (not engines)
- Layer Zip Generation: Creates the layer zip file with Prisma engines
- No AWS Calls: No network operations are performed during packaging
- Layer Zip Generation: Creates the layer zip file with Prisma engines
- Layer Upload: Uses the generated layer zip to create/update the Lambda layer
- Change Detection: It calculates a hash of the layer content to detect changes
- Smart Updates: Only uploads a new layer version when content has actually changed
- Function Updates: Automatically updates all functions to use the layer
- ✅ Only includes Prisma schema in function packages
- ✅ Generates layer zip file with Prisma engines
- ✅ No AWS API calls
- ✅ Fast and offline operation
- ✅ Perfect for CI/CD pipelines
- ✅ Generates layer zip file with Prisma engines
- ✅ Creates/updates Lambda layer using the generated zip
- ✅ Detects changes and only uploads when needed
- ✅ Updates function configurations to use layer
- ✅ Handles AWS operations
- ✅ Works independently (no need to run package first)
- Smaller Packages: Function packages are much smaller without Prisma engines
- Faster Deployments: Engines are cached in the layer, reducing upload time
- Shared Resources: Multiple functions can use the same layer
- Version Control: Layer versions are managed automatically
- CI/CD Friendly: Package phase is fast and doesn't require AWS credentials
The layer zip file is generated in ./.serverless/prisma-layer.zip and contains:
nodejs/directory with all Prisma enginesnodejs/schema.prisma(your Prisma schema)
- Layer Zip:
./.serverless/prisma-layer.zip(generated during package) - Function Packages:
./.serverless/{function-name}.zip(with schema only when using layers)
The plugin automatically:
- Creates the layer if it doesn't exist
- Detects content changes using SHA256 hashing
- Only uploads new versions when content changes
- Updates function configurations to use the latest layer version
- Cleans up temporary files
If you encounter issues:
- Check AWS permissions: Ensure your AWS credentials have Lambda layer permissions
- Verify region: Make sure the layer is created in the same region as your functions
- Check logs: The plugin provides detailed logging of layer operations
- Manual cleanup: If needed, you can manually delete old layer versions in the AWS console
To migrate from the default mode to layer mode:
- Add
useLayer: trueto your configuration - Deploy your service - the plugin will create the layer automatically
- Subsequent deployments will use the layer
No code changes are required in your application code.
Here’s a basic example of how to use this plugin:
service: my-service
provider:
name: aws
runtime: nodejs14.x
plugins:
- '@cosva-lab/serverless-esbuild-prisma'
custom:
prisma:
prismaPath: ./prisma/schema.prisma
ignoreFunctions:
- anotherFunction
esbuild:
outputDir: ./build
functions:
hello:
handler: handler.hello
anotherFunction:
handler: handler.anotherTo contribute to this plugin:
- Clone the repository.
- Install dependencies:
npm install - Make your changes and test them.
- Submit a pull request.