Home > Software design >  Can't deploy firestore cloud functions
Can't deploy firestore cloud functions

Time:05-25

I am trying to integrate Elastic Search in my Flutter app. I am trying to deploy a few functions but nothing seems to work.

here is my index.js code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { Client } = require('@elastic/elasticsearch')

admin.initializeApp(functions.config().firebase);

const env = functions.config();
const auth = {
  username: env.elasticsearch.username,
  password: env.elasticsearch.password,
};

const client = new Client({
    node: env.elasticsearch.url,
    auth: auth
})

exports.createPost = functions.firestore
    .document('bookings/{BookingId}')
    .onCreate( async (snap, context) => {
        await client.index({
            index: 'bookings',
            type: '_doc',
            id: snap.id,
            body: snap.data()
        })
    });

exports.updatePost = functions.firestore
    .document('bookings/{BookingId}')
    .onUpdate( async (snap, context) => {
        await client.update({
            index: 'bookings',
            type: '_doc',
            id: context.params.BookingId,
            body: snap.after.data()
        })
    });

exports.deletePost = functions.firestore
    .document('bookings/{BookingId}')
    .onDelete( snap => {
        client.delete({
            index: 'bookings',
            type: '_doc',
            id: snap.id,
        })
    });

Here is the packages.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint",
    "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": "^10.0.2",
    "firebase-functions": "^3.18.0"
    },
  "devDependencies": {
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

I installed elasticsearch using npm:

npm install @elastic/elasticsearch

Everytime I deploy, I get the following output:

 deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> lint
> eslint

   functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
   functions: required API cloudfunctions.googleapis.com is enabled
   functions: required API cloudbuild.googleapis.com is enabled
i  functions: preparing codebase default for deployment
i  functions: preparing functions directory for uploading...
i  functions: packaged C:\Users\Danis\Desktop\space-shuttle-functions\functions (68.46 KB) for uploading
   functions: functions folder uploaded successfully
i  functions: updating Node.js 16 function createPost(us-central1)...
i  functions: updating Node.js 16 function updatePost(us-central1)...
i  functions: updating Node.js 16 function deletePost(us-central1)...
Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.
Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.
Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.

Functions deploy had errors with the following functions:
        createPost(us-central1)
        deletePost(us-central1)
        updatePost(us-central1)
i  functions: cleaning up build files...
Error: There was an error deploying functions:
- Error Failed to update function createPost in region us-central1
- Error Failed to update function deletePost in region us-central1
- Error Failed to update function updatePost in region us-central1

functions:log shows me the following:

PS C:\Users\Danis\Desktop\space-shuttle-functions> firebase functions:log
2022-05-25T02:35:40.303705Z ? deletePost: - /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
2022-05-25T02:35:40.303713Z ? deletePost: - /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/main.js
2022-05-25T02:35:40.303723Z ? deletePost:     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-05-25T02:35:40.303730Z ? deletePost:     at Function.Module._load (node:internal/modules/cjs/loader:778:27)
2022-05-25T02:35:40.303737Z ? deletePost:     at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-25T02:35:40.303744Z ? deletePost:     at require (node:internal/modules/cjs/helpers:102:18)
2022-05-25T02:35:40.303751Z ? deletePost:     at Object.<anonymous> (/workspace/index.js:3:20)
2022-05-25T02:35:40.303757Z ? deletePost:     at Module._compile (node:internal/modules/cjs/loader:1101:14)
2022-05-25T02:35:40.303764Z ? deletePost:     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
2022-05-25T02:35:40.303771Z ? deletePost:     at Module.load (node:internal/modules/cjs/loader:981:32)
2022-05-25T02:35:40.303777Z ? deletePost:     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-05-25T02:35:40.303784Z ? deletePost:     at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-25T02:35:40.304148Z ? deletePost: Could not load the function, shutting down.
2022-05-25T02:35:40.720348431Z E deletePost: 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

2022-05-25T02:35:40.921043Z E deletePost: {"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation."},"authenticationInfo":{"principalEmail":"[email protected]"},"serviceName":"cloudfunctions.googleapis.com","methodName":"google.cloud.functions.v1.CloudFunctionsService.UpdateFunction","resourceName":"projects/space-shuttle-b0738/locations/us-central1/functions/deletePost"}
2022-05-25T02:35:41.776Z ? updatePost: Provided module can't be loaded.
2022-05-25T02:35:41.776039Z ? updatePost: Did you list all required modules in the package.json dependencies?
2022-05-25T02:35:41.776055Z ? updatePost: Detailed stack trace: Error: Cannot find module '@elastic/elasticsearch'
2022-05-25T02:35:41.776063Z ? updatePost: Require stack:
2022-05-25T02:35:41.776077Z ? updatePost: - /workspace/index.js
2022-05-25T02:35:41.776089Z ? updatePost: - /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
2022-05-25T02:35:41.776098Z ? updatePost: - /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/main.js
2022-05-25T02:35:41.776109Z ? updatePost:     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-05-25T02:35:41.776116Z ? updatePost:     at Function.Module._load (node:internal/modules/cjs/loader:778:27)
2022-05-25T02:35:41.776124Z ? updatePost:     at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-25T02:35:41.776132Z ? updatePost:     at require (node:internal/modules/cjs/helpers:102:18)
2022-05-25T02:35:41.776139Z ? updatePost:     at Object.<anonymous> (/workspace/index.js:3:20)
2022-05-25T02:35:41.776147Z ? updatePost:     at Module._compile (node:internal/modules/cjs/loader:1101:14)
2022-05-25T02:35:41.776155Z ? updatePost:     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
2022-05-25T02:35:41.776162Z ? updatePost:     at Module.load (node:internal/modules/cjs/loader:981:32)
2022-05-25T02:35:41.776170Z ? updatePost:     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-05-25T02:35:41.776178Z ? updatePost:     at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-25T02:35:41.776649Z ? updatePost: Could not load the function, shutting down.
2022-05-25T02:35:42.152840030Z E updatePost: 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

2022-05-25T02:35:42.370105Z E updatePost: {"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation."},"authenticationInfo":{"principalEmail":"[email protected]"},"serviceName":"cloudfunctions.googleapis.com","methodName":"google.cloud.functions.v1.CloudFunctionsService.UpdateFunction","resourceName":"projects/space-shuttle-b0738/locations/us-central1/functions/updatePost"}

The error seems to indicate that the elasticsearch module cannot be found which I do not know why. P.S : I am super new to Javascript, Firebase Cloud Functions and Elastic Search.

CodePudding user response:

Your elastic search package is not installed successfully. Make sure you are in the 'functions' directory when you run npm i @elastic/elasticsearch command. Also you can grab the latest version of your package from npm and add it to your package.json manually if the issue persists. You can add this to your dependencies:

"@elastic/elasticsearch": "^8.2.0"
  • Related