@@ -44,6 +44,19 @@ data "coder_parameter" "location" {
44
44
}
45
45
}
46
46
47
+ data "coder_parameter" "data_disk_size" {
48
+ description = " Size of your data (F:) drive in GB"
49
+ display_name = " Data disk size"
50
+ name = " data_disk_size"
51
+ default = 20
52
+ mutable = " false"
53
+ type = " number"
54
+ validation {
55
+ min = 5
56
+ max = 5000
57
+ }
58
+ }
59
+
47
60
resource "coder_agent" "main" {
48
61
arch = " amd64"
49
62
auth = " azure-instance-identity"
@@ -52,34 +65,38 @@ resource "coder_agent" "main" {
52
65
login_before_ready = false
53
66
}
54
67
68
+ resource "random_password" "admin_password" {
69
+ length = 16
70
+ special = true
71
+ # https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/password-must-meet-complexity-requirements#reference
72
+ # we remove characters that require special handling in XML, as this is how we pass it to the VM
73
+ # namely: <>&'"
74
+ override_special = " ~!@#$%^*_-+=`|\\ (){}[]:;,.?/"
75
+ }
76
+
55
77
locals {
56
- prefix = " spike "
78
+ prefix = " coder-win "
57
79
admin_username = " coder"
58
- # Password to log in via RDP
59
- #
60
- # Must meet Windows password complexity requirements:
61
- # https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/password-must-meet-complexity-requirements#reference
62
- admin_password = " coderRDP!"
63
80
}
64
81
65
82
resource "azurerm_resource_group" "main" {
66
- name = " ${ local . prefix } -${ data . coder_workspace . me . name } -resources "
83
+ name = " ${ local . prefix } -${ data . coder_workspace . me . id } "
67
84
location = data. coder_parameter . location . value
68
85
tags = {
69
86
Coder_Provisioned = " true"
70
87
}
71
88
}
72
89
73
90
// Uncomment here and in the azurerm_network_interface resource to obtain a public IP
74
- resource "azurerm_public_ip" "main" {
75
- name = " publicip"
76
- resource_group_name = azurerm_resource_group. main . name
77
- location = azurerm_resource_group. main . location
78
- allocation_method = " Static"
79
- tags = {
80
- Coder_Provisioned = " true"
81
- }
82
- }
91
+ # resource "azurerm_public_ip" "main" {
92
+ # name = "publicip"
93
+ # resource_group_name = azurerm_resource_group.main.name
94
+ # location = azurerm_resource_group.main.location
95
+ # allocation_method = "Static"
96
+ # tags = {
97
+ # Coder_Provisioned = "true"
98
+ # }
99
+ # }
83
100
resource "azurerm_virtual_network" "main" {
84
101
name = " network"
85
102
address_space = [" 10.0.0.0/24" ]
@@ -104,7 +121,7 @@ resource "azurerm_network_interface" "main" {
104
121
subnet_id = azurerm_subnet. internal . id
105
122
private_ip_address_allocation = " Dynamic"
106
123
// Uncomment for public IP address as well as azurerm_public_ip resource above
107
- public_ip_address_id = azurerm_public_ip. main . id
124
+ # public_ip_address_id = azurerm_public_ip.main.id
108
125
}
109
126
tags = {
110
127
Coder_Provisioned = " true"
@@ -133,14 +150,14 @@ resource "azurerm_managed_disk" "data" {
133
150
resource_group_name = azurerm_resource_group. main . name
134
151
storage_account_type = " Standard_LRS"
135
152
create_option = " Empty"
136
- disk_size_gb = 20
153
+ disk_size_gb = data . coder_parameter . data_disk_size . value
137
154
}
138
155
139
156
# Create virtual machine
140
157
resource "azurerm_windows_virtual_machine" "main" {
141
158
name = " vm"
142
159
admin_username = local. admin_username
143
- admin_password = local . admin_password
160
+ admin_password = random_password . admin_password . result
144
161
location = azurerm_resource_group. main . location
145
162
resource_group_name = azurerm_resource_group. main . name
146
163
network_interface_ids = [azurerm_network_interface . main . id ]
@@ -160,7 +177,7 @@ resource "azurerm_windows_virtual_machine" "main" {
160
177
version = " latest"
161
178
}
162
179
additional_unattend_content {
163
- content = " <AutoLogon><Password><Value>${ local . admin_password } </Value></Password><Enabled>true</Enabled><LogonCount>1</LogonCount><Username>${ local . admin_username } </Username></AutoLogon>"
180
+ content = " <AutoLogon><Password><Value>${ random_password . admin_password . result } </Value></Password><Enabled>true</Enabled><LogonCount>1</LogonCount><Username>${ local . admin_username } </Username></AutoLogon>"
164
181
setting = " AutoLogon"
165
182
}
166
183
additional_unattend_content {
@@ -175,9 +192,41 @@ resource "azurerm_windows_virtual_machine" "main" {
175
192
}
176
193
}
177
194
195
+ resource "coder_metadata" "rdp_login" {
196
+ resource_id = azurerm_windows_virtual_machine. main . id
197
+ item {
198
+ key = " Username"
199
+ value = local. admin_username
200
+ }
201
+ item {
202
+ key = " Password"
203
+ value = random_password. admin_password . result
204
+ sensitive = true
205
+ }
206
+ }
207
+
178
208
resource "azurerm_virtual_machine_data_disk_attachment" "main_data" {
179
209
managed_disk_id = azurerm_managed_disk. data . id
180
210
virtual_machine_id = azurerm_windows_virtual_machine. main . id
181
211
lun = " 10"
182
212
caching = " ReadWrite"
183
213
}
214
+
215
+ # Stop the VM
216
+ resource "null_resource" "stop_vm" {
217
+ count = data. coder_workspace . me . transition == " stop" ? 1 : 0
218
+ depends_on = [azurerm_windows_virtual_machine . main ]
219
+ provisioner "local-exec" {
220
+ # Use deallocate so the VM is not charged
221
+ command = " az vm deallocate --ids ${ azurerm_windows_virtual_machine . main . id } "
222
+ }
223
+ }
224
+
225
+ # Start the VM
226
+ resource "null_resource" "start" {
227
+ count = data. coder_workspace . me . transition == " start" ? 1 : 0
228
+ depends_on = [azurerm_windows_virtual_machine . main ]
229
+ provisioner "local-exec" {
230
+ command = " az vm start --ids ${ azurerm_windows_virtual_machine . main . id } "
231
+ }
232
+ }
0 commit comments