Skip to content

Commit 075818c

Browse files
committed
fix: update terraform to create windows machines
1 parent 68b8119 commit 075818c

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

test_code/AZURE_MACHINES/main.tf

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ locals {
55
compute_instances = { for key, value in var.AZURE_MACHINE_CONFIGS :
66
key => value if contains(var.AZURE_COMPUTE_FILTER, key) || length(var.AZURE_COMPUTE_FILTER) == 0 }
77

8+
win_instances = { for key, value in var.AZURE_WIN_MACHINE_CONFIGS :
9+
key => value if contains(var.AZURE_COMPUTE_FILTER, key) || length(var.AZURE_COMPUTE_FILTER) == 0 }
10+
11+
combined_instances = merge(local.compute_instances, local.win_instances)
12+
813
}
914

1015
resource "azurerm_resource_group" "linux_host_test" {
@@ -18,7 +23,8 @@ resource "azurerm_linux_virtual_machine" "linux_host_test" {
1823
azurerm_network_interface_security_group_association.linux_host_test
1924
]
2025
for_each = local.compute_instances
21-
name = replace(format(var.name_format, "${each.key}-machine"), local.str_f, local.str_r)
26+
name = replace(format(var.name_format, "${each.key}-vm"), local.str_f, local.str_r)
27+
computer_name = each.key
2228
resource_group_name = azurerm_resource_group.linux_host_test.name
2329
location = azurerm_resource_group.linux_host_test.location
2430
size = each.value.machine_type
@@ -47,8 +53,33 @@ resource "azurerm_linux_virtual_machine" "linux_host_test" {
4753
custom_data = filebase64(each.value.user_data)
4854
}
4955

56+
resource "azurerm_windows_virtual_machine" "linux_host_test" {
57+
# https://azapril.dev/2020/05/12/terraform-depends_on/
58+
depends_on = [
59+
azurerm_network_interface_security_group_association.linux_host_test
60+
]
61+
for_each = local.win_instances
62+
name = replace(format(var.name_format, "${each.key}-machine"), local.str_f, local.str_r)
63+
resource_group_name = azurerm_resource_group.linux_host_test.name
64+
location = azurerm_resource_group.linux_host_test.location
65+
size = each.value.machine_type
66+
admin_username = each.value.default_user
67+
admin_password = each.value.default_password
68+
network_interface_ids = [
69+
azurerm_network_interface.linux_host_test[each.key].id,
70+
]
5071

72+
os_disk {
73+
caching = "ReadWrite"
74+
storage_account_type = "Standard_LRS"
75+
}
5176

77+
source_image_reference {
78+
publisher = each.value.source_image_reference.publisher
79+
offer = each.value.source_image_reference.offer
80+
sku = each.value.source_image_reference.sku
81+
version = each.value.source_image_reference.version
82+
}
5283

53-
54-
84+
custom_data = filebase64(each.value.user_data)
85+
}

test_code/AZURE_MACHINES/security_group.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Create public IPs
22
resource "azurerm_public_ip" "linux_host_test" {
3-
for_each = local.compute_instances
3+
for_each = local.combined_instances
44
name = format(var.name_format, "${each.key}_PublicIP")
55
location = azurerm_resource_group.linux_host_test.location
66
resource_group_name = azurerm_resource_group.linux_host_test.name
77
allocation_method = "Dynamic"
88
}
99

1010
resource "azurerm_network_interface" "linux_host_test" {
11-
for_each = local.compute_instances
11+
for_each = local.combined_instances
1212
name = format(var.name_format, "${each.key}_nic")
1313
location = azurerm_resource_group.linux_host_test.location
1414
resource_group_name = azurerm_resource_group.linux_host_test.name
@@ -42,7 +42,7 @@ resource "azurerm_network_security_group" "linux_host_test" {
4242

4343
# Connect the security group to the network interface
4444
resource "azurerm_network_interface_security_group_association" "linux_host_test" {
45-
for_each = local.compute_instances
45+
for_each = local.combined_instances
4646
network_interface_id = azurerm_network_interface.linux_host_test[each.key].id
4747
network_security_group_id = azurerm_network_security_group.linux_host_test.id
4848
}

test_code/AZURE_MACHINES/variables.tf

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# tflint-ignore: terraform_naming_convention
22
variable "AZURE_MACHINE_CONFIGS" {
3-
description = "variable for what compute instances to create"
3+
description = "variable for what linux compute instances to create"
44
type = map(any)
55
default = {
66
# https://az-vm-image.info/
@@ -87,21 +87,30 @@ variable "AZURE_MACHINE_CONFIGS" {
8787
sleep = 120
8888

8989
}
90-
WIN_10_ENT_21H2 = {
91-
recreate = "changethistorecreate1"
92-
machine_type = "Standard_DS1_v2"
93-
description = "Windows 10 Enterprise 21H2"
94-
default_user = "test-user"
95-
wait = "120"
96-
user_data = "user_data/windows.ps"
90+
}
91+
}
92+
93+
# tflint-ignore: terraform_naming_convention
94+
variable "AZURE_WIN_MACHINE_CONFIGS" {
95+
description = "variable for what linux compute instances to create"
96+
type = map(any)
97+
default = {
98+
# az vm image list --output table --all --publisher MicrosoftWindowsDesktop --sku win10-21h2-ent
99+
W10_ENT_21H2 = {
100+
recreate = "changethistorecreate1"
101+
machine_type = "Standard_DS1_v2"
102+
description = "Windows 10 Enterprise 21H2"
103+
default_user = "test-user"
104+
default_password = "km$3MWPf&i6r4o@I"
105+
wait = "120"
106+
user_data = "user_data/windows.ps"
97107
source_image_reference = {
98108
publisher = "MicrosoftWindowsDesktop"
99109
offer = "Windows-10"
100110
sku = "win10-21h2-ent-g2"
101111
version = "19044.3086.230609"
102112
}
103113
sleep = 120
104-
105114
}
106115
}
107116
}

0 commit comments

Comments
 (0)