You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用ARM模板或Azure .NET SDK修改现有Azure虚拟机的规格

Hey there! Let's break down exactly how to modify an existing Azure VM's size using either ARM templates or the Azure .NET SDK—since that's exactly what you're needing. Here's what you need to know:

Using ARM Templates to Update VM Size

First off, a critical note: you can only change a VM's size when it's in a stopped (deallocated) state. So we'll start with that step, then move to the template deployment.

  • Step 1: Stop and deallocate your VM
    You can do this via the Azure portal, or use Azure CLI for faster scripting:

    az vm stop --resource-group <your-resource-group-name> --name <your-vm-name>
    az vm deallocate --resource-group <your-resource-group-name> --name <your-vm-name>
    
  • Step 2: Create or modify an ARM template
    You don't need a full template of your VM—just a simplified one targeting the hardwareProfile where the VM size is defined. Here's a minimal example:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "vmName": {
          "type": "string",
          "description": "Name of your existing VM"
        },
        "newVmSize": {
          "type": "string",
          "description": "New size for the VM (e.g., Standard_D2s_v3)"
        },
        "resourceGroupName": {
          "type": "string",
          "description": "Resource group containing the VM"
        }
      },
      "resources": [
        {
          "type": "Microsoft.Compute/virtualMachines",
          "apiVersion": "2023-07-01",
          "name": "[parameters('vmName')]",
          "location": "[resourceGroup().location]",
          "properties": {
            "hardwareProfile": {
              "vmSize": "[parameters('newVmSize')]"
            }
          }
        }
      ]
    }
    
  • Step 3: Deploy the template
    Use Azure CLI to deploy the update to your resource group:

    az deployment group create --resource-group <your-resource-group-name> --template-file ./update-vm-size.json --parameters vmName=<your-vm-name> newVmSize=<desired-size>
    
  • Step 4: Start your VM back up
    Once the deployment finishes, restart the VM:

    az vm start --resource-group <your-resource-group-name> --name <your-vm-name>
    
Using Azure .NET SDK to Update VM Size

If you prefer programmatic control via .NET, here's a step-by-step implementation:

  • Step 1: Install required NuGet packages
    You'll need these two packages for interacting with Azure Compute resources and authentication:

    Install-Package Azure.ResourceManager.Compute
    Install-Package Azure.Identity
    
  • Step 2: Write the code to update VM size
    This example uses DefaultAzureCredential for authentication (works for local development, Azure App Service, etc.). Make sure you have the appropriate permissions (like Virtual Machine Contributor) on the resource group.

    using Azure.Identity;
    using Azure.ResourceManager;
    using Azure.ResourceManager.Compute;
    using Azure.ResourceManager.Compute.Models;
    
    // Initialize the ArmClient with default authentication
    var armClient = new ArmClient(new DefaultAzureCredential());
    
    // Define your VM and resource group details
    string subscriptionId = "your-subscription-id";
    string resourceGroupName = "your-resource-group-name";
    string vmName = "your-vm-name";
    var desiredVmSize = VirtualMachineSizeTypes.StandardD2sV3; // Replace with your target size
    
    // Get references to the resource group and VM
    var resourceGroupId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId, resourceGroupName);
    var resourceGroup = await armClient.GetResourceGroupResource(resourceGroupId).GetAsync();
    var vm = await resourceGroup.GetVirtualMachineAsync(vmName);
    
    // Stop and deallocate the VM (required for size change)
    await vm.PowerOffAsync(WaitUntil.Completed);
    await vm.DeallocateAsync(WaitUntil.Completed);
    
    // Prepare the update payload with the new size
    var vmUpdate = new VirtualMachineUpdate
    {
        HardwareProfile = new VirtualMachineHardwareProfile
        {
            VmSize = desiredVmSize
        }
    };
    
    // Apply the update
    await vm.UpdateAsync(WaitUntil.Completed, vmUpdate);
    
    // Start the VM back up
    await vm.StartAsync(WaitUntil.Completed);
    
    Console.WriteLine("VM size updated successfully!");
    

Key Notes to Remember

  • Not all VM sizes are available in every region, or compatible with your VM's generation (Gen1 vs Gen2). Double-check availability before making changes.
  • Changing the VM size will cause downtime, so schedule this during a maintenance window if your VM is running production workloads.
  • Ensure your user/service principal has sufficient permissions to manage VMs in the target resource group.

内容的提问来源于stack exchange,提问作者DnyaneshSurya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 08:37:32