【问题标题】:Terraform can't create Azure VM with wanted sizeTerraform 无法创建具有所需大小的 Azure VM
【发布时间】:2022-11-22 15:52:10
【问题描述】:

我在使用 Terraform 在 Azure 上创建 VM 时遇到问题。

我们有一项政策限制为我们的订阅创建特定的虚拟机大小,但我们为特定的资源组创建了一个豁免。

我可以使用我的 ServicePrincipal 和以下命令创建具有所需大小的 VM:

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

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

已成功创建具有所需大小的 VM。

但是,当我尝试使用 Terraform 创建具有相同 VM 大小的 VM 时,出现以下错误:

level=error msg=错误:创建 Linux 虚拟机“...”(资源组“...”):compute.VirtualMachinesClient#CreateOrUpdate:发送请求失败:StatusCode=0 -- 原始错误:autorest/azure:服务已返回一个错误。 Status= Code="SkuNotAvailable" Message="资源'/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 门户,我能够创建具有此大小的 VM。

terraform 在相同的订阅、租户和资源组中使用与 CLI 命令相同的服务主体运行。

您是否知道什么会导致使用 terraform 创建 VM 时出现此问题?

谢谢

【问题讨论】:

  • 您使用的是哪个提供商版本?
  • 您能否提及您要寻找的“SkuNotAvailable”尺寸和位置?
  • @SwarnaAnipindi 大小是 Standard_NC12s_v3 和 eastus 地区
  • 据我了解,如果您能够在订阅“xxxxx”上从 CLI 创建 NCv3 系列 VM,那么使用 Terraform 也同样适用。在 Terraform 端,请检查您是否有正确的订阅。如果没有,请运行此命令。 az account set --subscription "XXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX- 如果不起作用,请提供任何屏幕截图;这将有助于复制问题。区域 "Standard_NC12s_v3 "在美国东部地区不可用。
  • 复制场景“具有“Standard_NC12s_v3”和美国东部地区的 VM 区域”并提供代码库。谢谢你。

标签: azure terraform


【解决方案1】:

这是用于创建具有指定配置的 VM 的 terraform 脚本

位置=“美国东部” vm_size = "Standard_NC12s_v3"

第一步:将以下代码复制到“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

第三步: 在 Azure 门户中验证结果

【讨论】:

    猜你喜欢
    • 2020-03-21
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 2018-10-21
    相关资源
    最近更新 更多