"Warning, FIREBASE_CONFIG and GCLOUD PROJECT environment variables are missing. Initializing firebase-admin will fail"
02:56 11 Jan 2023

I have a google cloud function that will send data from iot machine to Firebase Realtime database using MQTT broker. I was running this same code on virtual machine(VM) in gcp everything was working fine, I was getting data in Realtime database.

I also tried this code on my local system, and everything is working fine. But when I tried to run the same code on cloud function, I was getting some errors; I have fixed some of them, but I was not able to fix this one:

Function cannot be initialized. Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging

I am adding code and some images below for better understanding.

In this screenshot, I show the content or thr files which is present in cloud function:

enter image description here

In this screenshot, I show the .json file this json file is private key file which I have generated in json and give the same file name in ServiceAcoount

const serviceAccount = require("./****-***-firebase-adminsdk-*****-******ca66.json");

and .runtimeconfig.json also contains the private key file.

enter image description here

I have not added anything in variable.

enter image description here

This my code:

const Admin = require("firebase-admin");
const functions = require("firebase-functions");
const mqtt = require('mqtt');
const clientId = 'mqtt_googleserver_rtdb_main'
const topic = '#'

const serviceAccount = require("./****-*****-firebase-adminsdk-*****-******ca66.json");

Admin.initializeApp({
  credential: Admin.credential.cert(serviceAccount),
  databaseURL: "https://**********.firebaseio.com/"
});
exports.rtdb_main = functions.https.onRequest((request, response) => {
var client = mqtt.connect('mqtt://**.***.***.***:****',{
    clientId,
    clean: true,
    connectTimeout: 4000,
    username: '******',
    password: '*********',
    reconnectPeriod: 1000,
})
const db = Admin.database();
 
 //client.addListener or client.on is used to listen to the event 'connect' below 
  client.addListener('connect',function(){
    console.log('Connected')
    client.subscribe([topic],{qos:1})
    console.log(`Subscribe to topic '${topic}'`)
})
client.on('message', (topic, payload) => {
    console.log('Received Message:', topic, payload.toString())
    if(payload.toString() !== "" && topic !== ""){
    var ref = db.ref("All_machines")
    var childref = ref.child(topic.toString())
   childref.set(payload.toString())
   //Use the code below to isolate any data from All Machines
   const topicDetails = topic.split("/")
   const category = topicDetails[0]
   const machineId = topicDetails[1]
   const machineParameter = topicDetails[2]
   
   if (machineParameter === "BoardID")
   {
   var ref = db.ref(machineParameter)
   ref.set(machineId)
   }
   }
  }) 
});

This is my package.json

{
  "name": "sample-http",
  "version": "0.0.1",
  "description": "Cloud Function for Realtime Database",
   "scripts": {
    "serve": "firebase emulators:start --only functions",
      "shell": "firebase functions:shell",
      "start": "npm run shell",
      "deploy": "firebase deploy --only functions",
      "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^11.4.1",
    "firebase-functions": "^3.14.1",
    "mqtt": "^4.3.7",
    "node-schedule": "^2.1.0"
  },

  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
 
}
node.js firebase firebase-realtime-database google-cloud-functions mqtt