question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

new PrismaClient() takes 3 seconds to connect to Db on AWS lambda

See original GitHub issue

Bug description

As this docs suggest I create new PrismaClient outside handler

But Very first request takes 3 seconds to create prisma client. Then later requests use created prisma client. very fast.

This time does not include cold start time. as picture shows lambda logs are after lambda started.

Only difference and 3seconds execution in 1st request is url: postgresql://... line which is new PrismaClient code.

Screen Shot 2022-06-21 at 14 42 26

getPrismaClient.ts

import { PrismaClient } from "@prisma/client";
import { SecretsManager } from 'aws-sdk'

const sm = new SecretsManager()
let prisma: PrismaClient
let url: string

export const getPrismaClient = async () => {
  if (prisma) return prisma

  const dbURL = {.....}
  const secretString = JSON.parse(dbURL.SecretString || '{}')
  url = `postgresql://${secretString.username}:${secretString.password}@${secretString.host}:${secretString.port}/${secretString.dbname}?connection_limit=1`
  console.log('url: ', url);
  prisma = new PrismaClient({
    datasources: {db:{url}}
  });
  return prisma
}

How to reproduce

deploy simple todo app to aws lambda

Expected behavior

No response

Prisma information

Click to expand! ```prisma 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”) }

enum m_DoorOpenPermissionType { SHORT_OPEN // users allowed to user short open action LONG_OPEN // users allowed to use long open action }

model u_DoorOpenPermission { createdAt DateTime @default(now()) updatedAt DateTime @default(now()) cognitoUsername String // cognito company id doorId Int startTime DateTime @default(now()) // rule apply start time endTime DateTime? // rule end time type m_DoorOpenPermissionType

door m_Door @relation(fields: [doorId], references: [id]) @@id([doorId, cognitoUsername]) }

model m_Door { id Int @id @default(autoincrement()) createdAt DateTime @default(now()) updatedAt DateTime @default(now()) name String allowedUsers u_DoorOpenPermission[] imageUrl String isOpenDaily Boolean isHidden Boolean @default(false) }


</details>

### Environment & setup

- OS: Mac
- Database: Postgres
- Node.js version: 16


### Prisma Version

3.14.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
matart15commented, Jul 4, 2022

thank you @cayter. Looks like prisma data proxy is only solution.

😦

1reaction
caytercommented, Jul 4, 2022

@matart15 Sorry for my incorrect information. As documented here, Prisma client pooling doesn’t work with AWS RDS proxy.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection management (Guide) - Prisma
How to approach connection management with Prisma Client in serverless environments and long-running ... Three AWS Lambda function connecting to a database.
Read more >
Troubleshoot timeouts from Lambda to an Amazon RDS DB ...
My AWS Lambda function receives connection timeout errors when it tries to access an Amazon Relational Database Service (Amazon RDS) DB ...
Read more >
Configure a Lambda function to connect to an RDS instance
I want my AWS Lambda function to connect to an Amazon Relational Database Service (Amazon RDS) instance. How can I configure the network ......
Read more >
Troubleshoot Lambda function retry and timeout issues when ...
AWS SDK, Maximum retry count, Connection timeout, Socket timeout. Python (Boto 3), depends on service, 60 seconds, 60 seconds.
Read more >
Troubleshooting Lambda configurations - AWS Documentation
You can configure a Lambda function to use between 128 MB and 10240 MB. ... upper bound after which the processing of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found