[英]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:
我的完整要求是:
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.