dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.19k stars 796 forks source link

Not able to connect to Posgres DB behind a VPN #1679

Open imewish opened 1 year ago

imewish commented 1 year ago

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


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
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