0

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?

CuteBoy
  • 103
  • 1
  • 3
  • 13
  • Is that you want to know how to use vhd to create custom image and use the image to create VM? – Jim Xu Jun 01 '21 at 08:19
  • Besides, could you please provide the steps how you do that in amazon? – Jim Xu Jun 01 '21 at 08:20
  • @JimXu I am new to Azure, so I am not sure what is the equivalent of AMI in Azure. In AWS, I am using EC2.runInstances method which takes a parameter called RunInstancesRequest inside which I have mentioned ami-id, subnet-id and couple other properties. – CuteBoy Jun 01 '21 at 08:35
  • If you want to create azure vm, you can refer to https://www.techrepublic.com/article/how-to-create-and-deploy-a-virtual-machine-in-microsoft-azure/. Azure has provided some images. – Jim Xu Jun 01 '21 at 08:46
  • @JimXu I need to create a azure VM using nodejs azure sdk , and not using the management console. Secondly, I do not need a fresh new VM, instead I need to create a new VM based on an image. When the VM is created based on the image, the VM will include files/services already existing in it because it has been launched using the image. This is possible in case of AWS since it has a concept of AMI (amazon machine images), but I am unsure in case of Azure. Hope you can now understand what I need. – CuteBoy Jun 01 '21 at 08:54
  • Is that you have exported your VM as VHD file then you want to use the VHD file to create a new VM? – Jim Xu Jun 01 '21 at 09:02
  • I want to launch a new VM using an already existing Azure VM image. This is to be done using nodejs. – CuteBoy Jun 01 '21 at 09:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233166/discussion-between-cuteboy-and-jim-xu). – CuteBoy Jun 01 '21 at 09:20
  • Do you have any other concerns? If you have no other concerns. could you please accept it as an answer? – Jim Xu Jun 03 '21 at 01:00
  • Thanks. I have accepted the answer. I will try it out and let you know the result. – CuteBoy Jun 03 '21 at 05:47

1 Answers1

0

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

Jim Xu
  • 21,610
  • 2
  • 19
  • 39
  • I have two more questions. Do you know what is the response like in case of computeClient.virtualMachines.createOrUpdate method (maybe a stub response)? Also, Is public ip associated only with resource group and not the VM? – CuteBoy Jun 09 '21 at 08:16
  • @CuteBoy Please refer to https://learn.microsoft.com/en-us/javascript/api/@azure/arm-compute/virtualmachines?view=azure-node-latest#createOrUpdate_string__string__VirtualMachine__msRest_RequestOptionsBase_ – Jim Xu Jun 10 '21 at 01:19