-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
137 lines (129 loc) · 4.67 KB
/
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
resource "random_pet" "main" {
length = 2
separator = ""
}
resource "azurerm_resource_group" "main" {
name = var.resource_group_name
location = var.location
}
resource "azurerm_virtual_network" "main" {
depends_on = [ azurerm_resource_group.main ]
name = random_pet.main.id
location = var.location
resource_group_name = var.resource_group_name
address_space = ["10.0.0.0/16"]
}
resource "azurerm_subnet" "main" {
depends_on = [ azurerm_resource_group.main ]
name = random_pet.main.id
resource_group_name = var.resource_group_name
virtual_network_name = azurerm_virtual_network.main.name
address_prefixes = ["10.0.0.0/24"]
}
resource "azurerm_public_ip" "main" {
depends_on = [ azurerm_resource_group.main ]
name = random_pet.main.id
resource_group_name = var.resource_group_name
location = var.location
allocation_method = "Static"
sku = "Standard"
}
resource "azurerm_network_interface" "main" {
name = random_pet.main.id
location = var.location
resource_group_name = var.resource_group_name
ip_configuration {
name = random_pet.main.id
subnet_id = azurerm_subnet.main.id
private_ip_address_allocation = "Static"
private_ip_address = "10.0.0.5"
public_ip_address_id = azurerm_public_ip.main.id
}
}
resource "azurerm_network_security_group" "main" {
name = random_pet.main.id
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
security_rule {
name = "AllowRDP"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "AllowWinRM"
priority = 1002
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "5985"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
resource "azurerm_network_interface_security_group_association" "main" {
network_interface_id = azurerm_network_interface.main.id
network_security_group_id = azurerm_network_security_group.main.id
}
resource "azurerm_windows_virtual_machine" "main" {
name = random_pet.main.id
location = var.location
resource_group_name = var.resource_group_name
size = var.vm_size
admin_username = var.local_admin_username
admin_password = var.local_admin_password
network_interface_ids = [
azurerm_network_interface.main.id
]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
name = random_pet.main.id
}
source_image_reference {
publisher = var.vm_image_publisher
offer = var.vm_image_offer
sku = var.vm_image_sku
version = var.vm_image_version
}
computer_name = var.computer_name
timezone = "UTC"
additional_unattend_content {
setting = "AutoLogon"
content = <<-EOF
<AutoLogon>
<Enabled>true</Enabled>
<Username>${var.local_admin_username}</Username>
<Password>
<Value>${var.local_admin_password}</Value>
</Password>
<LogonCount>1</LogonCount>
</AutoLogon>
EOF
}
additional_unattend_content {
setting = "FirstLogonCommands"
content = <<-EOF
<FirstLogonCommands>
<SynchronousCommand>
<Order>1</Order>
<Description>Set PowerShell Execution Policy</Description>
<RequiresUserInput>false</RequiresUserInput>
<CommandLine>powershell.exe -Command "Set-ExecutionPolicy -ExecutionPolicy Bypass -Force"</CommandLine>
</SynchronousCommand>
<SynchronousCommand>
<Order>2</Order>
<Description>Configure WinRM</Description>
<RequiresUserInput>false</RequiresUserInput>
<CommandLine>powershell.exe -EncodedCommand ${textencodebase64(file("${path.module}/Enable-WinRM.ps1"), "UTF-16LE")}</CommandLine>
</SynchronousCommand>
</FirstLogonCommands>
EOF
}
}