0

The code the code bellow someone shared it here (Starting a Google Compute instance with Python), I made some adaptations and it works perfectly for me to start one single vm. I'm wondering if It is possible to adapt this code to start multiples instances in the same function? here is the code:

from googleapiclient import discovery
 
service = discovery.build('compute', 'v1')
 
def autostart_vm(request):
 
        # Project ID for this request.
        project = 'secret-metrics-278618'
 
        # The name of the zone for this request.
        zone = 'us-central1-a' 
 
        # Name of the instance resource to start.
        instance = 'devops1'
        instance2 = 'devops-2'
        
 
        request = service.instances().start(project=project, zone=zone, instance=instance)
        response = request.execute()
        
 
        print('VM Instance started')

1
  • You don't need to pass request as an argument on your function for this to work. Commented Nov 25, 2020 at 3:14

1 Answer 1

0

It is possible though you have to make adjustments. From the description of instances().start():

Starts an instance that was stopped using the instances().stop method. For more information, see Restart an instance.

Meaning you have to iterate the method (assuming that your instances are on a similar project and zone) like this:

# Name of the instance resource to start.
instanceList = ['devops1', 'devops-2']

for i in instanceList:   
  request = service.instances().start(project=project, zone=zone, instance=i)
  response = request.execute()
  print('VM instance '+ i + ' started')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.