eagle
eagle

Reputation: 843

Firebase Functions: Could not create or update Cloud Run service, Container Healthcheck failed

On deploy i got this error

i  functions: creating Node.js 18 (2nd Gen) function addCourseData(us-central1)...
Could not create or update Cloud Run service addcoursedata, Container Healthcheck failed. Revision 'addcoursedata-00001-cup' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.

Logs URL: https://console.cloud.google.com/logs/viewer?project=PROJECT_ID&resource=cloud_run_revision/service_name/addcoursedata/revision_name/addcoursedata-00001-cup&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22addcoursedata%22%0Aresource.labels.revision_name%3D%22addcoursedata-00001-cup%22
For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start

Functions deploy had errors with the following functions:
        addCourseData(us-central1)
i  functions: cleaning up build files...

with --debug I got this log

Total Function Deployment time: 67749
[] 1 Functions Deployed
[] 1 Functions Errored
[] 0 Function Deployments Aborted
[] Average Function Deployment time: 67748

Functions deploy had errors with the following functions:
        addCourseData(us-central1)
[] Not printing URL for HTTPS function. Typically this means it didn't match a filter or we failed deployment

Functions deploy failed.
[] {
  "endpoint": {
    "id": "addCourseData",
    "project": "PROJECT_ID",
    "region": "us-central1",
    "entryPoint": "addCourseData",
    "platform": "gcfv2",
    "runtime": "nodejs18",
    "httpsTrigger": {},
    "labels": {
      "deployment-tool": "cli-firebase"
    },
    "serviceAccount": null,
    "ingressSettings": null,
    "availableMemoryMb": null,
    "timeoutSeconds": null,
    "maxInstances": null,
    "minInstances": null,
    "concurrency": 80,
    "vpc": null,
    "environmentVariables": {
      "FIREBASE_CONFIG": "{\"projectId\":\"PROJECT_ID\",\"databaseURL\":\"https://PROJECT_ID-default-rtdb.asia-southeast1.firebasedatabase.app\",\"storageBucket\":\"PROJECT_ID.appspot.com\"}",
      "GCLOUD_PROJECT": "PROJECT_ID",
      "EVENTARC_CLOUD_EVENT_SOURCE": "projects/PROJECT_ID/locations/us-central1/services/addCourseData"
    },
    "codebase": "default",
    "securityLevel": "SECURE_ALWAYS",
    "cpu": 1,
    "targetedByOnly": true,
    "hash": "38475170b79b25f455db5cacbdc1d6c36adc4679"
  },
  "op": "update",
  "original": {
    "name": "FirebaseError",
    "children": [],
    "exit": 1,
    "message": "Could not create or update Cloud Run service addcoursedata, Container Healthcheck failed. Revision 'addcoursedata-00001-sox' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.\n\nLogs URL: https://console.cloud.google.com/logs/viewer?project=PROJECT_ID&resource=cloud_run_revision/service_name/addcoursedata/revision_name/addcoursedata-00001-sox&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22addcoursedata%22%0Aresource.labels.revision_name%3D%22addcoursedata-00001-sox%22 \nFor more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start",
    "status": 3,
    "code": 3
  }
}
[] Error: Failed to update function addcourseData in region us-central1
    at C:\Users\USER_ABC\AppData\Roaming\npm\node_modules\firebase-tools\lib\deploy\functions\release\fabricator.js:51:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Fabricator.updateV2Function (C:\Users\USER_ABC\AppData\Roaming\npm\node_modules\firebase-tools\lib\deploy\functions\release\fabricator.js:380:32)
    at async Fabricator.updateEndpoint (C:\Users\USER_ABC\AppData\Roaming\npm\node_modules\firebase-tools\lib\deploy\functions\release\fabricator.js:153:13)
    at async handle (C:\Users\USER_ABC\AppData\Roaming\npm\node_modules\firebase-tools\lib\deploy\functions\release\fabricator.js:88:17)

Error: There was an error deploying functions

How to fix it? Any help!

My code is

const calledFunctionName = process.env.K_SERVICE;
if (!calledFunctionName || calledFunctionName === "addCourseData") {
  const {onRequest} = require("firebase-functions/v2/https");

  // Take the email and adds an entry to Firestore with Course data
  exports.addCourseData = onRequest(async (request, response) => {
    return await (await require("./my-functions/course/add-course-data-function"))
        .addCourseData(request, response);
  });
}

Everything was working fine before introducing the process.env.K_SERVICE, so is there anything else to do too, to use the environment variable? As I just added in code to save the cold start and unnecessary file loading.

Why server not able to create a container for the function?

Upvotes: 16

Views: 9276

Answers (8)

Nanda Z
Nanda Z

Reputation: 1886

In my case, if you create a nested project with a parent project that has several dependencies installed. Make sure that the dependences are not shared with the child folder (where package.json from the cloud function is used). Internally, your project may not have errors, but when deployed, sharing dependencies will affect cloud functions. shared dep

Upvotes: 1

Mohammad
Mohammad

Reputation: 11

I had the same problem. As others have mentioned it, I hadn't added the npm packages to package.json.

Added them and voila.

Upvotes: 1

jvdutrag
jvdutrag

Reputation: 45

I am not using the Firebase Functions, but Google Cloud Functions with TS instead. For me the problem was a wrong ES Module import of the @google-cloud/functions-framework package.

Was using:

import functions from '@google-cloud/functions-framework'

Changed to: import * as functions from '@google-cloud/functions-framework'

And it worked.

If you are building TS like me, be sure to run the code you built locally first before pushing.

Upvotes: 0

Mouayad
Mouayad

Reputation: 173

Make sure you have all the dependents you are using specified in package.json:

"dependencies": {
  "firebase-admin": "^11.8.0",
  "firebase-functions": "^4.3.1",
  "nodemailer": "^6.9.7"
},

Same here. I am using Nodemailer so I had to install it first in the function's directory.

"message": "Could not create or update Cloud Run service sendemailonnewentry, Container Healthcheck failed. Revision 'sendemailonnewentry-00001-por' is not ready and cannot serve traffic.

Also make sure you didn't install the dependents in the parent directory. The code in the functions directory will still find the package when run in the local emulator, but the deployment obviously won't.

Upvotes: 11

Leonardo Migliorelli
Leonardo Migliorelli

Reputation: 96

For me the problem was about that I used require("../../service-account.json"); in typescript file and then it compiled and run successfully (also using firebase emulators:start) but when the function started up it didn't find the file (because it was not included into the build folder.

So if someone has this problem where you correctly specified all the dependencies (so that normal build or firebase emulators:start works correctly) the problem might be that in the build folder some files don't exists/aren't included as they should.

From what I understood when firebase deploys a function it creates a container with the folder you specify. So everything that is not inside dist folder might not be included into the container.

Upvotes: 0

gildniy
gildniy

Reputation: 3943

If anyone meets this issue, please verify if all dependencies are installed before deploying.

Upvotes: 2

Sivuyile TG Magutywa
Sivuyile TG Magutywa

Reputation: 1271

In my case, it was caused by npm package, somehow the package was not installed, but there were no compilation errors, locally it worked.

These dependencies were missing in my package.json, I am not sure ht it worked, without them.

@google-cloud/storage  
dayjs

Upvotes: 1

eagle
eagle

Reputation: 843

I need to change this line

if (!calledFunctionName || calledFunctionName === "addCourseData")

to

if (!calledFunctionName || calledFunctionName === "addcoursedata")

OR (better approach would be)

if (!calledFunctionName || calledFunctionName === "addCourseData".toLowerCase())

as process.env.K_SERVICE returns the lower alphabets ONLY, instead of camelCase or name-with-hyphens.

Finally, I found the answer to my issue after 2 days of researching and debugging. Maybe it saves someone's time!

Upvotes: 5

Related Questions