AWS Node 2 Lambda which shares the db postgres code not returning value for the second lambda?

60 views Asked by At

Am having the following project structure for my Node Ts Lambda

project-root/ │ ├── src/ │ ├── lambda1/ │ │ ├── index.ts
│ │ ├── Service.ts
│ │ ├── package.json.
│ │ └── tsconfig.json
│ │ │ ├── lambda2/ │ │ ├── index.ts
│ │ ├── Service.json
│ │ ├── package.json
│ │ └── tsconfig.json │ │ │ └── shared/ │ ├── dbconnect.ts
│ ├── constants.ts
│ └──
│ ├── tsconfig.json
├── package.json

in my shared dbconnect.ts having the db connection code and a method to execute function using pgpromise packages.

Actual Issue after deploying the 2 lambdas first lambda can successfully return data, but second lambda neither returning any value nor throwing any errors below is the logs for the second lambda and highlighted the console.log info in BOLD

INIT_START Runtime Version: nodejs:20.v15 Runtime Version ARN: arn:aws:lambda:us-east-2::runtime:0856f5d760b75612d4aa6b5feda50bafc1ce3ba07c036cbd0abc585dc28fb870 START RequestId: 85af3fa6-2044-4664-9d67-9890be0a088a Version: $LATEST 2024-02-16T10:59:14.484Z 85af3fa6-2044-4664-9d67-9890be0a088a INFO Inside GetLegalDashBoardData 2024-02-16T10:59:14.523Z 85af3fa6-2044-4664-9d67-9890be0a088a INFO Inside Service connectionString 2024-02-16T10:59:14.524Z 85af3fa6-2044-4664-9d67-9890be0a088a INFO Inside ConnectToDatabase 2024-02-16T10:59:14.726Z 85af3fa6-2044-4664-9d67-9890be0a088a INFO Inside ConnectToDatabase before return 2024-02-16T10:59:14.726Z 85af3fa6-2044-4664-9d67-9890be0a088a INFO After ConnectToDatabase END RequestId: 85af3fa6-2044-4664-9d67-9890be0a088a REPORT RequestId: 85af3fa6-2044-4664-9d67-9890be0a088a Duration: 525.35 ms Billed Duration: 526 ms Memory Size: 128 MB Max Memory Used: 76 MB Init Duration: 191.40 ms

Here is my DBConnection.ts Code what I have Tried

    import { IDatabase, IMain } from 'pg-promise';
    import pgpromise from 'pg-promise';

    async function ConnectToDatabase(user: string, password: string, host: string, port: number, database: string): Promise<IDatabase<any>> {
    try {
        console.log("Inside ConnectToDatabase");
        const pgp = pgpromise();
        const connectionOptions = {
            host: host,
            port: port,
            database: database,
            user: user,
            password: password,
        };

        const dbClient = pgp(connectionOptions)
        console.log("Inside ConnectToDatabase before return");
        return dbClient;
    } catch (error) {
        console.error('Error connecting to the database:', error);
        throw error;
    }
}

    export async function ExecuteFunction(query: string, connectionParams: any) {
    try {
        const dbCreds = JSON.parse(connectionParams);
        const plsqlQuery: any = JSON.parse(query)
        const dbInstance = await ConnectToDatabase(dbCreds.user, dbCreds.password, dbCreds.host, dbCreds.port, dbCreds.database);
        console.log(`After ConnectToDatabase`)
        const result = dbInstance.func(plsqlQuery.funcName, plsqlQuery.params);
        return result;
    } catch (error) {
        console.error('ExecuteFunction Error in Fetching Records From the database:', error);
        throw error;
    }
    }

Service.ts file where am calling the dbconnect.ts method to connect to db and execute function and return the result

import { config } from '../../../shared/config/whub_config';
import { ExecuteFunction } from '../../../shared/data_access/whub_data_service';
export async function GetLegalDashBoardData() {
    console.log("Inside GetLegalDashBoardData")
    try {

        const dbCred: any = GetHubDBCredentials();
        const query: any = {
            funcName: `${config.WHUB_LEGAL_SCHEMA}.${config.LEGAL_GET_DASHBOARD_DATA}`,
            params:"test"
        }
        console.log(`Inside Service connectionString`)
        const result = await ExecuteFunction(JSON.stringify(query),JSON.stringify(dbCred));
         return result;
    } catch (error) {
        console.error('Error executing query', error);
        console.log('Error executing query', JSON.stringify(error));
    }
}

Sorry if it's a duplicate question, please help me to resolve this issue, thanks in advance

1

There are 1 answers

4
andyb On

Since func returns a Promise, shouldn't you be using await when you call it?