Skip to content

Commit 0cbdfb5

Browse files
committed
chore: unify azure-linux and azure-windows templates
1 parent 95d769d commit 0cbdfb5

File tree

4 files changed

+65
-28
lines changed

4 files changed

+65
-28
lines changed

examples/templates/azure-linux/README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,35 @@ This template provisions the following resources:
3131

3232
This means, when the workspace restarts, any tools or files outside of the home directory are not persisted. To pre-bake tools into the workspace (e.g. `python3`), modify the VM image, or use a [startup script](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/script). Alternatively, individual developers can [personalize](https://coder.com/docs/dotfiles) their workspaces with dotfiles.
3333

34-
> **Note**
34+
> [!NOTE]
3535
> This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.
3636
37-
## code-server
3837

39-
`code-server` is installed via the `startup_script` argument in the `coder_agent`
40-
resource block. The `coder_app` resource is defined to access `code-server` through
41-
the dashboard UI over `localhost:13337`.
38+
### Persistent VM
39+
40+
> [!IMPORTANT]
41+
> This approach requires the [`az` CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli#install) to be present in the PATH of your Coder Provisioner.
42+
> You will have to do this installation manually as it is not included in our official images.
43+
44+
It is possible to make the VM persistent (instead of ephemeral) by removing the `count` attribute in the `azurerm_linux_virtual_machine` resource block as well as adding the following snippet:
45+
46+
```hcl
47+
# Stop the VM
48+
resource "null_resource" "stop_vm" {
49+
count = data.coder_workspace.me.transition == "stop" ? 1 : 0
50+
depends_on = [azurerm_linux_virtual_machine.main]
51+
provisioner "local-exec" {
52+
# Use deallocate so the VM is not charged
53+
command = "az vm deallocate --ids ${azurerm_linux_virtual_machine.main.id}"
54+
}
55+
}
56+
57+
# Start the VM
58+
resource "null_resource" "start" {
59+
count = data.coder_workspace.me.transition == "start" ? 1 : 0
60+
depends_on = [azurerm_linux_virtual_machine.main]
61+
provisioner "local-exec" {
62+
command = "az vm start --ids ${azurerm_linux_virtual_machine.main.id}"
63+
}
64+
}
65+
```

examples/templates/azure-linux/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ resource "tls_private_key" "dummy" {
264264
}
265265

266266
resource "azurerm_linux_virtual_machine" "main" {
267-
count = data.coder_workspace.me.transition == "start" ? 1 : 0
267+
count = data.coder_workspace.me.start_count
268268
name = "vm"
269269
resource_group_name = azurerm_resource_group.main.name
270270
location = azurerm_resource_group.main.location

examples/templates/azure-windows/README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,34 @@ This template provisions the following resources:
3131

3232
This means, when the workspace restarts, any tools or files outside of the data directory are not persisted. To pre-bake tools into the workspace (e.g. `python3`), modify the VM image, or use a [startup script](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/script).
3333

34-
> **Note**
34+
> [!NOTE]
3535
> This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.
36+
37+
### Persistent VM
38+
39+
> [!IMPORTANT]
40+
> This approach requires the [`az` CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli#install) to be present in the PATH of your Coder Provisioner.
41+
> You will have to do this installation manually as it is not included in our official images.
42+
43+
It is possible to make the VM persistent (instead of ephemeral) by removing the `count` attribute in the `azurerm_windows_virtual_machine` resource block as well as adding the following snippet:
44+
45+
```hcl
46+
# Stop the VM
47+
resource "null_resource" "stop_vm" {
48+
count = data.coder_workspace.me.transition == "stop" ? 1 : 0
49+
depends_on = [azurerm_windows_virtual_machine.main]
50+
provisioner "local-exec" {
51+
# Use deallocate so the VM is not charged
52+
command = "az vm deallocate --ids ${azurerm_windows_virtual_machine.main.id}"
53+
}
54+
}
55+
56+
# Start the VM
57+
resource "null_resource" "start" {
58+
count = data.coder_workspace.me.transition == "start" ? 1 : 0
59+
depends_on = [azurerm_windows_virtual_machine.main]
60+
provisioner "local-exec" {
61+
command = "az vm start --ids ${azurerm_windows_virtual_machine.main.id}"
62+
}
63+
}
64+
```

examples/templates/azure-windows/main.tf

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ resource "azurerm_managed_disk" "data" {
151151

152152
# Create virtual machine
153153
resource "azurerm_windows_virtual_machine" "main" {
154+
count = data.coder_workspace.me.start_count
154155
name = "vm"
155156
admin_username = local.admin_username
156157
admin_password = random_password.admin_password.result
@@ -189,7 +190,8 @@ resource "azurerm_windows_virtual_machine" "main" {
189190
}
190191

191192
resource "coder_metadata" "rdp_login" {
192-
resource_id = azurerm_windows_virtual_machine.main.id
193+
count = data.coder_workspace.me.start_count
194+
resource_id = azurerm_windows_virtual_machine.main[0].id
193195
item {
194196
key = "Username"
195197
value = local.admin_username
@@ -202,27 +204,9 @@ resource "coder_metadata" "rdp_login" {
202204
}
203205

204206
resource "azurerm_virtual_machine_data_disk_attachment" "main_data" {
207+
count = data.coder_workspace.me.start_count
205208
managed_disk_id = azurerm_managed_disk.data.id
206-
virtual_machine_id = azurerm_windows_virtual_machine.main.id
209+
virtual_machine_id = azurerm_windows_virtual_machine.main[0].id
207210
lun = "10"
208211
caching = "ReadWrite"
209212
}
210-
211-
# Stop the VM
212-
resource "null_resource" "stop_vm" {
213-
count = data.coder_workspace.me.transition == "stop" ? 1 : 0
214-
depends_on = [azurerm_windows_virtual_machine.main]
215-
provisioner "local-exec" {
216-
# Use deallocate so the VM is not charged
217-
command = "az vm deallocate --ids ${azurerm_windows_virtual_machine.main.id}"
218-
}
219-
}
220-
221-
# Start the VM
222-
resource "null_resource" "start" {
223-
count = data.coder_workspace.me.transition == "start" ? 1 : 0
224-
depends_on = [azurerm_windows_virtual_machine.main]
225-
provisioner "local-exec" {
226-
command = "az vm start --ids ${azurerm_windows_virtual_machine.main.id}"
227-
}
228-
}

0 commit comments

Comments
 (0)