在Google compute engine我可以使用 instance template从模板创建新 VM。使用 GCE 控制台可以正常工作,使用 API 也可以正常工作,也是(URL 参数“sourceInstanceTemplate”)。
如何使用 googleapis/nodejs-compute 从实例模板创建新的 GCE-VM (Node.js GCE SDK)?
最佳答案
google-auth-library-nodejs可用于访问 GCE instances.insert API直接。
以下示例改编自https://github.com/google/google-auth-library-nodejs如果在 GCE 中执行(特别是在 Google Cloud Function 中),则工作正常。
const zone = 'some-zone';
const name = 'a-name';
const sourceInstanceTemplate = `some-template-name`;
createVM(zone, name, sourceInstanceTemplate)
.then(console.log)
.catch(console.error);
async function createVM(zone, vmName, templateName) {
const {auth} = require('google-auth-library');
const client = await auth.getClient({
scopes: 'https://www.googleapis.com/auth/cloud-platform'
});
const projectId = await auth.getDefaultProjectId();
const sourceInstanceTemplate = `projects/${projectId}/global/instanceTemplates/${templateName}`;
const url = `https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/instances?sourceInstanceTemplate=${sourceInstanceTemplate}`;
return await client.request({
url: url,
method: 'post',
data: {name: vmName}
});
}
关于google-compute-engine - 如何使用 GCE Node.js 客户端从实例模板创建新的 GCE VM 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51480750/