Skip to content

Instantly share code, notes, and snippets.

@tinovyatkin
Forked from streamich/lambda.js
Last active July 27, 2023 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tinovyatkin/e865c9afa4c601f00eb5fc61a7309c45 to your computer and use it in GitHub Desktop.
Save tinovyatkin/e865c9afa4c601f00eb5fc61a7309c45 to your computer and use it in GitHub Desktop.
using node-postgres (`pg`) in AWS Lambda
import type { Handler } from "aws-lambda";
import { Pool } from "pg";
// connection details inherited from environment
const pool = new Pool({
max: 1,
min: 0,
idleTimeoutMillis: 120000,
connectionTimeoutMillis: 10000
});
export const handler: Handler = async (event, context) => {
// https://github.com/brianc/node-postgres/issues/930#issuecomment-230362178
context.callbackWaitsForEmptyEventLoop = false; // !important to reuse pool
const client = await pool.connect();
try {
const result = await client.query("SELECT NOW()");
return result;
} finally {
// https://github.com/brianc/node-postgres/issues/1180#issuecomment-270589769
client.release(true);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment