简体   繁体   English

通过 Terraform 创建 Azure Windows VM

[英]Creating a Azure Windows VM through Terraform

In Azure, I'm trying to create a Windows VM using Terraform.在 Azure 中,我尝试使用 Terraform 创建 Windows VM。 I have done this through Powershell previously using Template.json file.我以前使用 Template.json 文件通过 Powershell 完成了此操作。 Now I have to do with terraform, which I'm completely new to.现在我必须处理 terraform,我对它完全陌生。 So I have searched for some Sample scripts which creates VM in Azure and found this .所以我搜索了一些在 Azure 中创建 VM 的示例脚本并找到了这个

In this link, there is a sample Terraform script to spin a Linux VM.在此链接中,有一个用于旋转 Linux VM 的示例 Terraform 脚本。 But I need to spin a windows VM from an Image.但是我需要从图像旋转 Windows VM。 Where should I give the Image details.我应该在哪里提供图像详细信息。 My complete requirement is:我的完整要求是:

  1. Create a Windows VM from an Image (have resource Id)从映像创建 Windows VM(具有资源 ID)
  2. I already have Resource group, Virtual network, Subnet created.我已经创建了资源组、虚拟网络、子网。 I just need to pass those values and create them.我只需要传递这些值并创建它们。
  3. We have already defined the Subnet address prefix, Vnet address space from the portal itself.我们已经从门户本身定义了子网地址前缀、Vnet 地址空间。 So do I have to give again in the script or can I skip it.所以我必须在脚本中再次给出还是可以跳过它。
  4. The business requirement is that no VMs should have public IP and DNS name, So if I remove "# Create public IPs" section, will that not create public IP?业务要求是没有虚拟机应该有公共 IP 和 DNS 名称,所以如果我删除“#Create public IPs”部分,那不会创建公共 IP 吗?

The script for creating a Linux machine is here , which I'm taking it as reference.创建 Linux 机器的脚本在这里,我将其作为参考。

Below is an example of how to use data to use already existing resources in terraform, also there is a code block to create a windows VM.下面是如何使用数据使用 terraform 中现有资源的示例,还有一个代码块来创建 Windows VM。 You will need to get the existing VNET and create a NIC您需要获取现有 VNET 并创建 NIC

Use the data directive to get the VNET azurerm_virtual_network , you can see the syntax below for the resource group.使用 data 指令获取 VNET azurerm_virtual_network ,您可以看到以下资源组的语法。 You will need to add the resource group and possibly location into this block.您需要将资源组和可能的位置添加到此块中。

Create a azurerm_network_interface resource using the VNET ID使用 VNET ID 创建azurerm_network_interface资源

Add the network interface ID to the VM (network_interface_ids = [])将网络接口 ID 添加到 VM (network_interface_ids = [])

Example TF Code to Create and load balance VMs 用于创建和负载平衡 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 {
  }

}

From Terraform's perspective, a Windows VM is really quite similar to a Linux VM.从 Terraform 的角度来看,Windows VM 与 Linux VM 非常相似。 The #1 difference in my opinion is that the Windows VM requires a os_profile_windows_config attribute, while the Linux VM needs os_profile_linux_config .在我看来,#1 的区别是 Windows VM 需要os_profile_windows_config属性,而 Linux VM 需要os_profile_linux_config

The TF code you found on the Microsoft site is a fine start.您在 Microsoft 站点上找到的 TF 代码是一个好的开始。 Additionally, you may look in the Terraform Registry .此外,您可以查看Terraform Registry For example, here's a module for a Linux VM .例如,这里有一个 Linux VM 模块

I strongly recommend reading through all of the options in the VM resource .我强烈建议通读VM 资源中的所有选项。 I know it's a lot, but you should understand what choices you have.我知道很多,但你应该明白你有什么选择。

Lastly, there's no substitute for writing some code and testing it.最后,没有什么可以替代编写一些代码并对其进行测试。 If you do something wrong, either Terraform and/or the Azure API will tell you, and if it's unclear, a web search will pop up an answer or a pointer in the right direction.如果你做错了什么,Terraform 和/或 Azure API 会告诉你,如果不清楚,网络搜索会弹出一个答案或指向正确方向的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM