diff --git a/examples/examples.gen.json b/examples/examples.gen.json index b04cbb99906fc..83201b5243961 100644 --- a/examples/examples.gen.json +++ b/examples/examples.gen.json @@ -53,7 +53,7 @@ "linux", "azure" ], - "markdown": "\n# Remote Development on Azure VMs (Linux)\n\nProvision Azure Linux VMs as [Coder workspaces](https://coder.com/docs/workspaces) with this example template.\n\n\u003c!-- TODO: Add screenshot --\u003e\n\n## Prerequisites\n\n### Authentication\n\nThis template assumes that coderd is run in an environment that is authenticated\nwith Azure. For example, run `az login` then `az account set --subscription=\u003cid\u003e`\nto import credentials on the system and user running coderd. For other ways to\nauthenticate, [consult the Terraform docs](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#authenticating-to-azure).\n\n## Architecture\n\nThis template provisions the following resources:\n\n- Azure VM (ephemeral, deleted on stop)\n- Managed disk (persistent, mounted to `/home/coder`)\n\nThis 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.\n\n\u003e **Note**\n\u003e This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.\n\n## code-server\n\n`code-server` is installed via the `startup_script` argument in the `coder_agent`\nresource block. The `coder_app` resource is defined to access `code-server` through\nthe dashboard UI over `localhost:13337`.\n" + "markdown": "\n# Remote Development on Azure VMs (Linux)\n\nProvision Azure Linux VMs as [Coder workspaces](https://coder.com/docs/workspaces) with this example template.\n\n\u003c!-- TODO: Add screenshot --\u003e\n\n## Prerequisites\n\n### Authentication\n\nThis template assumes that coderd is run in an environment that is authenticated\nwith Azure. For example, run `az login` then `az account set --subscription=\u003cid\u003e`\nto import credentials on the system and user running coderd. For other ways to\nauthenticate, [consult the Terraform docs](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#authenticating-to-azure).\n\n## Architecture\n\nThis template provisions the following resources:\n\n- Azure VM (ephemeral, deleted on stop)\n- Managed disk (persistent, mounted to `/home/coder`)\n\nThis 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.\n\n\u003e [!NOTE]\n\u003e This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.\n\n\n### Persistent VM\n\n\u003e [!IMPORTANT] \n\u003e 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.\n\u003e You will have to do this installation manually as it is not included in our official images.\n\nIt 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:\n\n```hcl\n# Stop the VM\nresource \"null_resource\" \"stop_vm\" {\n count = data.coder_workspace.me.transition == \"stop\" ? 1 : 0\n depends_on = [azurerm_linux_virtual_machine.main]\n provisioner \"local-exec\" {\n # Use deallocate so the VM is not charged\n command = \"az vm deallocate --ids ${azurerm_linux_virtual_machine.main.id}\"\n }\n}\n\n# Start the VM\nresource \"null_resource\" \"start\" {\n count = data.coder_workspace.me.transition == \"start\" ? 1 : 0\n depends_on = [azurerm_linux_virtual_machine.main]\n provisioner \"local-exec\" {\n command = \"az vm start --ids ${azurerm_linux_virtual_machine.main.id}\"\n }\n}\n```\n" }, { "id": "digitalocean-linux", diff --git a/examples/templates/azure-linux/README.md b/examples/templates/azure-linux/README.md index 7ee99173bf3f6..a16526c187b54 100644 --- a/examples/templates/azure-linux/README.md +++ b/examples/templates/azure-linux/README.md @@ -31,11 +31,35 @@ This template provisions the following resources: 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. -> **Note** +> [!NOTE] > This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case. -## code-server -`code-server` is installed via the `startup_script` argument in the `coder_agent` -resource block. The `coder_app` resource is defined to access `code-server` through -the dashboard UI over `localhost:13337`. +### Persistent VM + +> [!IMPORTANT] +> 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. +> You will have to do this installation manually as it is not included in our official images. + +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: + +```hcl +# Stop the VM +resource "null_resource" "stop_vm" { + count = data.coder_workspace.me.transition == "stop" ? 1 : 0 + depends_on = [azurerm_linux_virtual_machine.main] + provisioner "local-exec" { + # Use deallocate so the VM is not charged + command = "az vm deallocate --ids ${azurerm_linux_virtual_machine.main.id}" + } +} + +# Start the VM +resource "null_resource" "start" { + count = data.coder_workspace.me.transition == "start" ? 1 : 0 + depends_on = [azurerm_linux_virtual_machine.main] + provisioner "local-exec" { + command = "az vm start --ids ${azurerm_linux_virtual_machine.main.id}" + } +} +``` diff --git a/examples/templates/azure-linux/main.tf b/examples/templates/azure-linux/main.tf index 63f027687b433..9f1e34fccad3c 100644 --- a/examples/templates/azure-linux/main.tf +++ b/examples/templates/azure-linux/main.tf @@ -264,7 +264,7 @@ resource "tls_private_key" "dummy" { } resource "azurerm_linux_virtual_machine" "main" { - count = data.coder_workspace.me.transition == "start" ? 1 : 0 + count = data.coder_workspace.me.start_count name = "vm" resource_group_name = azurerm_resource_group.main.name location = azurerm_resource_group.main.location diff --git a/examples/templates/azure-windows/README.md b/examples/templates/azure-windows/README.md index 732c7dbfda1b1..d42cb9d659dec 100644 --- a/examples/templates/azure-windows/README.md +++ b/examples/templates/azure-windows/README.md @@ -31,5 +31,34 @@ This template provisions the following resources: 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). -> **Note** +> [!NOTE] > This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case. + +### Persistent VM + +> [!IMPORTANT] +> 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. +> You will have to do this installation manually as it is not included in our official images. + +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: + +```hcl +# Stop the VM +resource "null_resource" "stop_vm" { + count = data.coder_workspace.me.transition == "stop" ? 1 : 0 + depends_on = [azurerm_windows_virtual_machine.main] + provisioner "local-exec" { + # Use deallocate so the VM is not charged + command = "az vm deallocate --ids ${azurerm_windows_virtual_machine.main.id}" + } +} + +# Start the VM +resource "null_resource" "start" { + count = data.coder_workspace.me.transition == "start" ? 1 : 0 + depends_on = [azurerm_windows_virtual_machine.main] + provisioner "local-exec" { + command = "az vm start --ids ${azurerm_windows_virtual_machine.main.id}" + } +} +``` diff --git a/examples/templates/azure-windows/main.tf b/examples/templates/azure-windows/main.tf index 6b8b00ccca079..9ff5bcbf4e0a4 100644 --- a/examples/templates/azure-windows/main.tf +++ b/examples/templates/azure-windows/main.tf @@ -151,6 +151,7 @@ resource "azurerm_managed_disk" "data" { # Create virtual machine resource "azurerm_windows_virtual_machine" "main" { + count = data.coder_workspace.me.start_count name = "vm" admin_username = local.admin_username admin_password = random_password.admin_password.result @@ -189,7 +190,8 @@ resource "azurerm_windows_virtual_machine" "main" { } resource "coder_metadata" "rdp_login" { - resource_id = azurerm_windows_virtual_machine.main.id + count = data.coder_workspace.me.start_count + resource_id = azurerm_windows_virtual_machine.main[0].id item { key = "Username" value = local.admin_username @@ -202,27 +204,9 @@ resource "coder_metadata" "rdp_login" { } resource "azurerm_virtual_machine_data_disk_attachment" "main_data" { + count = data.coder_workspace.me.start_count managed_disk_id = azurerm_managed_disk.data.id - virtual_machine_id = azurerm_windows_virtual_machine.main.id + virtual_machine_id = azurerm_windows_virtual_machine.main[0].id lun = "10" caching = "ReadWrite" } - -# Stop the VM -resource "null_resource" "stop_vm" { - count = data.coder_workspace.me.transition == "stop" ? 1 : 0 - depends_on = [azurerm_windows_virtual_machine.main] - provisioner "local-exec" { - # Use deallocate so the VM is not charged - command = "az vm deallocate --ids ${azurerm_windows_virtual_machine.main.id}" - } -} - -# Start the VM -resource "null_resource" "start" { - count = data.coder_workspace.me.transition == "start" ? 1 : 0 - depends_on = [azurerm_windows_virtual_machine.main] - provisioner "local-exec" { - command = "az vm start --ids ${azurerm_windows_virtual_machine.main.id}" - } -}