0

I would like to automatically create client instances and run client Python script on them. I can create a client instance as follows:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()
service = discovery.build('compute', 'beta', credentials=credentials)

project = 'my-project'
zone = 'us-central1-a'

instance_body = {
    "name": "my-instance-name",
    "sourceMachineImage": "projects/my-project/global/machineImages/my-image",
}

request = service.instances().insert(project=project, zone=zone, body=instance_body)
response = request.execute()

However, the response object does not seem to contain any authentication details for the new instance. Since I have the name of the instance, I can obtain its internal IP address from the list of instances, but that is not enough to connect to it. So how can run my client Python script on the newly created instance?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68

1 Answers1

2

Google's Compute Engine API is used to program the "control plane" for Google Cloud Platform, i.e. it's used to create, update and delete Compute Engine resources on Google Cloud Platform.

Once you've created a Compute Engine instance (VM), you need to use operating-specific tools to access the operating system remotely. For example, if you're creating Linux instances then you can interact with the VM using e.g. SSH. For Windows, I think the tool is called RDP.

Google provides credentials (for all flavors of operating system) for you to access the VM. A common approach when using Linux VMs, is to use SSH and SSH keys. If you use gcloud compute ssh to connect to an instance, gcloud will use locally-generated SSH keys for you (google_compute_engine) and automate the process. For Windows there's a different process.

So you will need to:

  1. Run the above code to create the instance returning e.g. IP|DNS
  2. Create or reuse credentials that provide suitable access to the VM
  3. Use (or automate using a Python library) SSH or RDP to connect to the VM
  4. Authenticate using the credentials
  5. Run your script
DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • I was able to connect using `gcloud compute ssh`. Unfortunately, there is no Python API for it, so I want to use plain `ssh`. How do you I connect with plain `ssh`? – AlwaysLearning May 25 '22 at 16:32
  • There are Python libraries for SSH, I referenced a list of them in my answer. You'll want to pick one and read the docs. Good luck! – DazWilkin May 25 '22 at 16:41
  • Once you've learned how to do it, you'll be able to connect to any Linux VM regardless of whether it's provisioned on GCP or elsewhere. – DazWilkin May 25 '22 at 16:42
  • I meant the ssh keys, which `gcloud compute ssh` seems to produce in the background. But I already found a relevant post: https://stackoverflow.com/questions/27535945/how-to-access-ssh-keys-for-a-google-cloud-platform-compute-engine-vm-instance – AlwaysLearning May 25 '22 at 16:50