遵循以下教程时,为什么会出现“未设置必填字段”的问题:使用Terraform和HCL创建Azure VM群集?

时间:2020-05-06 20:11:47

标签: azure terraform terraform-provider-azure

尝试首次使用terraform设置Azure vms-

https://docs.microsoft.com/en-us/azure/developer/terraform/create-vm-cluster-with-infrastructure

 resource "azurerm_resource_group" "test" {  name     = "acctestrg" 
 location = "West US 2" }

 resource "azurerm_virtual_network" "test" {  name                =
 "acctvn"  address_space       = ["10.0.0.0/16"]  location            =
 azurerm_resource_group.test.location  resource_group_name =
 azurerm_resource_group.test.name }

 resource "azurerm_subnet" "test" {  name                 = "acctsub" 
 resource_group_name  = azurerm_resource_group.test.name 
 virtual_network_name = azurerm_virtual_network.test.name 
 address_prefix       = "10.0.2.0/24" }

 resource "azurerm_public_ip" "test" {  name                         =
 "publicIPForLB"  location                     =
 azurerm_resource_group.test.location  resource_group_name          =
 azurerm_resource_group.test.name  allocation_method            =
 "Static" }

 resource "azurerm_lb" "test" {  name                = "loadBalancer" 
 location            = azurerm_resource_group.test.location 
 resource_group_name = azurerm_resource_group.test.name

  frontend_ip_configuration {    name                 =
 "publicIPAddress"    public_ip_address_id = azurerm_public_ip.test.id 
 } }

 resource "azurerm_lb_backend_address_pool" "test" { 
 resource_group_name = azurerm_resource_group.test.name 
 loadbalancer_id     = azurerm_lb.test.id  name                =
 "BackEndAddressPool" }

 resource "azurerm_network_interface" "test" {  count               = 2
 name                = "acctni${count.index}"  location            =
 azurerm_resource_group.test.location  resource_group_name =
 azurerm_resource_group.test.name

  ip_configuration {    name                          =
 "testConfiguration"    subnet_id                     =
 azurerm_subnet.test.id    private_ip_address_allocation = "dynamic"  }
 }

 resource "azurerm_managed_disk" "test" {  count                = 2 
 name                 = "datadisk_existing_${count.index}"  location   
 = azurerm_resource_group.test.location  resource_group_name  = azurerm_resource_group.test.name  storage_account_type =
 "Standard_LRS"  create_option        = "Empty"  disk_size_gb         =
 "1023" }

 resource "azurerm_availability_set" "avset" {  name                   
 = "avset"  location                     = azurerm_resource_group.test.location  resource_group_name          =
 azurerm_resource_group.test.name  platform_fault_domain_count  = 2 
 platform_update_domain_count = 2  managed                      = true
 }

 resource "azurerm_virtual_machine" "test" {  count                 = 2
 name                  = "acctvm${count.index}"  location             
 = azurerm_resource_group.test.location  availability_set_id   = azurerm_availability_set.avset.id  resource_group_name   =
 azurerm_resource_group.test.name  network_interface_ids =
 [element(azurerm_network_interface.test.*.id, count.index)]  vm_size  
 = "Standard_DS1_v2"

  # 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              = "myosdisk${count.index}"   
 caching           = "ReadWrite"    create_option     = "FromImage"   
 managed_disk_type = "Standard_LRS"  }

  # Optional data disks  storage_data_disk {    name              =
 "datadisk_new_${count.index}"    managed_disk_type = "Standard_LRS"   
 create_option     = "Empty"    lun               = 0    disk_size_gb  
 = "1023"  }

  storage_data_disk {    name            =
 element(azurerm_managed_disk.test.*.name, count.index)   
 managed_disk_id = element(azurerm_managed_disk.test.*.id, count.index)
 create_option   = "Attach"    lun             = 1    disk_size_gb    =
 element(azurerm_managed_disk.test.*.disk_size_gb, count.index)  }

  os_profile {    computer_name  = "hostname"    admin_username =
 "testadmin"    admin_password = "Password1234!"  }

  os_profile_linux_config {    disable_password_authentication = false 
 }

  tags = {    environment = "staging"  } }

仍然无法设置。

正在运行的Terraform计划始终会出现以下错误- Abhisheks-MBP:第一个Abhisheksingh $地形方案

错误:“功能”:未设置必填字段

正如许多堆栈溢出问题所建议的那样,我尝试添加具有空功能部件的提供程序块。我还尝试将版本固定为2.0.0。但是,每次下载2.8.0版,都会再次出现相同的错误。有人知道需要做什么吗?对于第一次尝试的人来说,这非常令人沮丧。它是一种什么样的教程,它使您第一次自己就很难挣扎。

Abhisheks-MBP:第一个abhisheksingh $ terraform -v Terraform v0.12.24 + provider.azurerm v2.8.0

我什至在tf文件中添加了以下部分-

Azure提供程序的“ azurerm”提供程序{version =“ = 2.0.0”功能{}}

但无济于事!

在遵循以下教程时,为什么会出现“未设置必填字段”的问题:使用Terraform和HCL创建Azure VM群集?

2 个答案:

答案 0 :(得分:1)

我可以确认本教程能够正常工作,我还匹配了Terraform和Azure提供程序版本:

Terraform v0.12.24
+ provider.azurerm v2.8.0

该错误将表明Terraform找不到指定的provider块,并且您提供的代码示例未设置一个,因此,假设您提供了整个main.tf文件,我建议添加以下块到您的main.tf文件顶部

provider "azurerm" {
  version = "2.8.0"
  features {}
}

启动main.tfterraform init时,请确保您位于terraform plan所在的目录中

我成功完成了以下main.tf

provider "azurerm" {
  version = "2.8.0"
  features {}
}

resource "azurerm_resource_group" "test" {
  name     = "acctestrg"
  location = "West US 2"
}

resource "azurerm_virtual_network" "test" {
  name                = "acctvn"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_subnet" "test" {
  name                 = "acctsub"
  resource_group_name  = azurerm_resource_group.test.name
  virtual_network_name = azurerm_virtual_network.test.name
  address_prefix       = "10.0.2.0/24"
}

resource "azurerm_public_ip" "test" {
  name                = "publicIPForLB"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
  allocation_method   = "Static"
}

resource "azurerm_lb" "test" {
  name                = "loadBalancer"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
  frontend_ip_configuration {
    name                 = "publicIPAddress"
    public_ip_address_id = azurerm_public_ip.test.id
  }
}

resource "azurerm_lb_backend_address_pool" "test" {
  resource_group_name = azurerm_resource_group.test.name
  loadbalancer_id     = azurerm_lb.test.id
  name                = "BackEndAddressPool"
}


resource "azurerm_network_interface" "test" {
  count               = 2
  name                = "acctni${count.index}"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
  ip_configuration {
    name                          = "testConfiguration"
    subnet_id                     = azurerm_subnet.test.id
    private_ip_address_allocation = "dynamic"
  }
}


resource "azurerm_managed_disk" "test" {
  count                = 2
  name                 = "datadisk_existing_${count.index}"
  location             = azurerm_resource_group.test.location
  resource_group_name  = azurerm_resource_group.test.name
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  disk_size_gb         = "1023"
}

resource "azurerm_availability_set" "avset" {
  name                         = "avset"
  location                     = azurerm_resource_group.test.location
  resource_group_name          = azurerm_resource_group.test.name
  platform_fault_domain_count  = 2
  platform_update_domain_count = 2
  managed                      = true
}


resource "azurerm_virtual_machine" "test" {
  count                 = 2
  name                  = "acctvm${count.index}"
  location              = azurerm_resource_group.test.location
  availability_set_id   = azurerm_availability_set.avset.id
  resource_group_name   = azurerm_resource_group.test.name
  network_interface_ids = [element(azurerm_network_interface.test.*.id, count.index)]
  vm_size               = "Standard_DS1_v2"
  # 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              = "myosdisk${count.index}"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }
  # Optional data disks  
  storage_data_disk {
    name              = "datadisk_new_${count.index}"
    managed_disk_type = "Standard_LRS"
    create_option     = "Empty"
    lun               = 0
    disk_size_gb      = "1023"
  }
  storage_data_disk {
    name            = element(azurerm_managed_disk.test.*.name, count.index)
    managed_disk_id = element(azurerm_managed_disk.test.*.id, count.index)
    create_option   = "Attach"
    lun             = 1
    disk_size_gb    = element(azurerm_managed_disk.test.*.disk_size_gb, count.index)
  }
  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
  tags = { environment = "staging" }
}

确保使用terraform init下载正确的提供程序版本。

希望这会有所帮助。

答案 1 :(得分:0)

您并不孤单,我遇到了同样的错误...
如果我只是复制粘贴该教程中的代码,则会得到:
Error: "features": required field is not set

我看到的是它没有提供者块,应该是这样的:

provider "azurerm" { 
    # The "feature" block is required for AzureRM provider 2.x. 
    # If you are using version 1.x, the "features" block is not allowed.
    version = "~>2.0"
    features {}
}

其他教程对此进行了介绍:

https://docs.microsoft.com/en-us/azure/developer/terraform/create-vm-cluster-module#set-up-authentication-with-azure

有根据的猜测,没有人真正在复习那些教程。
您所能做的就是给他们留言,让他们知道...