‘2124, 344 PM (Quickstart Use Teraform to create @ Windows VM - Azure Virtual Machines | Microsoft Leam
Quickstart: Use Terraform to create a
Windows VM
Article + 07/21/2023
© Al-assisted content. This article was partially created with the help of Al. An author reviewed and
revised the content as needed. Learn more
Applies to: / Windows VMs
This article shows you how to create a complete Windows environment and supporting
resources with Terraform. Those resources include a virtual network, subnet, public IP
address, and more.
Terraform enables the definition, preview, and deployment of cloud infrastructure. Using
Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to
specify the cloud provider - such as Azure - and the elements that make up your cloud
infrastructure. After you create your configuration files, you create an execution plan that
allows you to preview your infrastructure changes before they're deployed. Once you verify
the changes, you apply the execution plan to deploy the infrastructure.
In this article, you learn how to:
Create a random value for the Azure resource group name using random_pet
Create an Azure resource group using azurerm_resource_group
Create a virtual network (VNET) using azurerm_virtual_network
Create a subnet using azurerm_subnet
Create a public IP using azurerm_public_ip
Create a network security group using azurerm_network_security_ group
Create a network interface using azurerm_network interface
KSEE KEK
Create an association between the network security group and the network interface
using azurerm_network_interface_security_group_association
Generate a random value for a unique storage account name using random _id
Create a storage account for boot diagnostics using azurerm_storage_account
Create a Windows VM with an IIS web server using
466
azurerm_windows_virtual_machine
VY Create a Windows VM extension using azurerm_virtual_machine_extension
Prerequisites
ntips:leam microsoft comlen-usfazurevitual-machinesiwindowsiqulck-ceate-terratorm sno‘i204, 34 Pm ‘uickstat: Uso Terafom to croato a Windows VM- Azure Vitual Machines | Microsoft Leam
‘* Azure subscription: If you don’t have an Azure subscription, create a free account
before you begin.
‘Install and configure Terraform
Implement the Terraform code
O Note
The sample code for this article is located in the Azure Terraform GitHub repo. You
can view the log file containing the test results from current and previous versions of
Terraform
See more articles and sample code showing how to use Terraform to manage Azure
resources
1. Create a directory in which to test the sample Terraform code and make it the current
directory.
2. Create a file named providers.t# and insert the following code:
Terraform
terraform {
required_version
required_providers {
azurerm = {
source
"hashicorp/azurerm"
version = "~>3.0"
y
random = {
source = "hashicorp/random"
version = "~>3.0"
?
3
y
provider “azurerm™ {
features {}
}
3. Create a file named main.tf and insert the follo\
ntips:leam microsoft comlen-usfazurevitual-machinesiwindowsiqulck-ceate-terratorm
20‘uozi2a, 3:44 PM
uicks
Use Terratorm to era
Windows VM- Azure Virtual Machines | Microsoft Learn
Terraform
resource “azurerm_resource_group" “rg” {
location = var.resource_group_location
name = "${randon_pet prefix. id}-re'
# Create virtual network
resource “azurerm_virtual_network"
iny_terraform_network" {
name "$({random_pet. prefix.id}-vnet™
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
?
# Create subnet
resource “azurerm_subnet" "ny_terraform_subnet” {
name $(random_pet .prefix.id}-subnet
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.my_terraforn_network.name
address_prefixes = ["10.0.1.0/24"]
}
# Create public IPs
resource “azurerm_public_ip" “ny_terraform_public_ip" {
name *${randon_pet..prefix.id}-public- ip
location = azurerm_resource_group.rg-location
resource_group_nane = azurerm_resource_group.rg-nane
allocation_method =
y
# Create Network Security Group and rules
resource “azurerm_network_security group” “my_terraform_nsg" {
name "$(random_pet. prefix. id)-nsg'
location = azurerm_resource_group.rg. location
resource_group_name = azurerm_resource_group.rg.name
security rule {
name DF
priority = 1000
direction "Inbound"
access = "Allow"
protocol ="
source_port_range
destination_port_range
source_address_prefix
destination_address_prefix
+
security_rule {
name web"
priority 1001
3380"
hntps:ileam ierosof. conver-us/azurelirtualmachinesiwindowsiquickcreate-teraform
3it0122124, 344 PM ‘Quickstart Use Terraform to erat
direction =
access
protocol
source_port_range
destination_port_range
source_address_prefix
destination_address_prefix
Windows VM - Azure Virtual Machines | Microsoft Learn
abound”
# Create network interface
resource “azurerm_network_interface" "ny terraform_ni
name “$(random_pet.prefix.id}-nic
location = azurerm_resource_group. rg. location
Pesource_group_nane = azurerm_resource_group.rg.nane
ip_configuration {
subnet_id
private_ip_address_allocation
public_ip_address_id -
azurerm_public_ip.my_terraform_public_ip.id
3
+
iy_nic_configuratio
azurerm_subnet.my_terraform_subnet.id
“Dynamic”
# Connect the security group to the network interface
resource “azurerm_network_interface_security_group_association"
t
network_interface_id
azurerm_network_interface.my_terraform_nic.id
network_security_group_id
azurerm_network_security_group.my_terraform_nsg.id
y
example"
# Create storage account for boot diagnostics
resource “azurerm_storage_account™ "ny_storage_account™ {
name diags{random_id.randon_id.hex}"
location azurerm_resource_group.rg.location
resource group_nane = azurerm_resource_group.rg.nane
account_tier = "Standard”
account_replication_type = "LRS"
# Create virtual machine
resource “azurerm_windows_virtual_machine”
name = "${var.prefix}-vm'
admin_usernane = “azureuser"
admin_password random_password.password.result
location azurerm_resource_group.rg. location
resource group_name = azurerm_resource_group.rg.name
ntips:leam microsot.comlen-ustazurevitual-machinesiwindowsiqulck-create-terratorm 40‘uozi2a, 3:44 PM
‘Quickstart: Use Teraform to create a Windows VM - Azure Virtual Machines | Mierosoft Leam
network_interface_ids = [azurerm_network_interface.my_terraform_nic.id]
size "standard_pS1_v2"
os_disk {
name
caching
storage_account_type
+
source_image_reference {
publisher = "MicrosoftWindowsServer”
offer “WindowsServer”
sku "2022-datacenter-azure-edition
version = “latest”
3
boot_diagnostics {
storage_account_uri
azurerm_storage_account.my_storage_account.primary_blob_endpoint
+
}
# Install IIS web server to the virtual machine
resource “azurerm_virtual_machine_extension" "web_server_install” {
name ‘$(randon_pet.pretix.id)-wsi
virtual_machine_id = azurerm_windows_virtual_machine.main.id
publisher Nicrosoft.Conpute”
type ‘CustonScriptéxtension
type_handler_version 1.8"
auto_upgrade_minor_version = true
settings = <