dherault / dherault/serverless-offline
Not able to connect to Posgres DB behind a VPN
- Dominant language
- JavaScript
- Stars
- 5.3k
- Forks
- 811
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 3
Description
## Bug Report
I have lambda function which talks to an RDS Postgress instance. If I connect to VPN and run `sls invoke local function -f fnName` the function is able to establish the connection and execute successfully.
But when I'm running with `sls offline` the function fails at the DB connection part with the message `TypeError: Native is not a constructor`. I get the same message even though I'm not connected to the VPN as well.
been struck with this issue for the last couple of days. No clue how to solve this.
**Sample Code**
- file: serverless.yml
```yaml
service: my-service
frameworkVersion: '3'
useDotenv: true
provider:
name: aws
runtime: nodejs14.x
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'us-east-1'}
versionFunctions: false
logRetentionInDays: ${param:logRetentionInDays}
deploymentBucket:
blockPublicAccess: true
skipPolicySetup: false
name: 'serverless-artifacts-${aws:accountId}'
tags:
service: ${self:service}
stage: ${self:provider.stage}
iam:
role:
statements:
- Effect: 'Allow'
Action:
- 'secretsmanager:*'
- 'execute-api:Invoke'
- 'rds-db:connect'
Resource: '*'
package:
individually: true
include:
- '!**'
custom:
splitStacks:
nestedStackCount: 25
perFunction: false
perType: false
perGroupFunction: true
alerts: ${file(./sls.alerts.yml)}
esbuild:
exclude:
- aws-sdk
bundle: true
minify: false
packager: yarn
concurrency: 1
params:
prod: ${file(./sls.params.prod.yml)}
dev: ${file(./sls.params.dev.yml)}
functions:
- ${file(app/handlers/app/sls.yml)}
plugins:
- serverless-plugin-aws-alerts
- serverless-plugin-split-stacks
- serverless-esbuild
- serverless-lumigo
- serverless-offline
```
- file: handler.js
```js
DB connection code
import { DataSource, DataSourceOptions } from 'typeorm';
console.log('RDS connection not established, connecting');
const dbOptions: DataSourceOptions = {
type: 'postgres',
host: dbInfo.DB_PROXY_HOST,
port: dbInfo.DB_PORT,
username: dbInfo.DB_USER,
password: dbInfo.DB_PASS,
database: dbInfo.DB_DATASBASE,
logging: false,
synchronize: false,
extra: {
ssl: 'true',
},
...params.dbOptions,
};
const db = new DataSource(dbOptions);
console.log(dbOptions);
instance = await db.initialize();
```
**Environment**
- `serverless` version: [e.g. v3.30.0]
- `serverless-offline` version: [e.g. v12.0.4]
- `node.js` version: [e.g. v14.8.0]
- `OS`: [e.g. macOS 12.4]
Contributor guide
Research direction
Start with the supplied serverless.yml and handler.js, reproducing both `sls invoke local function -f fnName` and `sls offline`. Compare the failing `new DataSource(dbOptions)` path and the `TypeError: Native is not a constructor` output, including the stated Serverless, serverless-offline, and Node.js versions. Done means the handler can establish its Postgres connection under `sls offline` as it does during local invocation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, javascript, node.js, postgres
- Domain
- backend, databases, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100