Joey Yi Zhao
Joey Yi Zhao

Reputation: 42596

How can I let `cloud function` find the source code uploaded from terraform?

I have a nodejs app and use terraform to upload it to Cloud function in GCP. But I got below error from terraform command.

Error: Error waiting for Creating CloudFunctions Function: Error 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.

my terraform code is:

resource "google_storage_bucket" "bucket" {
  name = "${local.app_name}-function-${var.project_id}-${var.gcp_region}"
}

resource "google_storage_bucket_object" "archive" {
  name   = "${var.stage}-${local.app_name}-function.zip"
  bucket = google_storage_bucket.bucket.name
  source = "../apps/dist/src.zip"
}

resource "google_cloudfunctions_function" "identity" {
  name        = "gcp-identity"
  description = "GCP Identity"
  runtime     = "nodejs14"

  available_memory_mb   = 512
  source_archive_bucket = google_storage_bucket.bucket.name
  source_archive_object = google_storage_bucket_object.archive.name
  trigger_http          = true
  entry_point           = "gcp-identity"
}

The src.zip file has only one file in the root index.js. The content of this file is:

"use strict";
exports.helloHttp = (req, res) => {
    res.send(`Hello ${JSON.stringify(req)}`);
};
//# sourceMappingURL=index.js.map

I wonder why gcp can't find the function code? Is there a way for me to specify the source code path inside the zip file?

Upvotes: 0

Views: 373

Answers (2)

Joey Yi Zhao
Joey Yi Zhao

Reputation: 42596

It turns out that the entry_point should be the exported function name from index.js file.

Upvotes: 2

Totem
Totem

Reputation: 1222

This is probably happening because of your .ts file.

CloudFunction does not support typescript out of the box

You will need to transpile your code into .js before deploying it into CloudFunction.

Upvotes: 0

Related Questions