-
Notifications
You must be signed in to change notification settings - Fork 2
/
windows-vm-ss-main.tf
119 lines (101 loc) · 3.91 KB
/
windows-vm-ss-main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#################################
## Windows VM Scale Set - Main ##
#################################
# Bootstrapping Script
data "template_file" "tf-script" {
template = file("setup.ps1")
}
# Create Network Security Group to Access VM from Internet
resource "azurerm_network_security_group" "web-nsg" {
name = "${var.prefix}-${var.environment}-web-nsg"
location = azurerm_resource_group.network-rg.location
resource_group_name = azurerm_resource_group.network-rg.name
security_rule {
name = "AllowRDP"
description = "Allow RDP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "Internet"
destination_address_prefix = "*"
}
security_rule {
name = "AllowHTTP"
description = "Allow HTTP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefix = "Internet"
destination_address_prefix = "*"
}
security_rule {
name = "AllowHTTPS"
description = "Allow HTTPS"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "443"
source_address_prefix = "Internet"
destination_address_prefix = "*"
}
tags = {
application = var.app_name
environment = var.environment
}
}
# Associate the NSG with the Subnet
resource "azurerm_subnet_network_security_group_association" "web-nsg-association" {
subnet_id = azurerm_subnet.vm-subnet.id
network_security_group_id = azurerm_network_security_group.web-nsg.id
}
# Create Windows Virtual Machine Scale Set
resource "azurerm_windows_virtual_machine_scale_set" "web-ss" {
name = "${var.prefix}-${var.environment}-web-ss"
location = azurerm_resource_group.network-rg.location
resource_group_name = azurerm_resource_group.network-rg.name
sku = var.windows-vm-size
instances = 1
computer_name_prefix = var.windows-vm-hostname
admin_username = var.windows-admin-username
admin_password = var.windows-admin-password
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = var.windows-2019-sku
version = "latest"
}
os_disk {
storage_account_type = "Standard_LRS"
caching = "ReadWrite"
}
network_interface {
name = "${var.windows-vm-hostname}-network"
primary = true
ip_configuration {
name = "${var.windows-vm-hostname}-internal"
primary = true
subnet_id = azurerm_subnet.vm-subnet.id
}
}
extension {
name = "CustomScript"
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.10"
auto_upgrade_minor_version = true
settings = jsonencode({ "commandToExecute" = "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.tf-script.rendered)}')) | Out-File -filepath setup.ps1\" && powershell -ExecutionPolicy Unrestricted -File setup.ps1" })
protected_settings = jsonencode({ "managedIdentity" = {} })
}
tags = {
application = var.app_name
environment = var.environment
}
}