Shankar
Shankar

Reputation: 2835

Error in log when accessing Google Compute Python SDK within Google Cloud Function

I am creating a Google Cloud Function to start a Compute VM instance. I am referring to documentation and several SO answers on this topic - like Using gcloud cli within a cloud function and https://stackoverflow.com/a/61343478/6352160

Here is the Cloud Function:

import base64
from googleapiclient import discovery
from google.auth import compute_engine

def hello_pubsub(event, context):
"""Triggered from a message on a Cloud Pub/Sub topic.
Args:
     event (dict): Event payload.
     context (google.cloud.functions.Context): Metadata for the event.
"""
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
print(pubsub_message)
#credentials = GoogleCredentials.get_application_default()
credentials = compute_engine.Credentials()  

service = discovery.build('compute', 'v1',credentials=credentials)
# Project ID for this request.
project = 'Project name' 
# The name of the zone for this request.
zone = 'zone'  
# Name of the instance resource to start.
instance = 'instance-name'
try:
    request = service.instances().start(project=project, zone=zone, instance=instance)
    response = request.execute()
except Exception as e:
    print(e)     

print('VM Instance started')

The requirements tab is:

google-api-python-client

When I run this function I get the following error

E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth TestCloudFunction lt7vm36u2i1w 
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w Traceback (most recent call last): TestCloudFunction lt7vm36u2i1w 
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w   File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module> TestCloudFunction lt7vm36u2i1w 
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w     from oauth2client.contrib.locked_file import LockedFile TestCloudFunction lt7vm36u2i1w 
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w ModuleNotFoundError: No module named 'oauth2client.contrib.locked_file' TestCloudFunction lt7vm36u2i1w 
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w  TestCloudFunction lt7vm36u2i1w 
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w During handling of the above exception, another exception occurred: TestCloudFunction lt7vm36u2i1w 

It looks like an authentication error when initializing the Compute API. Can anyone please let me know how to fix this?

Update: I noticed that in spite of this error the code still successfully starts the Compute VM instance. However I am still wondering why this error is shown and how I can fix this.

Upvotes: 2

Views: 78

Answers (1)

vitooh
vitooh

Reputation: 4272

This might be found in the code for the Google API Python Client (github here).

It seems that this is intended behavior for >=4.0.0 versions of oauth2client just to aware that the feature is not available.

I hope it will help!

Upvotes: 1

Related Questions