Creating a Azure Windows VM through terraform

若如初见. 提交于 2019-12-11 04:07:56

问题


In Azure , I'm trying to create a Windows VM using terraform.
I have done this through powershell previously using Template.json file. Now I have to do with terraform, which I'm completely new to.
So I have searched for some Sample scripts which creates VM in Azure and found this.

In this link , there is a sample terraform script to spin a linux VM. But I need to spin a windows VM from an Image. Where should I give the Image details.
My complete requirement is :
1. Create a Windows VM from an Image (have resource 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 itslef. So do I have to give again in the script or can I skip it.
4. 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?

The script for creating a linux machine is here , which I'm taking it as reference.

Please forgive me if the questions I have asked is completely lame, but I need to know these details.
Can anyone help me / guide me the correct way please!


回答1:


From Terraform's perspective, a Windows VM is really quite similar to a 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.

The TF code you found on the Microsoft site is a fine start. Additionally, you may look in the Terraform Registry. For example, here's a module for a Linux VM.

I strongly recommend reading through all of the options in the VM resource. 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.

Good luck!




回答2:


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. You will need to get the existing VNET and create a NIC

Use the data directive to get the VNET azurerm_virtual_network, you can see the syntax below for the resource group. You will need to add the resource group and possibly location into this block.

Create a azurerm_network_interface resource using the VNET ID

Add the network interface ID to the VM (network_interface_ids = [])

Example TF Code to Create and load balance VMs

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 {
  }

}


来源:https://stackoverflow.com/questions/52419444/creating-a-azure-windows-vm-through-terraform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!