[英]How can I use a Google Cloud Function to start and stop vm instances?
我正在嘗試通過雲 function 啟動和停止現有的谷歌雲計算虛擬機。
我在文檔中找到了它,但我不確定它如何作為雲工作 function。身份驗證將如何工作? 如何安裝 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);
});
}
謝謝您的幫助!
您可以查看這篇中型文章,這可以幫助您了解如何使用雲功能實現啟動和停止實例。
目前 GCP 中有一個新功能。 Cloud Scheduler ,允許用戶在 GCE 虛擬機中安排啟動和停止。
Cloud Scheduler是一個完全托管的企業級 cron 作業調度程序。
要啟動/停止 GCE VM Cloud Scheduler,請使用 Cloud Function 和 Pub/Sub。
有一個完整的文檔指南,使用提供的 Cloud Functions 使用 Cloud Scheduler 調度計算實例(在那里您會找到一個 start instance.js function sample 1和一個 stop instance function sample 2 )。
按照指南,您將能夠使用 Google Cloud Function 來啟動和停止 GCE 實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.