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

7

10

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?

Triste answered 28/8, 2023 at 7:2 Comment(3)
The error message tells you to check the logs. Include the relevant log entries in your post.People
May I know which logs, as I already included logs from firebase-debug.logTriste
The error message also tells you which log has more details and provides you with a link to access it.People
T
5

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!

Triste answered 29/8, 2023 at 6:42 Comment(2)
The error message was insanely unhelpful - is that correct?Saturnalia
Similarly with the OP, our issue was with the source code as well rather than some GCP configuration. We changed coding standards and had to rename some files (e.g., change casing from camelCase to PascalCase). Our TypeScript compiler completely ignored the new casing during compilation of the .ts files into its .js counterparts. So, for example, the code produced some errors as it looked for thisFile.js, but, locally, we had ThisFile.js.Ryan
C
8

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.

Cartwheel answered 5/11, 2023 at 11:13 Comment(1)
Thanks a lot! I also used the nodemailer and only installed outside the functions dir. Save me a lot time!Jea
T
5

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!

Triste answered 29/8, 2023 at 6:42 Comment(2)
The error message was insanely unhelpful - is that correct?Saturnalia
Similarly with the OP, our issue was with the source code as well rather than some GCP configuration. We changed coding standards and had to rename some files (e.g., change casing from camelCase to PascalCase). Our TypeScript compiler completely ignored the new casing during compilation of the .ts files into its .js counterparts. So, for example, the code produced some errors as it looked for thisFile.js, but, locally, we had ThisFile.js.Ryan
B
2

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

Berey answered 18/10, 2023 at 5:46 Comment(1)
Better to use the dependencies when you need it to get rid of long (& unnecessary) loading time overheadTriste
L
1

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
Livvyy answered 4/10, 2023 at 3:50 Comment(0)
B
0

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.

Badalona answered 10/4, 2024 at 17:51 Comment(1)
Did the error message tell you anything about having trouble finding that file?Saturnalia
P
0

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.

Poignant answered 13/4, 2024 at 3:19 Comment(0)
T
0

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

Added them and voila.

Tantalate answered 29/6, 2024 at 22:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.