在Google compute engine中,我可以使用instance template从模板创建新的VM。使用GCE-console可以很好地工作,使用API (URL参数"sourceInstanceTemplate")也可以很好地工作。
如何使用googleapis/nodejs-compute ( Node.js GCE SDK)从实例模板创建新的GCE-VM?
发布于 2018-07-24 19:05:05
可以使用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}
});
}
https://stackoverflow.com/questions/51480750
复制相似问题