首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用GCE Node.js客户端从实例模板创建新的GCE VM实例?

如何使用GCE Node.js客户端从实例模板创建新的GCE VM实例?
EN

Stack Overflow用户
提问于 2018-07-23 22:05:26
回答 1查看 616关注 0票数 2

Google compute engine中,我可以使用instance template从模板创建新的VM。使用GCE-console可以很好地工作,使用API (URL参数"sourceInstanceTemplate")也可以很好地工作。

如何使用googleapis/nodejs-compute ( Node.js GCE SDK)从实例模板创建新的GCE-VM?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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中),效果会很好。

代码语言:javascript
复制
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}
  });
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51480750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档