Question

I have managed to create an instance and ssh into it. However, I have couple of questions regarding the Google Compute Engine.

  1. I understand that I will be charged for the time my instance is running. That is till I exit out of the instance. Is my understanding correct?
  2. I wish to run some batch job (java program) on my instance. How do I make my instance stop automatically after the job is complete (so that I don't get charged for the additional time it may run)
  3. If I start the job and disconnect my PC, will the job continue to run on the instance?

Regards, Asim

Was it helpful?

Solution

Correct, instances are charged for the time they are running. (to the minute, minimum 10 minutes). Instances run from the time they are started via the API until they are stopped via the API. It doesn't matter if any user is logged in via SSH or not. For most automated use cases users never log in - programs are installed and started via start up scripts.

You can view your running instances via the Cloud Console, to confirm if any are currently running.

If you want to stop your instance from inside the instance, the easiest way is to start the instance with the compute-rw Service Account Scope and use gcutil.

For example, to start your instance from the command line with the compute-rw scope:

$ gcutil --project=<project-id> addinstance <instance name> --service_account_scopes=compute-rw

(this is the default when manually creating an instance via the Cloud Console)

Later, after your batch job completes, you can remove the instance from inside the instance:

$ gcutil deleteinstance -f <instance name>

OTHER TIPS

You can put halt command at the end of your batch script (assuming that you output your results on persistent disk). After halt the instance will have a state of TERMINATED and you will not be charged. See https://developers.google.com/compute/docs/pricing scroll downn to "instance uptime"

You can auto shutdown instance after model training. Just run few extra lines of code after the model training is complete.

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

service = discovery.build('compute', 'v1', credentials=credentials)

# Project ID for this request.
project = 'xyz'  # Project ID
# The name of the zone for this request.
zone = 'xyz'  # Zone information

# Name of the instance resource to stop.
instance = 'xyz'  # instance id

request = service.instances().stop(project=project, zone=zone, instance=instance)
response = request.execute()

add this to your model training script. When the training is complete GCP instance automatically shuts down. More info on official website: https://cloud.google.com/compute/docs/reference/rest/v1/instances/stop

If you want to stop the instance using the python script, you can follow this way:

from google.cloud.compute_v1.services.instances import InstancesClient
from google.oauth2 import service_account

instance_client = InstancesClient().from_service_account_file(<location-path>)
zone = <zone>
project = <project>
instance = <instance_id>
instance_client.stop(project=project, instance=instance, zone=zone)

In the above script, I have assumed you are using service-account for authentication. For documentation of libraries used you can go here: https://googleapis.dev/python/compute/latest/compute_v1/instances.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top