下面是一个如何使用数据来使用 terraform 中现有资源的示例,还有一个创建 windows VM 的代码块。您将需要获取现有的 VNET 并创建一个 NIC
使用 data 指令获取 VNET azurerm_virtual_network
,您可以看到以下资源组的语法。您需要将资源组和可能的位置添加到此块中。
azurerm_network_interface
使用 VNET ID创建资源
将网络接口 ID 添加到 VM (network_interface_ids = [])
用于创建和负载平衡 VM 的示例 TF 代码
variable "subscription_id" {}
variable "client_id" {}
variable "client_secret" {}
variable "tenant_id" {}
provider "azurerm" {
tenant_id = "${var.tenant_id}"
subscription_id = "${var.subscription_id}"
client_id = "${var.client_id}"
client_secret = "${var.client_secret}"
}
data "azurerm_resource_group" "resource_group" {
name = "learning-tf-web-rg"
}
resource "azurerm_virtual_machine" "web_server" {
name = "server"
location = "westus2"
resource_group_name = "${data.azurerm_resource_group.resource_group.name}"
network_interface_ids = []
vm_size = "Standard_B2s"
storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter-Server-Core-smalldisk"
version = "latest"
}
storage_os_disk {
name = "server-os"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "server"
admin_username = "server"
admin_password = "Passw0rd1234"
}
os_profile_windows_config {
}
}