【问题标题】:How to launch a new VM with azure VM image using nodejs azure SDK?如何使用 nodejs azure SDK 启动具有 azure VM 映像的新 VM?
【发布时间】:2026-02-17 15:55:01
【问题描述】:

我使用 AWS nodejs SDK 并使用 AMI(亚马逊机器映像)ID 创建了一个实例。现在,我需要为 Azure 复制相同的内容。我偶然发现了文档并找到了如下方法:

resourceClient.resourceGroups.createOrUpdate(resourceGroupName, groupParameters, callback);

但是,我不确定如何创建映像(如 AWS 中的 AMI),然后使用映像 ID 在 Azure 中启动 VM。该文档似乎非常直截了当,并且缺乏像我这样的初学者所需的解释。是否有任何代码示例/示例对 azure 新手有用?

【问题讨论】:

  • 你想知道如何使用vhd创建自定义镜像并使用镜像创建VM吗?
  • 另外,能否请您提供您在亚马逊上的操作步骤?
  • @JimXu 我是 Azure 的新手,所以我不确定 Azure 中的 AMI 等价物是什么。在 AWS 中,我使用 EC2.runInstances 方法,该方法接受一个名为 RunInstancesRequest 的参数,其中我提到了 ami-id、subnet-id 和其他一些属性。
  • 如果想创建azure vm,可以参考techrepublic.com/article/…。 Azure 提供了一些图像。
  • @JimXu 我需要使用 nodejs azure sdk 创建一个 azure VM,而不是使用管理控制台。其次,我不需要全新的虚拟机,而是需要基于映像创建新的虚拟机。基于映像创建 VM 时,VM 将包含其中已经存在的文件/服务,因为它是使用映像启动的。这在 AWS 的情况下是可能的,因为它具有 AMI(亚马逊机器映像)的概念,但我不确定在 Azure 的情况下。希望你现在能明白我需要什么。

标签: node.js azure azure-sdk azure-sdk-js


【解决方案1】:

关于问题,请参考以下代码

  1. Create a service principal and assign Azure RABC role contributor to the sp
az ad sp create-for-rbac --name YOUR-SERVICE-PRINCIPAL-NAME
  1. 代码
const { ClientSecretCredential } = require("@azure/identity");
const { NetworkManagementClient } = require("@azure/arm-network");
const { ComputeManagementClient } = require("@azure/arm-compute");
const { ResourceManagementClient } = require("@azure/arm-resources");
const clientId = "the appId of the sp";
const tenant = "tenant id";
const clientSecret = "the clientsecret of the sp";
const subscriptionId = "the id of your Azure subscription";
const creds = new ClientSecretCredential(tenant, clientId, clientSecret);
const resouceClient = new ResourceManagementClient(creds, subscriptionId);
const newtworkClient = new NetworkManagementClient(creds, subscriptionId);
const computeClient = new ComputeManagementClient(creds, subscriptionId);
async function main() {
  try {
    // create resource group
    const group = await resouceClient.resourceGroups.createOrUpdate(
      "testdf78",
      {
        location: "eastasia",
      }
    );
    // create vnet and subnet
    const vnet = await newtworkClient.virtualNetworks.createOrUpdate(
      group.name,
      "testdf1_vnet",
      {
        addressSpace: {
          addressPrefixes: ["10.0.0.0/16"],
        },
        location: group.location,
        subnets: [{ name: "default", addressPrefix: "10.0.0.0/24" }],
      }
    );
    // create public ip
    const ip = await newtworkClient.publicIPAddresses.createOrUpdate(
      group.name,
      "testdf1_ip",
      {
        location: group.location,
        publicIPAllocationMethod: "Dynamic",
      }
    );
    // create nic
    const nic = await newtworkClient.networkInterfaces.createOrUpdate(
      group.name,
      "testdf1_nic",
      {
        location: group.location,
        ipConfigurations: [
          {
            name: "test",
            privateIPAllocationMethod: "Dynamic",
            subnet: vnet.subnets[0],
            publicIPAddress: ip,
          },
        ],
      }
    );
    // get you custom  image
    const image = await computeClient.images.get("<groupname>", "<image name>");
    //create vm
    computeClient.virtualMachines.createOrUpdate(group.name, "testdf1", {
      location: group.location,
      hardwareProfile: {
        vmSize: "Standard_B1s",
      },
      storageProfile: {
        imageReference: {
          id: image.id,
        },
        osDisk: {
          caching: "ReadWrite",
          managedDisk: {
            storageAccountType: "Standard_LRS",
          },
          name: "testdf1osdisk",
          createOption: "FromImage",
        },
      },
      osProfile: {
        computerName: "testdf1",
        adminUsername: "testqw",
        adminPassword: "Password0123!",
        linuxConfiguration: {
          patchSettings: { patchMode: "ImageDefault" },
        },
      },
      networkProfile: {
        networkInterfaces: [
          {
            id: nic.id,
          },
        ],
      },
      diagnosticsProfile: {
        bootDiagnostics: {
          enabled: true,
        },
      },
    });
  } catch (error) {
    console.log(error);
  }
}

main();

【讨论】: