Home > database >  gcloud functions deploy issue
gcloud functions deploy issue

Time:03-09

I try to deploy one gcloud function with the below Github link to back up the datastore.

https://github.com/portsoc/cloud-simple-datastore-backup/blob/master/index.js

After updating the variant BUCKET_NAME with my cloud storage bucket name, I run it under gcloud shell with the command: node index.js and it will backup the datastore successfully.

but when I continue to run the below command to deploy it:

gcloud functions deploy main
--runtime nodejs12 --trigger-http --allow-unauthenticated
--region=asia-southeast2

After a while, it will give me the below error:

Deploying function (may take a while - up to 2 minutes)...failed.
ERROR: (gcloud.functions.deploy) OperationError: 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.
Click to view the error screenshot

Any suggestion on this?

CodePudding user response:

Cloud Functions have a specific set of signatures that must be used.

I'm less familiar with JavaScript|Node.JS but I think the function you reference is intended to be invoked as you do node index.js (or similar) and this is incompatible with Cloud Functions.

Please review Write Cloud Functions to understand that signature type that you will need. You will probably have to tweak the authentication in the example to better meet your needs too.

Almost certainly you don't want --allow-unauthenticated either.

CodePudding user response:

After changing the upload code below, I can deploy it.

const { GoogleAuth } = require("google-auth-library");

// fill in your bucket name here:
const BUCKET_NAME = "gs://testinbbk10";


exports.myfunction = async (req,res) =>{
  try {
    const auth = new GoogleAuth({
      scopes: "https://www.googleapis.com/auth/cloud-platform",
    });
    const client = await auth.getClient();
    const projectId = await auth.getProjectId();
    console.log(`Project ID is ${projectId}`);

    const res2 = await client.request({
      method: "POST",
      url: `https://datastore.googleapis.com/v1/projects/${projectId}:export`,
      data: {
        outputUrlPrefix: BUCKET_NAME,
      },
    });
    console.error("RESPONSE:");
    console.log(res2.data);
  } catch (error) {
    console.error("ERROR");
    console.error(error);
  }
}

but when I try to access the provided link: deploy link, it will show me the error: could not handle the request.

I am confused about how to properly deploy this to the google cloud function? Just want to deploy one simply google cloud function to backup datastore in the google cloud.

  • Related