This project demonstrates how to use Prisma as a layer in AWS Lambda with AWS Serverless Application Model (SAM). By leveraging AWS Lambda Layers, we can optimize function deployment, reduce package size, and ensure a consistent Prisma ORM setup across multiple functions.
- AWS Account
- AWS CLI configured
- AWS SAM installed
- Node.js 18+ installed (recommended: use
.nvmrc
with18
) - Yarn (preferred package manager)
| prisma-lambda-layer
|- layers/ # Contains the Prisma layer
|- scripts/ # Deployment and setup scripts
|- src/ # Lambda function handlers
|-- handlers/
|-- services/
Add the necessary dependencies:
yarn add @prisma/client aws-lambda
yarn add --dev @types/aws-lambda @types/node prisma typescript
Modify prisma/schema.prisma
to support AWS Lambda:
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "rhel-openssl-1.0.x", "linux-arm64-openssl-1.0.x"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
Execute the setup script to create the Prisma layer:
sh scripts/prisma-layer.sh
Run the following command to deploy:
sh scripts/deploy.sh
This will:
- Install dependencies.
- Generate the Prisma client.
- Build the SAM project.
- Deploy the stack to AWS.
Once deployed, test the API using Postman or cURL.
curl -X POST "<API_URL>/v1/countries" \
-H "Content-Type: application/json" \
-d '{"name": "Japan"}'
curl -X GET "<API_URL>/v1/countries"
curl -X GET "<API_URL>/v1/countries/1"
To remove all AWS resources, run:
sam delete --stack-name sample-api --profile codeanding
- Explore Terraform as an alternative to AWS SAM.
- Implement API Gateway authentication.
- Optimize cold start performance by tuning Lambda configurations.
This project is licensed under the MIT License.
🚀 Happy coding!
If you find this useful, feel free to contribute or reach out for improvements.