使用 Terraform Azure 的多个 VM 和磁盘
Posted
技术标签:
【中文标题】使用 Terraform Azure 的多个 VM 和磁盘【英文标题】:Multiple VMs & Disks Using Terraform Azure 【发布时间】:2021-05-30 20:09:33 【问题描述】:我有一个要求,我需要创建多个 Azure Windows VM,每个磁盘具有不同的大小,并且每个 VM 都放置在不同的 AZ 中(因此,如果我传入 3 个 vm 名称,每个都将转到不同的可用区以及磁盘)。
我有一些东西可以处理相同大小的磁盘,但当我需要分配不同大小的磁盘时却不行。
提前感谢您的帮助。
到目前为止我的代码:
variable "instances"
description = "No. of instances"
type = list(string)
default = ["vm-1", "vm-2"]
variable "nb_disks_per_instance"
description = "No. disks per vm"
type = number
default = 2
variable "disks"
description = "disk sizes"
type = list(number)
default = [10, 20, 30]
locals
vm_datadiskdisk_count_map = for k in toset(var.instances) : k => var.nb_disks_per_instance
luns = for k in local.datadisk_lun_map : k.datadisk_name => k.lun
datadisk_lun_map = flatten([
for vm_name, count in local.vm_datadiskdisk_count_map : [
for i in range(count) :
datadisk_name = format("%s_Data_Disk_%02d", vm_name, i)
lun = i
]
])
output "myMap"
value = local.datadisk_lun_map
resource "azurerm_managed_disk" "managed_disk"
for_each = toset([for j in local.datadisk_lun_map : j.datadisk_name])
name = replace(each.key, "_", "-")
location = "UK South"
resource_group_name = "rg-one"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = 20
tags = local.common_tags
zones = ["1"]
resource "azurerm_virtual_machine_data_disk_attachment" "managed_disk_attach"
for_each = toset([for j in local.datadisk_lun_map : j.datadisk_name])
managed_disk_id = azurerm_managed_disk.managed_disk[each.key].id
virtual_machine_id = azurerm_windows_virtual_machine.vm[element(split("_", each.key), 0)].id
lun = lookup(local.luns, each.key)
caching = "ReadWrite"
resource "azurerm_network_interface" "nic"
for_each = toset(var.instances)
location = "UK South"
name = "$each.key-NIC"
resource_group_name = "rg-one"
ip_configuration
name = "internal"
private_ip_address_allocation = "Dynamic"
subnet_id = data.azurerm_subnet.subnet.id
tags = local.common_tags
resource "azurerm_windows_virtual_machine" "vm"
for_each = toset(var.instances)
admin_password = <ENTER PW>
admin_username = "test"
location = "UK South"
name = each.key
network_interface_ids = [azurerm_network_interface.nic[format("%s", each.key)].id]
resource_group_name = "rg-one"
size = "Standard_D4s_v4"
tags = local.common_tags
zone = index(var.instances, each.value) + 1
os_disk
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
name = "$each.key-OS-DISK-00"
【问题讨论】:
关于这个问题的任何更新?它能解决你的问题吗? 【参考方案1】:以您的方式,创建一个具有多个磁盘的 VM 很容易,但创建多个具有不同大小和数量的磁盘的 VM 却很困难。最好的方法是使用 Terraform 模块。
该模块类似于您创建一个具有多个磁盘的虚拟机的方式,因此我将仅向您展示如何使用该模块:
variable "vms"
type = map(object(
size = string
admin_user = string
admin_password = string
disks = list(number)
))
module "vms"
for_each = var.vms
source = "./modules/vm"
resource_group_name = var.rg_name
vm_name = each.key
vm = each.value
disks = each.value["disks"]
location = var.location
terraform.tfvar
rg_name = "charlesVMs"
location = "East US"
vms =
azurevm1 =
size = "Standard_DS1_v2"
admin_user = "azureuser"
admin_password = "azureuser@2021"
disks = [30, 30]
这个例子只是为你提供了一个解决你的需求的方向,获取更多关于另一个issue的细节。如果您对此问题有任何疑问,请告诉我,我愿意为您提供帮助。
【讨论】:
以上是关于使用 Terraform Azure 的多个 VM 和磁盘的主要内容,如果未能解决你的问题,请参考以下文章
创建 azure vm 时,terraform 抛出“无效或未知的密钥:区域”
使用 Terraform 将自定义 DNS 服务器 IP 添加到 Azure VM NIC