Title
c

Cam Sloan

01/14/2022, 7:40 PM
Hey there - currently trying to run sst start locally. I have set up an Api resource pointing to a lambda, and in that lambda I am trying to use the
pg
npm package to make the connection. I have also tried using
serverless-postgres
with the same results. I am getting an error about not able to resolve pg-native.
> node_modules/pg/lib/native/client.js:4:21: error: Could not resolve "pg-native" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
the code implementation looks like:
const { Pool } = require('pg')
// import ServerlessClient from 'serverless-postgres'

const pool = new Pool({
  user: process.env.DB_USER,
  host: process.env.DB_HOST,
  database: process.env.DB_NAME,
  password: process.env.DB_PASSWORD,
  port: Number(process.env.DB_PORT),
  debug: true,
  delayMs: 3000,
})
Any help would be greatly appreciated!
Ok I seem to have resolved this (at least locally) after some searching around.
const api = new Api(this, 'TriggerVideoMigrationsApi', {
      defaultFunctionProps: {
        bundle: {
          externalModules: ['pg-native'],
        },
      },
      routes: {
        // ...
      },
    })
f

Frank

01/14/2022, 10:44 PM
Hey @Cam Sloan, yeah that’s the way to do it! Glad you figured it out.
w

William Hatch

01/24/2022, 8:17 PM
@bike-bill check this out. same problem we're seeing now
f

Frank

01/24/2022, 8:39 PM
@William Hatch @bike-bill lemme know if Cam’s solution works for you guys.
w

William Hatch

01/28/2022, 6:27 PM
@Frank yes, that worked for us. Sorry for the delay in responding. We're now onto solving atlas/mongo connection issues, which we feel are more related to lambda and atlas in general.
f

Frank

01/30/2022, 4:29 AM
@William Hatch have you checked out their new serverless offering? Here’s an example https://serverless-stack.com/examples/how-to-use-mongodb-atlas-in-your-serverless-app.html
b

bike-bill

01/30/2022, 10:19 AM
That looks good @Frank. Thanks, we'll try it out!
w

William Hatch

02/03/2022, 7:17 PM
revisiting this due to current issues... No, we''re not using the serverless variety of atlas, as it didn't seem appropriate for our use case, after reading 'not meant for higher volume...' So, we're using a standard cluster configuration. We are using that guide for managing connections and caching, however. But, we're having a heck of a time, and it's not at all reliable at the moment.