|
| 1 | +terraform { |
| 2 | +required_providers { |
| 3 | +coder={ |
| 4 | + source="coder/coder" |
| 5 | + version="0.7.0" |
| 6 | + } |
| 7 | +azurerm={ |
| 8 | + source="hashicorp/azurerm" |
| 9 | + version="=3.52.0" |
| 10 | + } |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +provider"azurerm" { |
| 15 | +features {} |
| 16 | +} |
| 17 | + |
| 18 | +provider"coder" { |
| 19 | +} |
| 20 | + |
| 21 | +data"coder_workspace""me" {} |
| 22 | + |
| 23 | +data"coder_parameter""location" { |
| 24 | +description="What location should your workspace live in?" |
| 25 | +display_name="Location" |
| 26 | +name="location" |
| 27 | +default="eastus" |
| 28 | +mutable=false |
| 29 | +option { |
| 30 | +value="eastus" |
| 31 | +name="East US" |
| 32 | + } |
| 33 | +option { |
| 34 | +value="centralus" |
| 35 | +name="Central US" |
| 36 | + } |
| 37 | +option { |
| 38 | +value="southcentralus" |
| 39 | +name="South Central US" |
| 40 | + } |
| 41 | +option { |
| 42 | +value="westus2" |
| 43 | +name="West US 2" |
| 44 | + } |
| 45 | +} |
| 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 | + |
| 60 | +resource"coder_agent""main" { |
| 61 | +arch="amd64" |
| 62 | +auth="azure-instance-identity" |
| 63 | +os="windows" |
| 64 | +login_before_ready=false |
| 65 | +} |
| 66 | + |
| 67 | +resource"random_password""admin_password" { |
| 68 | +length=16 |
| 69 | +special=true |
| 70 | +# https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/password-must-meet-complexity-requirements#reference |
| 71 | +# we remove characters that require special handling in XML, as this is how we pass it to the VM |
| 72 | +# namely: <>&'" |
| 73 | +override_special="~!@#$%^*_-+=`|\\(){}[]:;,.?/" |
| 74 | +} |
| 75 | + |
| 76 | +locals { |
| 77 | +prefix="coder-win" |
| 78 | +admin_username="coder" |
| 79 | +} |
| 80 | + |
| 81 | +resource"azurerm_resource_group""main" { |
| 82 | +name="${local.prefix}-${data.coder_workspace.me.id}" |
| 83 | +location=data.coder_parameter.location.value |
| 84 | +tags={ |
| 85 | + Coder_Provisioned="true" |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// Uncomment here and in the azurerm_network_interface resource to obtain a public IP |
| 90 | +#resource "azurerm_public_ip" "main" { |
| 91 | +# name = "publicip" |
| 92 | +# resource_group_name = azurerm_resource_group.main.name |
| 93 | +# location = azurerm_resource_group.main.location |
| 94 | +# allocation_method = "Static" |
| 95 | +# tags = { |
| 96 | +# Coder_Provisioned = "true" |
| 97 | +# } |
| 98 | +#} |
| 99 | +resource"azurerm_virtual_network""main" { |
| 100 | +name="network" |
| 101 | +address_space=["10.0.0.0/24"] |
| 102 | +location=azurerm_resource_group.main.location |
| 103 | +resource_group_name=azurerm_resource_group.main.name |
| 104 | +tags={ |
| 105 | + Coder_Provisioned="true" |
| 106 | + } |
| 107 | +} |
| 108 | +resource"azurerm_subnet""internal" { |
| 109 | +name="internal" |
| 110 | +resource_group_name=azurerm_resource_group.main.name |
| 111 | +virtual_network_name=azurerm_virtual_network.main.name |
| 112 | +address_prefixes=["10.0.0.0/29"] |
| 113 | +} |
| 114 | +resource"azurerm_network_interface""main" { |
| 115 | +name="nic" |
| 116 | +resource_group_name=azurerm_resource_group.main.name |
| 117 | +location=azurerm_resource_group.main.location |
| 118 | +ip_configuration { |
| 119 | +name="internal" |
| 120 | +subnet_id=azurerm_subnet.internal.id |
| 121 | +private_ip_address_allocation="Dynamic" |
| 122 | +// Uncomment for public IP address as well as azurerm_public_ip resource above |
| 123 | +# public_ip_address_id = azurerm_public_ip.main.id |
| 124 | + } |
| 125 | +tags={ |
| 126 | + Coder_Provisioned="true" |
| 127 | + } |
| 128 | +} |
| 129 | +# Create storage account for boot diagnostics |
| 130 | +resource"azurerm_storage_account""my_storage_account" { |
| 131 | +name="diag${random_id.storage_id.hex}" |
| 132 | +location=azurerm_resource_group.main.location |
| 133 | +resource_group_name=azurerm_resource_group.main.name |
| 134 | +account_tier="Standard" |
| 135 | +account_replication_type="LRS" |
| 136 | +} |
| 137 | +# Generate random text for a unique storage account name |
| 138 | +resource"random_id""storage_id" { |
| 139 | +keepers={ |
| 140 | +# Generate a new ID only when a new resource group is defined |
| 141 | + resource_group= azurerm_resource_group.main.name |
| 142 | + } |
| 143 | +byte_length=8 |
| 144 | +} |
| 145 | + |
| 146 | +resource"azurerm_managed_disk""data" { |
| 147 | +name="data_disk" |
| 148 | +location=azurerm_resource_group.main.location |
| 149 | +resource_group_name=azurerm_resource_group.main.name |
| 150 | +storage_account_type="Standard_LRS" |
| 151 | +create_option="Empty" |
| 152 | +disk_size_gb=data.coder_parameter.data_disk_size.value |
| 153 | +} |
| 154 | + |
| 155 | +# Create virtual machine |
| 156 | +resource"azurerm_windows_virtual_machine""main" { |
| 157 | +name="vm" |
| 158 | +admin_username=local.admin_username |
| 159 | +admin_password=random_password.admin_password.result |
| 160 | +location=azurerm_resource_group.main.location |
| 161 | +resource_group_name=azurerm_resource_group.main.name |
| 162 | +network_interface_ids=[azurerm_network_interface.main.id] |
| 163 | +size="Standard_DS1_v2" |
| 164 | +custom_data=base64encode( |
| 165 | +templatefile("${path.module}/Initialize.ps1.tftpl", { init_script= coder_agent.main.init_script }) |
| 166 | + ) |
| 167 | +os_disk { |
| 168 | +name="myOsDisk" |
| 169 | +caching="ReadWrite" |
| 170 | +storage_account_type="Premium_LRS" |
| 171 | + } |
| 172 | +source_image_reference { |
| 173 | +publisher="MicrosoftWindowsServer" |
| 174 | +offer="WindowsServer" |
| 175 | +sku="2022-datacenter-azure-edition" |
| 176 | +version="latest" |
| 177 | + } |
| 178 | +additional_unattend_content { |
| 179 | +content="<AutoLogon><Password><Value>${random_password.admin_password.result}</Value></Password><Enabled>true</Enabled><LogonCount>1</LogonCount><Username>${local.admin_username}</Username></AutoLogon>" |
| 180 | +setting="AutoLogon" |
| 181 | + } |
| 182 | +additional_unattend_content { |
| 183 | +content=file("${path.module}/FirstLogonCommands.xml") |
| 184 | +setting="FirstLogonCommands" |
| 185 | + } |
| 186 | +boot_diagnostics { |
| 187 | +storage_account_uri=azurerm_storage_account.my_storage_account.primary_blob_endpoint |
| 188 | + } |
| 189 | +tags={ |
| 190 | + Coder_Provisioned="true" |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +resource"coder_metadata""rdp_login" { |
| 195 | +resource_id=azurerm_windows_virtual_machine.main.id |
| 196 | +item { |
| 197 | +key="Username" |
| 198 | +value=local.admin_username |
| 199 | + } |
| 200 | +item { |
| 201 | +key="Password" |
| 202 | +value=random_password.admin_password.result |
| 203 | +sensitive=true |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +resource"azurerm_virtual_machine_data_disk_attachment""main_data" { |
| 208 | +managed_disk_id=azurerm_managed_disk.data.id |
| 209 | +virtual_machine_id=azurerm_windows_virtual_machine.main.id |
| 210 | +lun="10" |
| 211 | +caching="ReadWrite" |
| 212 | +} |
| 213 | + |
| 214 | +# Stop the VM |
| 215 | +resource"null_resource""stop_vm" { |
| 216 | +count=data.coder_workspace.me.transition=="stop"?1:0 |
| 217 | +depends_on=[azurerm_windows_virtual_machine.main] |
| 218 | +provisioner"local-exec" { |
| 219 | +# Use deallocate so the VM is not charged |
| 220 | +command="az vm deallocate --ids${azurerm_windows_virtual_machine.main.id}" |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +# Start the VM |
| 225 | +resource"null_resource""start" { |
| 226 | +count=data.coder_workspace.me.transition=="start"?1:0 |
| 227 | +depends_on=[azurerm_windows_virtual_machine.main] |
| 228 | +provisioner"local-exec" { |
| 229 | +command="az vm start --ids${azurerm_windows_virtual_machine.main.id}" |
| 230 | + } |
| 231 | +} |