How to launch a new VM with azure VM image using nodejs azure SDK?

290 Views Asked by At

I have used AWS nodejs SDK and created a instance by using AMI(amazon machine image) ID. Now, I need to replicate the same for Azure. I came across the documentation and found a method like below:

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

But, I am not sure how to create an image (like AMI in AWS) and then use the image id for launching a VM in Azure. The documentation seems to be pretty straight to the point and lacks explanation required for a beginner like me. Are there any code samples/examples which will be useful for those who are new to azure?

1

There are 1 best solutions below

2
On BEST ANSWER

Regarding the issue, please refer to the following code

  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. Code
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();