Connection hangs in AWS Lambda when using response streaming

I know that this issue is not really drizzle related but there might be someone who can help me out I have a lambda function that I want to use to stream responses. If I create a DB connection to make some queries within the lambda, after the streaming ends, the lambda just hangs and eventually times out. I assume that it has to do with the fact that there is an open db connection, which I don't want to close because then I can not reuse the same db connection for following invocations. this is my code
import { APIGatewayProxyEventV2 } from "aws-lambda";
import { userTable } from "core/database/schema";
import { eq } from "drizzle-orm";
import { drizzle } from "drizzle-orm/postgres-js";
import { ResponseStream, streamifyResponse } from "lambda-stream";
import { pipeline } from "node:stream/promises";
import postgres from "postgres";
import { Readable } from "stream";
import { config } from "config";
import * as schema from "../../core/database/schema";

const connectionString = 'postgres.....'
const queryClient = postgres(connectionString, { max: 1 });
const db = drizzle(queryClient, { schema });

export const lambdaHandler = async (event: APIGatewayProxyEventV2, responseStream: ResponseStream) => {
await db.select().from(userTable).where(eq(userTable.id, "test")).limit(1);

const requestStream = Readable.from(Buffer.from(JSON.stringify({ message: "Hello, world!" })));
await pipeline(requestStream as NodeJS.ReadableStream, responseStream);
};

export const handler = streamifyResponse(lambdaHandler);
import { APIGatewayProxyEventV2 } from "aws-lambda";
import { userTable } from "core/database/schema";
import { eq } from "drizzle-orm";
import { drizzle } from "drizzle-orm/postgres-js";
import { ResponseStream, streamifyResponse } from "lambda-stream";
import { pipeline } from "node:stream/promises";
import postgres from "postgres";
import { Readable } from "stream";
import { config } from "config";
import * as schema from "../../core/database/schema";

const connectionString = 'postgres.....'
const queryClient = postgres(connectionString, { max: 1 });
const db = drizzle(queryClient, { schema });

export const lambdaHandler = async (event: APIGatewayProxyEventV2, responseStream: ResponseStream) => {
await db.select().from(userTable).where(eq(userTable.id, "test")).limit(1);

const requestStream = Readable.from(Buffer.from(JSON.stringify({ message: "Hello, world!" })));
await pipeline(requestStream as NodeJS.ReadableStream, responseStream);
};

export const handler = streamifyResponse(lambdaHandler);
if i comment out await db.select().from(userTable).where(eq(userTable.id, "test")).limit(1); then it works fine I attached two picture of my logs, which shows that the lambda times out when doing a db query, the other one shows that the invocations stops as it should when not doing the db query
No description
No description
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server