Terraform无法创建所需大小的Azure VM

bmp9r5qi  于 2022-11-25  发布在  其他
关注(0)|答案(1)|浏览(120)

我使用Terraform在Azure上创建虚拟机时遇到问题。
我们有一个策略限制为我们的订阅创建特定的虚拟机大小,但我们为特定的资源组创建了一个例外。
我可以使用ServicePrincipal和以下命令创建所需大小的虚拟机:

$ az login --service-principal -u ... -p ... --tenant ...

$ az vm create --resource-group ... --name ... --image ... --admin-username ... --generate-ssh-keys --location ... --size ...

已成功创建具有所需大小的虚拟机。
但是,当我尝试使用Terraform创建具有相同虚拟机大小的虚拟机时,出现以下错误:
级别=错误消息=错误:正在创建Linux虚拟机“...”(资源组“..”):compute.VirtualMachinesClient#CreateOrUpdate:发送请求失败:状态代码=0 --原始错误:Autorest/ Azure :服务返回错误。状态=代码=“SkuNotAvailable”消息=“对于订阅"...”,请求的资源“/subscriptions/.../resourceGroups/.../providers/Microsoft.Compute/virtualMachines/...”大小当前在位置“...”区域“..."中不可用。请尝试其他大小或部署到其他位置或区域。有关详细信息,请参阅https://aka.ms/azureskunotavailable。”
运行az vm list-skus --location ... --size ... --all --output table
所需大小的输出为:

restrictions
---
NotAvailableForSubscription, type: Zone, locations: ..., zones: 1,2,3

看起来大小不可用,但使用CLI或Azure门户,我可以创建此大小的虚拟机。
该平台使用与CLI命令相同的服务主体在相同的订阅、租户和资源组中运行。
您是否知道使用terraform创建VM时会出现此问题的原因?
谢谢

uqjltbpv

uqjltbpv1#

以下是用于创建具有指定配置的虚拟机的terraform脚本

位置=“美国东部”虚拟机大小=“标准_NC12s_v3”
步骤1:将以下代码复制到“main tf”文件中

provider "azurerm" {
 features {}
 }
variable "prefix" {
  default = "rg_swarna"
}
resource "azurerm_resource_group" "example" {
  name     = "${var.prefix}-resources"
  location = "East US"
}
resource "azurerm_virtual_network" "main" {
  name                = "${var.prefix}-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "internal" {
  name                 = "internal"
  resource_group_name  = "rg_swarna-resources"//azurerm_resource_group.example.name
  virtual_network_name = "rg_swarna-network"//azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
}
resource "azurerm_network_interface" "main" {
  name                = "${var.prefix}-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = azurerm_subnet.internal.id
    private_ip_address_allocation = "Dynamic"
  }
}
resource "azurerm_virtual_machine" "main" {
  name                  = "${var.prefix}-vm"
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.main.id]
  //vm_size               = "Standard_DS1_v2"
  vm_size               = "Standard_NC12s_v3"
  # Uncomment this line to delete the OS disk automatically when deleting the VM
  # delete_os_disk_on_termination = true

  # Uncomment this line to delete the data disks automatically when deleting the VM
  # delete_data_disks_on_termination = true

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }
  storage_os_disk {
    name              = "myosdisk1"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }
  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
  tags = {
    environment = "staging"
  }
}

第2步:运行以下命令

terraform plan 
terraform apply -auto-approve

第3步:在Azure Portal

中验证结果

相关问题