Asked 1 month ago by UranianPilot317
Firebase Functions Deployment Error: Container Not Listening on PORT 8080 during Stripe Webhook Setup
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by UranianPilot317
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm trying to deploy Firebase Functions with a Stripe webhook using Svelte5 and have set the secrets as follows:
BASHfirebase functions:secrets:get STRIPE_SECRET_KEY firebase functions:secrets:get STRIPE_WEBHOOK_SECRET
The minimal code in functions/src/index.ts
is:
TYPESCRIPTimport { initializeApp } from 'firebase-admin/app'; import { defineSecret } from 'firebase-functions/params'; import { onRequest } from 'firebase-functions/v2/https'; import Stripe from 'stripe'; // Initialize Firebase initializeApp(); // Define secrets using defineSecret const stripeSecretKey = defineSecret('STRIPE_SECRET_KEY'); const stripeWebhookSecret = defineSecret('STRIPE_WEBHOOK_SECRET'); // Simplified handleStripeWebhook function export const handleStripeWebhook = onRequest( { secrets: [stripeSecretKey, stripeWebhookSecret], cors: true }, async (req, res) => { const stripe = new Stripe(stripeSecretKey.value()); const sig = req.headers['stripe-signature']; let event; try { const body = req.rawBody.toString(); event = stripe.webhooks.constructEvent(body, sig, stripeWebhookSecret.value()); } catch (err) { console.error(`Webhook Error: ${(err as Error).message}`); res.status(400).send(`Webhook Error: ${(err as Error).message}`); return; } // Log the event for troubleshooting console.log(`Received event: ${JSON.stringify(event)}`); res.json({ received: true }); } );
However, during deployment I encounter the following error:
BASHI functions: updating Node.js 18 (2nd Gen) function handleStripeWebhook(us-central1)... Could not create or update Cloud Run service handlestripewebhook, Container Healthcheck failed. Revision 'handlestripewebhook-00012-qar' 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 within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. Logs for this revision might contain more information. Logs URL: https://console.cloud.google.com/logs/viewer?project=XXXX&resource=cloud_run_revision/service_name/handlestripewebhook/revision_name/handlestripewebhook-00012-qar&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22handlestripewebhook%22%0Aresource.labels.revision_name%3D%22handlestripewebhook-00012-qar%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: handleStripeWebhook(XXXXX) I functions: cleaning up build files... ⚠ functions: Unhandled error cleaning up build images. This could result in a small monthly bill if not corrected. You can attempt to delete these images by redeploying or you can delete them manually at https://console.cloud.google.com/gcr/images/XXXX/us/gcf Error: There was an error deploying functions
I've tried deleting and reinstalling the node modules (as suggested in this thread: https://github.com/firebase/firebase-tools/issues/6465#issuecomment-2484556000) and even recreated the secrets. Nothing seems to resolve the issue.
EDIT:
Error 1
JSON{ "textPayload": "Default STARTUP TCP probe failed 1 time consecutively for container \"worker\" on port 8080. The instance was not started.", "insertId": "6758d8180000a03441679b15", "resource": { "type": "cloud_run_revision", "labels": { "revision_name": "handlestripewebhook-00012-qar", "service_name": "handlestripewebhook", "configuration_name": "handlestripewebhook", "location": "REDACTED_LOCATION", "project_id": "REDACTED_PROJECT_ID" } }, "timestamp": "2024-12-11T00:08:56.041012Z", "severity": "ERROR", "labels": { "goog-managed-by": "cloudfunctions", "instanceId": "REDACTED_INSTANCE_ID" }, "logName": "projects/REDACTED_PROJECT_ID/logs/run.googleapis.com%2Fvarlog%2Fsystem", "receiveTimestamp": "2024-12-11T00:08:56.295019255Z" }
Error 2
JSON{ "protoPayload": { "@type": "type.googleapis.com/google.cloud.audit.AuditLog", "status": { "code": 9, "message": "Ready condition status changed to False for Revision handlestripewebhook-00012-qar with message: The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. 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/handlestripewebhook/revision_name/handlestripewebhook-00012-qar&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22handlestripewebhook%22%0Aresource.labels.revision_name%3D%22handlestripewebhook-00012-qar%22\nFor more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start" }, "serviceName": "run.googleapis.com", "methodName": "/InternalServices.ReplaceInternalService", "resourceName": "namespaces/PROJECT_ID/revisions/handlestripewebhook-00012-qar", "response": { "metadata": { "name": "handlestripewebhook-00012-qar", "namespace": "PROJECT_NAMESPACE", "selfLink": "/apis/serving.knative.dev/v1/namespaces/PROJECT_NAMESPACE/revisions/handlestripewebhook-00012-qar", "uid": "REDACTED_UID", "resourceVersion": "REDACTED_VERSION", "generation": 1, "creationTimestamp": "2024-12-11T00:08:23.090699Z", "labels": { "serving.knative.dev/configuration": "handlestripewebhook", "serving.knative.dev/configurationGeneration": "12", "serving.knative.dev/service": "handlestripewebhook", "serving.knative.dev/serviceUid": "REDACTED_UID", "goog-managed-by": "cloudfunctions", "goog-cloudfunctions-runtime": "nodejs18", "cloud.googleapis.com/location": "us-central1", "serving.knative.dev/route": "handlestripewebhook", "run.googleapis.com/startupProbeType": "Default" }, "annotations": { "run.googleapis.com/client-name": "cloudfunctions", "run.googleapis.com/startup-cpu-boost": "true", "cloudfunctions.googleapis.com/trigger-type": "HTTP_TRIGGER", "run.googleapis.com/secrets": "secret-REDACTED:projects/PROJECT_ID/secrets/STRIPE_WEBHOOK_SECRET,secret-REDACTED:projects/PROJECT_ID/secrets/STRIPE_SECRET_KEY", "run.googleapis.com/build-source-location": "{\"worker\":\"gs://gcf-v2-sources-PROJECT_ID-us-central1/handleStripeWebhook/function-source.zip#REDACTED\"}", "serving.knative.dev/creator": "service-PROJECT_ID@gcf-admin-robot.iam.gserviceaccount.com", "run.googleapis.com/build-function-target": "{\"worker\":\"handleStripeWebhook\"}", "run.googleapis.com/base-images": "{\"worker\":\"us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/nodejs18\"}", "run.googleapis.com/build-id": "{\"worker\":\"REDACTED\"}", "run.googleapis.com/launch-stage": "BETA", "run.googleapis.com/operation-id": "REDACTED", "autoscaling.knative.dev/maxScale": "100" }, "ownerReferences": [ { "kind": "Configuration", "name": "handlestripewebhook", "uid": "REDACTED_UID", "apiVersion": "serving.knative.dev/v1", "controller": true, "blockOwnerDeletion": true } ] }, "apiVersion": "serving.knative.dev/v1", "kind": "Revision", "spec": { "containerConcurrency": 80, "timeoutSeconds": 60, "serviceAccountName": "PROJECT_ID-compute@developer.gserviceaccount.com", "containers": [ { "name": "worker", "image": "us-central1-docker.pkg.dev/PROJECT_ID/gcf-artifacts/PROJECT_ID__us--central1__handle_stripe_webhook@sha256:REDACTED", "ports": [ { "name": "http1", "containerPort": 8080 } ], "env": [ { "name": "BLA_SENDGRID_KEY", "value": "REDACTED" }, { "name": "BLA_ENDGRID_TEMPLATE", "value": "REDACTED" }, { "name": "FIREBASE_CONFIG", "value": "{\"projectId\":\"PROJECT_ID\",\"storageBucket\":\"PROJECT_ID.firebasestorage.app\"}" }, { "name": "GCLOUD_PROJECT", "value": "PROJECT_ID" }, { "name": "EVENTARC_CLOUD_EVENT_SOURCE", "value": "projects/PROJECT_ID/locations/us-central1/services/handleStripeWebhook" }, { "name": "FUNCTION_TARGET", "value": "handleStripeWebhook" }, { "name": "LOG_EXECUTION_ID", "value": "true" }, { "name": "STRIPE_SECRET_KEY", "valueFrom": { "secretKeyRef": { "key": "2", "name": "secret-REDACTED" } } }, { "name": "STRIPE_WEBHOOK_SECRET", "valueFrom": { "secretKeyRef": { "key": "2", "name": "secret-REDACTED" } } } ], "resources": { "limits": { "cpu": "1", "memory": "256Mi" } }, "startupProbe": { "timeoutSeconds": 240, "periodSeconds": 240, "failureThreshold": 1, "tcpSocket": { "port": 8080 } } } ], "runtimeClassName": "run.googleapis.com/linux-base-image-update" }, "status": { "observedGeneration": 1, "conditions": [ { "type": "Ready", "status": "False", "reason": "HealthCheckContainerError", "message": "The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. 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/handlestripewebhook/revision_name/handlestripewebhook-00012-qar&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22handlestripewebhook%22%0Aresource.labels.revision_name%3D%22handlestripewebhook-00012-qar\nFor more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start", "lastTransitionTime": "2024-12-11T00:08:56.042205Z" }, { "type": "ContainerHealthy", "status": "False", "reason": "HealthCheckContainerError", "message": "The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. 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/handlestripewebhook/revision_name/handlestripewebhook-00012-qar&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22handlestripewebhook%22%0Aresource.labels.revision_name%3D%22handlestripewebhook-00012-qar\nFor more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start", "lastTransitionTime": "2024-12-11T00:08:56.042205Z" }, { "type": "ContainerReady", "status": "True", "message": "Container image import completed in 5.970382s.", "lastTransitionTime": "2024-12-11T00:08:29.856747Z" }, { "type": "ResourcesAvailable", "status": "True", "message": "Revision deployment finished in 23.73708s. Checking container health.", "lastTransitionTime": "2024-12-11T00:08:53.590376Z" }, { "type": "Retry", "status": "True", "reason": "ImmediateRetry", "message": "System will retry after 0:00:00 from lastTransitionTime for attempt 0.", "lastTransitionTime": "2024-12-11T00:08:53.590376Z", "severity": "Info" } ], "logUrl": "https://console.cloud.google.com/logs/viewer?project=PROJECT_ID&resource=cloud_run_revision/service_name/handlestripewebhook/revision_name/handlestripewebhook-00012-qar&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22handlestripewebhook%22%0Aresource.labels.revision_name%3D%22handlestripewebhook-00012-qar", "imageDigest": "us-central1-docker.pkg.dev/PROJECT_ID/gcf-artifacts/PROJECT_ID__us--central1__handle_stripe_webhook@sha256:REDACTED", "containerStatuses": [ { "name": "worker", "imageDigest": "us-central1-docker.pkg.dev/PROJECT_ID/gcf-artifacts/PROJECT_ID__us--central1__handle_stripe_webhook@sha256:REDACTED" } ] }, "@type": "type.googleapis.com/google.cloud.run.v1.Revision" } }, "insertId": "umk9de28fhw", "resource": { "type": "cloud_run_revision", "labels": { "service_name": "handlestripewebhook", "revision_name": "handlestripewebhook-00012-qar", "project_id": "PROJECT_ID", "location": "us-central1", "configuration_name": "handlestripewebhook" } }, "timestamp": "2024-12-11T00:08:56.052959Z", "severity": "ERROR", "logName": "projects/PROJECT_ID/logs/cloudaudit.googleapis.com%2Fsystem_event", "receiveTimestamp": "2024-12-11T00:08:56.435853493Z" }
I’m looking for insight on why the container isn’t starting properly and not listening on port 8080, and how to resolve this health check failure during deployment.
Comments 0
•Answers 2
•Views 107
Answer by TychoAI Agent • 1 month ago
The error:
"The user‐provided container failed to start and listen on the port defined (PORT=8080) …"
means that Cloud Run’s health checks (which are automatically applied as part of functions v2 deploy) are not detecting a service listening on port 8080. In Cloud Functions (v2, which runs on Cloud Run behind the scenes) an onRequest trigger is expected to set up an HTTP server that listens on process.env.PORT (default 8080). Since that isn’t happening, the runtime is marking the revision unhealthy.
Here are some troubleshooting suggestions:
Verify that your function code is built and loaded correctly.
• Make sure your TypeScript compiles without errors and that the deployed bundle contains your exported onRequest function.
• Try deploying a minimal “Hello World” HTTP function (without secret calls or Stripe code) to see if that works. This helps isolate whether the issue is with the environment setup or with your added dependencies.
Check secret injection and initialization.
• When you call stripeSecretKey.value() and stripeWebhookSecret.value(), ensure that these secrets are indeed defined.
• You might add temporary logging (at startup or inside the function) to verify that the values are nonempty.
• (Note: For sensitive keys, do not log production secrets. Use this only for debugging.)
Stripe library configuration.
• In most examples using Stripe, the constructor is called with an options object (including an API version) for extra safety. For example:
TYPESCRIPTconst stripe = new Stripe(stripeSecretKey.value(), { apiVersion: '2020-08-27', });
• Although omitting the API version usually won’t cause a container not to bind to PORT, it’s worth aligning with best practices.
Raw body availability.
• Since Stripe webhook verification requires the raw request body, confirm that the runtime is providing it. The v2 HTTP trigger is supposed to make the rawBody available in req.rawBody—but if it isn’t set up (or if middleware is inadvertently consuming it), the function might crash before the HTTP server is fully ready.
• If needed, review the Firebase Functions documentation to verify that no other middleware or configuration is interfering.
Examine logs via Cloud Logging.
• The startup TCP probe error means that before any request arrives, the container did not open a listening socket on the expected port. Look at the logs (using the provided Cloud Logging URL) for any crash or error messages during startup. Sometimes an unhandled error in global initialization (for example, if the secret value is missing) might cause the process to exit immediately.
Remove extra configuration temporarily.
• Try removing or setting aside the CORS option or extra library initialization so you can deploy a minimal function that does nothing but return a simple response. Once that deploys successfully, reintroduce your Stripe integration step by step.
In summary, the problem is not with deploying secrets per se but with your function code (or its deployment configuration) not resulting in a container that listens on port 8080. Make sure that your Cloud Functions v2 code (with onRequest) is correctly compiled and that no early runtime errors (such as missing secrets or misconfiguration in the Stripe constructor) are causing the container to crash.
Once you’ve confirmed a minimal working function, add your secrets and Stripe integration step by step, checking Cloud Logging at each step. Also, refer to the troubleshooting guide for Cloud Run (the “Container failed to start” section) for additional tips if the container still does not bind as expected.
These steps should help narrow down whether the issue is with the function’s startup (and by extension, the Cloud Run container) or with the integration of your secrets/Stripe configuration.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by MartianRanger197 • 3 months ago
So I made the biggest blunder of all - I noticed via firebase deploy --only functions --debug
that I didn't have stripe installed. So npm i stripe
in my functions
folder and all good to go.
No comments yet.
No comments yet.