Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Function Accessing BigQuery

I'm trying to write a google cloud function that accesses my bigquery data. However, I get an error when I run it. It works correctly if I run under the local emulator, but not when deployed.

My package.json:

{
  "dependencies": {
    "@google-cloud/bigquery": "^0.9.6"
  }
}

index.js

var BigQuery = require('@google-cloud/bigquery');

exports.hello = function hello(req, res) {

    var bigQuery = BigQuery({ projectId: 'influence-ranking' });
    bigQuery.query({
        query: 'SELECT label from `influence-ranking.wikidata.labels` LIMIT 1',
        useLegacySql: false
    }).then(function () {
        return res.status(200).send('hello');
    }).catch(function (error) {
        return res.status(500).json(error);
    });
};

I deploy the function:

$ gcloud beta functions deploy hello --stage-bucket influence-ranking-web --trigger-http --project influence-ranking

Copying file:///tmp/tmpwR3Sza/fun.zip [Content-Type=application/zip]...
/ [1 files][  7.3 KiB/  7.3 KiB]                                                
Operation completed over 1 objects/7.3 KiB.                                      
Deploying function (may take a while - up to 2 minutes)...done.                                                                      
availableMemoryMb: 256
entryPoint: hello
httpsTrigger:
  url: https://us-central1-influence-ranking.cloudfunctions.net/hello
labels:
  deployment-tool: cli-gcloud
latestOperation: operations/aW5mbHVlbmNlLXJhbmtpbmcvdXMtY2VudHJhbDEvaGVsbG8vdHJoV1pjeWlnSkk
name: projects/influence-ranking/locations/us-central1/functions/hello
serviceAccount: [email protected]
sourceArchiveUrl: gs://influence-ranking-web/us-central1-hello-poucdhlbhptc.zip
status: READY
timeout: 60s
updateTime: '2017-09-20T18:42:22Z'
versionId: '15'


$ curl https://us-central1-influence-ranking.cloudfunctions.net/hello
{"code":403,"errors":[{"domain":"usageLimits","reason":"accessNotConfigured","message":"Access Not Configured. BigQuery API has not been used in project 283683622993 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/bigquery.googleapis.com/overview?project=283683622993 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.","extendedHelp":"https://console.developers.google.com/apis/api/bigquery.googleapis.com/overview?project=283683622993"}],"message":"Access Not Configured. BigQuery API has not been used in project 283683622993 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/bigquery.googleapis.com/overview?project=283683622993 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."}

The link in the error message gives the error:

The API "bigquery.googleapis.com" doesn't exist or you don't have permission to access it

It looks like the project parameter doesn't correspond to the project number listed for my project in google cloud console. So it makes me think that somehow I'm getting the request being made against the wrong project, but I can't figure out how.

like image 651
Winston Ewert Avatar asked Sep 20 '17 18:09

Winston Ewert


1 Answers

Something was messed up in my google cloud functions account.

I ended up:

  1. Deleting all my functions
  2. Disabling the google cloud functions
  3. Re-enabling the google cloud functions

And now it works.

Importantly, the output from deploying now says:

serviceAccount: [email protected]

So it is using the correct service account.

like image 72
Winston Ewert Avatar answered Sep 28 '22 12:09

Winston Ewert