简体   繁体   中英

Lambda function timing out after 10 seconds

Code:

const knex = require('knex')({
    client: 'mysql',
    connection: {
        host: process.env.database_host,
        user: process.env.database_user,
        password: process.env.database_pass,
        database: process.env.database_db,
        charset: 'utf8'
    }
});
const bcrypt = require('bcrypt');
const bookshelf = require('bookshelf')(knex);
const User = bookshelf.Model.extend({
    tableName: 'users'
});

const checkValues = (values) => {
    // todo: add data validation
    return true;
};

exports.test = (database) => {
    // todo: add tests
};

exports.handler = (event, context, callback) => {
    let salt = bcrypt.genSaltSync();

    let values = {
        first_name: event.firstname,
        last_name: event.lastname,
        username: event.username,
        date_of_birth: event.birthday,
        password: bcrypt.hashSync(event.password, salt),
        password_salt: salt
    };

    if (!checkValues(values)) {
        callback(null, {
            success: false,
            error: {
                id: 2,
                details: 'data validation error'
            }
        });

        context.done(null, "User not created");

        return;
    }

    try {
        new User({
            'first_name': values.first_name,
            'last_name': values.last_name,
            'username': values.username,
            'date_of_birth': values.date_of_birth,
            'password': values.password,
            'password_salt': values.password_salt
        }).save();

        callback(null, {
            success: true
        });

        context.done(null, "User created");
    } catch (err) {
        console.log(err);

        callback(null, {
            success: false,
            error: {
                id: 1,
                details: 'error inserting user into database'
            }
        });

        context.done(null, "User not created");
    }
};

I am trying to make a basic sign up api endpoint using AWS API Gateway and Lambda functions, however every time I post the information to the api gateway I get the error

{
    "errorMessage": "2017-09-07T08:38:50.174Z f2368466-93a7-11e7-b4bc-01142a109ede Task timed out after 10.00 seconds"
}

I have tried using different database libraries but I seem to always be hitting the same problem. The database connection works I know this because the user does infact get added to the users table in the database and the password is successfully hashed..

I have also tried using asynchronous bcrypt but it doesn't make any difference to the result, it still does it but says it times out.

Lambda doesn't seem to be terminating properly, something keeps the process still running and I can't figure out what, any ideas?

i had the similar issue using API gateway invoking my lambda.

The default timeout for API gateway is 30 seconds. If your response is not ready within in 30 seconds, you will be timed out though your lambda would still run!

So may be try to get the response back within 30 seconds. If not have one lambda being invoked from the API and give the response back immediately and let the first lambda invoke your second lambda and that will run upto max time which is 5 mins.

Thanks

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