How can I use a Google Cloud Function to start and stop vm instances?

Viewed 3241

I am trying to start and stop existing google cloud compute virtual machines through a cloud function.

I found this in the documentation, but I am not sure how this would work as a cloud function. How would the authentication work? How can I install a Node.js library?

// BEFORE RUNNING:
// ---------------
// 1. If not already done, enable the Compute Engine API
//    and check the quota for your project at
//    https://console.developers.google.com/apis/api/compute
// 2. This sample uses Application Default Credentials for authentication.
//    If not already done, install the gcloud CLI from
//    https://cloud.google.com/sdk and run
//    `gcloud beta auth application-default login`.
//    For more information, see
//    https://developers.google.com/identity/protocols/application-default-credentials
// 3. Install the Node.js client library by running
//    `npm install googleapis --save`

const {google} = require('googleapis');
var compute = google.compute('v1');

authorize(function(authClient) {
  var request = {
    // Project ID for this request.
    project: 'my-project',  // TODO: Update placeholder value.

    // The name of the zone for this request.
    zone: 'my-zone',  // TODO: Update placeholder value.

    // Name of the instance resource to start.
    instance: 'my-instance',  // TODO: Update placeholder value.

    auth: authClient,
  };

  compute.instances.start(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }

    // TODO: Change code below to process the `response` object:
    console.log(JSON.stringify(response, null, 2));
  });
});

function authorize(callback) {
  google.auth.getClient({
    scopes: ['https://www.googleapis.com/auth/cloud-platform']
  }).then(client => {
    callback(client);
  }).catch(err => {
    console.error('authentication failed: ', err);
  });
}

Thank you for your help!

2 Answers

Currently there's a new feature in GCP. Cloud Scheduler, that allow users to schedule starts and stops in GCE VMs.

Cloud Scheduler is a fully managed enterprise-grade cron job scheduler.


To start/stop a GCE VM Cloud Scheduler uses a Cloud Function and Pub/Sub. enter image description here

There's a complete documentation guide to scheduling the compute instances with Cloud Scheduler using the Cloud Functions provided (there you would find a start instance .js function sample 1 and a stop instance function sample 2).

Following the guide you would be able to use a Google Cloud Function to start and stop GCE instances.

To install npm libraries for your cloud functions:

  1. Include a package.json in the same directory as your function (eg. index.js)
  2. And run the gcloud functions deploy <function name> command from within the same directory

Note that the service account running the cloud function needs to have the necessary permissions to start/stop the VMs.

Example code and related tutorial.

Related