简体   繁体   中英

Node Lambda Function can't query an AWS Aurora MySQL database

I'm trying to write to an aws aurora mysql database with a Lambda Function.

Code

For some reason, it doesn't seem to work. Until this point, my code is very simple but fails to execute correctly. It is a simple lambda function, that is called when a user signs up to cognito and should just create a new entry in the SQL db with user credentials.

const mysql = require('mysql')

const db = mysql.createConnection({
    host: "*****",
    port: *****,
    user: "*****",
    password: "*****",
    database: "*****",
})

export const lambdaHandler = async (event: any, context: any,) => {
    const user = {
        uid: event.request.userAttributes.sub,
        username: event.request.userAttributes.nickname,
        email: event.request.userAttributes.email,
    }

    console.log("before query")

    const query = `INSERT INTO users (Uid, Username, Email) VALUES (${user.uid}, ${user.username}, ${user.email})`
    await db.query(query, (err: any, result: any) => {
        if (err) console.error(err)

        console.log("query response: " + result)
    })

    console.log("after query")

    return event;
};

The output of the function is the following:

START RequestId: bb1d2143-97e4-4761-a48a-f482cf94f73b Version: $LATEST
2022-03-17T14:02:35.956Z        bb1d2143-97e4-4761-a48a-f482cf94f73b    INFO    before query
2022-03-17T14:02:35.961Z        bb1d2143-97e4-4761-a48a-f482cf94f73b    INFO    after query
END RequestId: bb1d2143-97e4-4761-a48a-f482cf94f73b
REPORT RequestId: bb1d2143-97e4-4761-a48a-f482cf94f73b  Init Duration: 0.14 ms  Duration: 138.07 ms     Billed Duration: 139 ms Memory Size: 128 MB     Max Memory Used: 128 MB

Note: there is no log to be found by the query function. (should either throw error or log response)

Policies

The policies that are in the execution Role of the lambda function are: AWSLambdaVPCAccessExecutionRole and AWSLambda_FullAccess

Other

The Lambda function and Aurora are in different su.nets of the same vpc. I am not sure if this could result in a problem. But wouldn't that lead to an error thrown by the function? Something like missing authorization ?

I don't understand what else I'm missing at this point

The issue is that db.query uses the callback-style instead of promise-style. You have to convert it to use promises.

One way of doing it is using Util.promisify ...

const util = require('util');
const mysql = require('mysql');

const db = mysql.createConnection({
    host: "*****",
    port: *****,
    user: "*****",
    password: "*****",
    database: "*****",
});

// Convert db.query into a function that returns a promise
const promisifiedQuery = util.promisify(db.query);

export const lambdaHandler = async (event: any, context: any,) => {
    const user = {
        uid: event.request.userAttributes.sub,
        username: event.request.userAttributes.nickname,
        email: event.request.userAttributes.email,
    };

    console.log("before query");

    // BTW, this is bad. Read about how to avoid SQL injections.
    const query = `INSERT INTO users (Uid, Username, Email) VALUES (${user.uid}, ${user.username}, ${user.email})`;

    await promisifiedQuery(query).then(result => {
        console.log("query response: " + result)
        console.log("after query")
    }).catch(console.error)

    return event;
};

You have to call db.connect() before doing the query and db.end() after you did the query. See https://www.npmjs.com/package/mysql#introduction . I suspect if you don't it just ignores the await db.query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM