Home > Net >  How to create Azure Windows VM using Terraform?
How to create Azure Windows VM using Terraform?

Time:02-01

I have environment in Azure with VNet configuration and resources. I wanto automate the deployment of Azure Windows VM with existing vnet configuration using Terraform.

CodePudding user response:

here is the sample code from official site.

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_windows_virtual_machine" "example" {
  name                = "example-machine"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  size                = "Standard_F2"
  admin_username      = "adminuser"
  admin_password      = "******"
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "MicrosoftWindows"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter"
    version   = "latest"
  }
}

upon running plan and apply you can see the VM in portal.

CodePudding user response:

Firstofall, if you use the existing vnet configuration in the terraform code, it will throw the error code: "Resource already exists". To resolve this, one can run terraform import to import this resource into the "terraform state file" and let terraform import it into the current executable file.

enter image description here

terraform import from terraform registry:

terraform import azurerm_virtual_network.existing /subscriptions/<subscriptionID>/resourceGroups/<resourcegroup>/providers/Microsoft.Network/virtualNetworks/<vnet>

enter image description here

When trying to deploy existing resources in Terraform, you should include a data block. Check the complete script below for your issue, and it was successfully deployed as follows.

data "azurerm_virtual_network" "existing"{
name = "jahnavivnet"
resource_group_name = "example-resources"
} 
provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}

resource "azurerm_virtual_network" "existing" {
  name                = "jahnavivnet"
  address_space       = ["10.0.0.0/16"]
  resource_group_name = "example-resources"
  location = "West Europe"
}

resource "azurerm_subnet" "existing" {
  name                 = "default"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.existing.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "example" {
  name                = "NICxxx"
  location            = "West Europe"
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "default"
    subnet_id                     = azurerm_subnet.existing.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_windows_virtual_machine" "example" {
  name                = "xxxxxnameofVM"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  size                = "Standard_F2"
  admin_username      = "user"
  admin_password      = "xxxx"
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2019-Datacenter"
    version   = "latest"
  }
}

Executed terraform init:

enter image description here

Executed terraform plan:

enter image description here

Executed terraform apply --auto-approve:

enter image description here

Deployed virtual machine with the existing virtual network configuration.

enter image description here

Refer terraform templates.

  • Related