Camilo Ortegón
Camilo Ortegón

Reputation: 3692

Using Firebase from AWS Lambda results in Task timed out

I'm using a Lambda function to create users with Firebase Authentication and then save them at my own database.

var firebase = require('firebase-admin')
const serviceAccount = require('./firebase.json')
firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccount),
    databaseURL: 'https://*****.firebaseio.com'
})

exports.handler = function(event, context, callback) {

    const {email, password, name, ...} = event

    firebase.auth().createUser({
        email,
        emailVerified: false,
        password,
        displayName: name,
        disabled: false
    })
    .then(firebaseResult => {
        const {uid} = firebaseResult
        return saveUserAtDatabase({email, name, ...})
    })
    .then(result => {
        callback(null, result)
    })
}

The user is created at Firebase and at my database as well, but when I run it at AWS Lambda, it throws this error:

{"errorMessage":"2019-01-07T21:25:49.095Z c...e9 Task timed out after 6.01 seconds"}

Doesn't matter how much time I increase the timeout from the function or set higher memory, it still throws the same error.

Upvotes: 0

Views: 528

Answers (1)

Camilo Ortegón
Camilo Ortegón

Reputation: 3692

I solved the problem setting context.callbackWaitsForEmptyEventLoop=false.

This is because callback waits for the event loop to be empty, which doesn't happen using firebase.auth().createUser(). There is the context.callbackWaitsForEmptyEventLoop option documented here http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html

When this property is set to false, Lambda freezes the container but does not clear the event loop when you call the callback.

Upvotes: 3

Related Questions