2021年5月4日星期二

Node.js 12.x AWS lambda does not return using firebase-admin realtime database

I've been stuck on this issue for days with little to no progress, please help if you can!

I have a Node.js (v12) AWS Lambda that needs to pull data from my Firebase realtime database and process each record into a Redis cache if it doesn't exist already. The function starts but never finishes and instead I receive Task timed out after 180.10 seconds from AWS.

Things I've tried:

  • Using exports.handler = async function(event) versus exports.handler = function(event, context, callback);
  • For the synchronous attempt above I've tried using context.callbackWaitsForEmptyEventLoop = false versus not;
  • Using promises versus cascading functions versus stitching a bunch of .then()'s together;
  • Using the firebase-admin versus the https module in-conjunction with the Firebase REST API;
  • Using settimeout to fire the callback later versus not;
  • Setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to my service account credentials versus referencing the file directly in the code;
  • I've even beefed-up the memory and timeout of the Lambda itself to the maximum it can go, as well as cut down the data I want to pull from Firebase to just 1 record.

Responses I've had as-per the attempts above:

  • AWS (the most frequent): Task timed out after 180.10 seconds;
  • AWS (.then stitching approach): Function completed successfully (but no data was actually processed);
  • Node HTTPS (REST API approach): ETIMEDOUT or ECONNREFUSED;

Below is where I'm up to and still no luck. I have cut-out the caching code since I know that works fine. The settimeout's you see were my last resorts before reaching out here.

const admin = require("firebase-admin");  admin.initializeApp({      credential: admin.credential.applicationDefault(),      databaseURL: "https://{projectName}.firebaseio.com"  });  var result = [];  exports.handler = (event, context, callback) => {      context.callbackWaitsForEmptyEventLoop = false;      try {          admin.database().ref("data").orderByChild("timestamp").limitToLast(1).once("value", snapshot => {              if (snapshot.exists()) {                  console.log('snapshot exists...');                  let posts = snapshot.val();                  result = Object.keys(posts);              }              setTimeout(() => {                  admin.database().goOffline();                  admin.app().delete();                  callback(null, `Success! ${JSON.stringify(result)}`); // <-- NEVER RETURNS              }, 2000);          }, error => {               setTimeout(() => {                  admin.database().goOffline();                  admin.app().delete();                  callback(error); // <-- NEVER RETURNS              }, 2000);          });      } catch (error) {          setTimeout(() => {              admin.database().goOffline();              admin.app().delete();              callback(error); // <-- NEVER RETURNS          }, 2000);      }  };  
https://stackoverflow.com/questions/67350598/node-js-12-x-aws-lambda-does-not-return-using-firebase-admin-realtime-database May 02, 2021 at 04:31AM

没有评论:

发表评论