使用terraform错误从自定义图像创建azure vm



我需要使用Terraform从自定义映像在Azure中提供虚拟机,所有操作都可以与市场上的映像配合使用,但当我尝试指定自定义映像时,会返回错误。我整天都在为这个问题绞尽脑汁。这里是我的tf脚本:

resource "azurerm_windows_virtual_machine" "tftest" {
name                  = "myazurevm"  
location              = "eastus"
resource_group_name   = "myresource-rg"
network_interface_ids = [azurerm_network_interface.azvm1nic.id]
size               = "Standard_B1s"
storage_image_reference {
id = "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxx/providers/Microsoft.Compute/images/mytemplate"
}
storage_os_disk {
name              = "my-os-disk"
create_option     = "FromImage"
managed_disk_type = "Premium_LRS"
}
storage_data_disk {
name              = "my-data-disk"
managed_disk_type = "Premium_LRS"
disk_size_gb      = 75
create_option     = "FromImage"
lun               = 0
}
os_profile {
computer_name  = "myvmazure"
admin_username = "admin"
admin_password = "test123"
}
os_profile_windows_config {
provision_vm_agent = true
}
} 

这里是计划阶段返回的错误:

2020-07-17T20:02:26.9367986Z ==============================================================================
2020-07-17T20:02:26.9368212Z Task         : Terraform
2020-07-17T20:02:26.9368456Z Description  : Execute terraform commands to manage resources on AzureRM, Amazon Web Services(AWS) and Google Cloud Platform(GCP)
2020-07-17T20:02:26.9368678Z Version      : 0.0.142
2020-07-17T20:02:26.9368852Z Author       : Microsoft Corporation
2020-07-17T20:02:26.9369049Z Help         : [Learn more about this task](https://aka.ms/AA5j5pf)
2020-07-17T20:02:26.9369262Z ==============================================================================
2020-07-17T20:02:27.2826725Z [command]D:agent_work_toolterraform.12.3x64terraform.exe providers
2020-07-17T20:02:27.5303002Z .
2020-07-17T20:02:27.5304176Z └── provider.azurerm
2020-07-17T20:02:27.5304628Z 
2020-07-17T20:02:27.5363313Z [command]D:agent_work_toolterraform.12.3x64terraform.exe plan
2020-07-17T20:02:29.7685150Z [31m
2020-07-17T20:02:29.7788471Z [1m[31mError: [0m[0m[1mInsufficient os_disk blocks[0m
2020-07-17T20:02:29.7792789Z 
2020-07-17T20:02:29.7793007Z [0m  on  line 0:
2020-07-17T20:02:29.7793199Z   (source code not available)
2020-07-17T20:02:29.7793305Z 
2020-07-17T20:02:29.7793472Z At least 1 "os_disk" blocks are required.
2020-07-17T20:02:29.7793660Z [0m[0m
2020-07-17T20:02:29.7793800Z [31m
2020-07-17T20:02:29.7793975Z [1m[31mError: [0m[0m[1mMissing required argument[0m

您对查找该问题有什么建议吗?

我终于解决了这个问题。我使用了错误的地形资源:

wrong --> azurerm_windows_virtual_machine
correct -->  azurerm_virtual_machine

azurerm_windows_virtual_machine不支持(storage_os_disk,storage_data_disk(之类的参数,并且除非在共享图像库中发布图像,否则它不适合自定义图像。

有关每个提供程序支持的选项,请参阅文档:https://www.terraform.io/docs/providers/azurerm/r/virtual_machine.htmlhttps://www.terraform.io/docs/providers/azurerm/r/windows_virtual_machine.html

首先执行https://learn.microsoft.com/pt-br/azure/virtual-machines/windows/upload-generalized-managed?toc=%2Fazure%2Fvirtual-机器%2Fwindows%2Ftoc.json

比我的全鳕鱼

resource "azurerm_resource_group" "example" {
name     = "example-resources1"
location = "West Europe"
}
resource "azurerm_virtual_network" "example" {
name                = "example-network1"
address_space       = ["10.0.0.0/16"]
location            = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "example" {
name                 = "internal1"
resource_group_name  = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes     = ["10.0.2.0/24"]
}
resource "azurerm_network_interface" "example" {
name                = "example-nic1"
location            = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
ip_configuration {
name                          = "internal1"
subnet_id                     = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_virtual_machine" "example" {
name                = "example-machine1"
resource_group_name = azurerm_resource_group.example.name
location            = azurerm_resource_group.example.location
vm_size                = "Standard_B1s"
network_interface_ids = [
azurerm_network_interface.example.id,
]
storage_image_reference {
id = "/subscriptions/XXXXXXXXXXXXX/resourceGroups/ORIGEM/providers/Microsoft.Compute/images/myImage"
//just copi id from your image that you created
}
storage_os_disk {
name              = "my-os-disk"
create_option     = "FromImage"
managed_disk_type = "Premium_LRS"
}
os_profile {
computer_name  = "myvmazure"
admin_username = "adminusername"
admin_password = "testenovo@123"
}
os_profile_windows_config {
provision_vm_agent = true
}


}
//bellow the cod to call powershell o work extension,

resource "azurerm_virtual_machine_extension" "software" {
name                 = "install-software"
//resource_group_name  = azurerm_resource_group.example.name
virtual_machine_id   = azurerm_virtual_machine.example.id
publisher            = "Microsoft.Compute"
type                 = "CustomScriptExtension"
type_handler_version = "1.9"
protected_settings = <<SETTINGS
{
"commandToExecute": "powershell -encodedCommand ${textencodebase64(file("install.ps1"), "UTF-16LE")}"

}
SETTINGS
}

您可以使用带有"azurerm_windows_virtual_machine";模块设置";source_image_id";参数文献指出;必须设置source_image_id或source_image_reference之一"一个可以用于市场/画廊图像,另一个用于托管图像。

最新更新