Node.js 如何使用Google云函数启动和停止vm实例?

Node.js 如何使用Google云函数启动和停止vm实例?,node.js,google-cloud-functions,google-compute-engine,Node.js,Google Cloud Functions,Google Compute Engine,我正试图通过云功能启动和停止现有的谷歌云计算虚拟机 我在中发现了这一点,但我不确定这将如何作为云函数工作。身份验证将如何工作?如何安装Node.js库 // BEFORE RUNNING: // --------------- // 1. If not already done, enable the Compute Engine API // and check the quota for your project at // https://console.developers.

我正试图通过云功能启动和停止现有的谷歌云计算虚拟机

我在中发现了这一点,但我不确定这将如何作为云函数工作。身份验证将如何工作?如何安装Node.js库

// 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);
  });
}
谢谢你的帮助

您可以检查此项,这有助于您了解如何使用云函数实现启动和停止实例