I am trying to start a Google Compute instance with the Google API Python Client Library. This is so that a cheap instance (running on a single core) can periodically start and stop a more expensive instance (with many cores) periodically, to keep costs down.
I have successfully installed the different components and run Google's example script create_instance.py (which creates an instances, runs a startup script, and deletes the instance). Inspecting the PyDoc reference for the Compute Engine API, and cross-referencing how the other instances() functions work in the create_instance.py example, I would expect the start instance command to be:
python compute.instances().start(project=*, zone=*, instance=*).execute()
The above command gives me the error "An expression was expected after '('. at line:1 char:34" - this is the first parenthesis.
a. What have I done wrong?
b. Is using the Google API with Python a good way to start instances from other instances, programmatically?
Generally I would expect you would need to import the api library with an import
statement or perhaps a runtime flag (-m somemodule
?). Running a line of python directly from the command line is usually not the best way to proceed. Instead Google provides the gcloud
command line tool.
An authentication/login function is usually called before sending the API actual commands. On a Google VM, this can be either an id/private key or a blank id/key if the VM is specifically authorized to call the API or act as a specific account. This authorization can be set up from the compute engine web control panel when creating a Google VM the first time. On an external VM, it will need an id/private key to supply to the Google API. So, a one liner in python probably won't work as it is missing this step.
The compute.instances().start()
function takes required parameters to start a specific instance that has stopped. That means:
start
From Google Cloud Python Documentation
start(project=, zone=, instance=*) Starts an instance that was stopped using the instances().stop method. For more information, see Restart an instance.
Args: project: string, Project ID for this request. (required)
zone: string, The name of the zone for this request. (required)
instance: string, Name of the instance resource to start. (required)...