-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathvm.json
More file actions
409 lines (409 loc) · 15.9 KB
/
vm.json
File metadata and controls
409 lines (409 loc) · 15.9 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.29.47.4906",
"templateHash": "18192218196942100983"
}
},
"parameters": {
"vmName": {
"type": "string",
"defaultValue": "vm1",
"metadata": {
"description": "The name of your Virtual Machine."
}
},
"vmSize": {
"type": "string",
"defaultValue": "Standard_B2s",
"allowedValues": [
"Standard_B1ls",
"Standard_B1s",
"Standard_B2s",
"Standard_B1ms",
"Standard_B2ms",
"Standard_B4ms",
"Standard_D2s_v5",
"Standard_D4s_v5",
"Standard_D2ps_v5",
"Standard_D4ps_v5"
],
"metadata": {
"description": "The Virtual Machine size."
}
},
"diskAccountType": {
"type": "string",
"defaultValue": "Premium_LRS",
"allowedValues": [
"Standard_LRS",
"Premium_LRS",
"UltraSSD_LRS"
],
"metadata": {
"description": "The Storage Account Type for OS and Data disks."
}
},
"osDiskSize": {
"type": "int",
"defaultValue": 256,
"allowedValues": [
1024,
512,
256,
128,
64,
32
],
"metadata": {
"description": "The OS Disk size."
}
},
"osImage": {
"type": "string",
"defaultValue": "Ubuntu 20.04-LTS",
"allowedValues": [
"Ubuntu 20.04-LTS",
"Ubuntu 20.04-LTS (arm64)",
"Ubuntu 18.04-LTS"
],
"metadata": {
"description": "The OS image for the VM."
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
},
"virtualNetworkName": {
"type": "string",
"defaultValue": "",
"metadata": {
"description": "Name of the VNET."
}
},
"allowIpPort22": {
"type": "string",
"defaultValue": "127.0.0.1",
"metadata": {
"description": "Default IP to allow Port 22 (SSH). Set to your own IP Address"
}
},
"adminUsername": {
"type": "string",
"defaultValue": "azureuser",
"metadata": {
"description": "Username for the Virtual Machine."
}
},
"adminPasswordOrKey": {
"type": "securestring",
"defaultValue": "",
"metadata": {
"description": "SSH Key for the Virtual Machine."
}
},
"cloudInit": {
"type": "string",
"defaultValue": "none",
"allowedValues": [
"none",
"docker",
"tailscale",
"tailscale-private",
"tailscale-postgres",
"url"
],
"metadata": {
"description": "Deploy with cloud-init."
}
},
"env": {
"type": "secureObject",
"defaultValue": {},
"metadata": {
"description": "Environment variables as JSON object."
}
}
},
"variables": {
"rand": "[substring(uniqueString(resourceGroup().id), 0, 6)]",
"virtualNetworkName_var": "[if(not(equals(parameters('virtualNetworkName'), '')), parameters('virtualNetworkName'), format('{0}-vnet', resourceGroup().name))]",
"subnetName": "default",
"keyData_var": "[if(not(equals(parameters('adminPasswordOrKey'), '')), parameters('adminPasswordOrKey'), 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3gkRpKwprN00sT7yekr0xO0F+uTllDua02puhu1v0zGu3aENvUsygBHJiTy+flgrO2q3mY9F5/D67+WHDeSpr5s71UtnbzMxTams89qmo+raTm+IqjzdNujaWf0/pbT6JUkQq0fR0BfIvg3/7NTXhlzjmCOP2EpD91LzN6b5jAm/5hXr0V5mcpERo8kk2GWxjKmwmDOV+huH1DIFDpMxT3WzR2qvZp1DZbNSYmKkrite3FHlPGLXA1I3bRQT+iTj8vRGpxOPSiMdPK4RNMEZVXSGQ3OZbSl2FBCbd/tdJ1idKo8/ZCkHxdh9/em28/yfPUK0D164shgiEdIkdOQJv')]",
"publicIPAddressName": "[format('{0}-ip', parameters('vmName'))]",
"networkInterfaceName": "[format('{0}-nic', parameters('vmName'))]",
"ipConfigName": "[format('{0}-ipconfig', parameters('vmName'))]",
"subnetAddressPrefix": "10.1.0.0/24",
"addressPrefix": "10.1.0.0/16",
"cloudInitDocker": "#cloud-config\n# vim: syntax=yaml\n\npackages:\n- docker.io\n- jq\n\n# create the docker group\ngroups:\n- docker\n\n# Add default auto created user to docker group\nsystem_info:\n default_user:\n groups: [docker]\n",
"cloudInitTailscale": "#cloud-config\n# vim: syntax=yaml\n\npackages:\n- docker.io\n- jq\n\n# create the docker group\ngroups:\n- docker\n\n# Add default auto created user to docker group\nsystem_info:\n default_user:\n groups: [docker]\n\nwrite_files:\n- path: /home/azureuser/env.json\n content: {0}\n encoding: b64\n- path: /home/azureuser/tailscale.sh\n content: |\n curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null\n curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.tailscale-keyring.list | sudo tee /etc/apt/sources.list.d/tailscale.list\n \n sudo apt-get update\n sudo apt-get install -y tailscale\n \n sudo tailscale up --advertise-routes=10.1.0.0/24,168.63.129.16/32 --accept-dns=false --ssh --authkey \"$1\"\n\nruncmd:\n- cd /home/azureuser/\n- bash tailscale.sh \"$(jq -r '.tskey' env.json)\"\n- echo $(date) > hello.txt\n- chown -R azureuser:azureuser /home/azureuser/\n",
"cloudInitTailscaleFormat": "[format(variables('cloudInitTailscale'), base64(string(parameters('env'))))]",
"cloudInitTailscalePostgres": "#cloud-config\n# vim: syntax=yaml\n\npackages:\n- docker.io\n- jq\n\n# create the docker group\ngroups:\n- docker\n\n# Add default auto created user to docker group\nsystem_info:\n default_user:\n groups: [docker]\n\nwrite_files:\n- path: /home/azureuser/env.json\n content: {0}\n encoding: b64\n- path: /home/azureuser/tailscale.sh\n content: |\n curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null\n curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.tailscale-keyring.list | sudo tee /etc/apt/sources.list.d/tailscale.list\n \n sudo apt-get update\n sudo apt-get install -y tailscale\n \n sudo tailscale up --advertise-routes=10.1.0.0/24,168.63.129.16/32 --accept-dns=false --ssh --authkey \"$1\"\n\nruncmd:\n- cd /home/azureuser/\n- bash tailscale.sh \"$(jq -r '.tskey' env.json)\"\n- docker run --name postgres --restart always -e POSTGRES_HOST_AUTH_METHOD=trust -v /home/azureuser/postgresql/data:/var/lib/postgresql/data -p 5432:5432 -d postgres:14\n- echo $(date) > hello.txt\n- chown -R azureuser:azureuser /home/azureuser/\n",
"cloudInitTailscalePostgresFormat": "[format(variables('cloudInitTailscalePostgres'), base64(string(parameters('env'))))]",
"cloudInitUrl": "#include\nhttps://raw.githubusercontent.com/Azure-Samples/azure-opensource-labs/main/linux/vm/cloud-init/cloud-init.sh\n",
"kvCloudInit": {
"none": null,
"docker": "[base64(variables('cloudInitDocker'))]",
"tailscale": "[base64(variables('cloudInitTailscaleFormat'))]",
"tailscale-private": "[base64(variables('cloudInitTailscaleFormat'))]",
"tailscale-postgres": "[base64(variables('cloudInitTailscalePostgresFormat'))]",
"url": "[base64(variables('cloudInitUrl'))]"
},
"kvImageReference": {
"Ubuntu 20.04-LTS": {
"publisher": "canonical",
"offer": "0001-com-ubuntu-server-focal",
"sku": "20_04-lts-gen2",
"version": "latest"
},
"Ubuntu 18.04-LTS": {
"publisher": "Canonical",
"offer": "UbuntuServer",
"sku": "18.04-LTS",
"version": "latest"
},
"Ubuntu 20.04-LTS (arm64)": {
"publisher": "canonical",
"offer": "0001-com-ubuntu-server-focal",
"sku": "20_04-lts-arm64",
"version": "latest"
}
},
"nsgSecurityRulesBase": [
{
"name": "Port_22",
"properties": {
"priority": 100,
"protocol": "Tcp",
"access": "Allow",
"direction": "Inbound",
"sourceAddressPrefix": "[parameters('allowIpPort22')]",
"sourcePortRange": "*",
"destinationAddressPrefix": "*",
"destinationPortRange": "22"
}
},
{
"name": "Port_80",
"properties": {
"protocol": "*",
"sourcePortRange": "*",
"destinationPortRange": "80",
"sourceAddressPrefix": "Internet",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 110,
"direction": "Inbound"
}
},
{
"name": "Port_443",
"properties": {
"protocol": "*",
"sourcePortRange": "*",
"destinationPortRange": "443",
"sourceAddressPrefix": "Internet",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 120,
"direction": "Inbound"
}
},
{
"name": "Port_8080",
"properties": {
"protocol": "*",
"sourcePortRange": "*",
"destinationPortRange": "8080",
"sourceAddressPrefix": "Internet",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 130,
"direction": "Inbound"
}
},
{
"name": "Port_41641",
"properties": {
"protocol": "Udp",
"sourcePortRange": "*",
"destinationPortRange": "41641",
"sourceAddressPrefix": "Internet",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 140,
"direction": "Inbound"
}
}
],
"nsgSecurityRules": "[if(equals(parameters('cloudInit'), 'tailscale-private'), createArray(last(variables('nsgSecurityRulesBase'))), variables('nsgSecurityRulesBase'))]"
},
"resources": [
{
"type": "Microsoft.ManagedIdentity/userAssignedIdentities",
"apiVersion": "2018-11-30",
"name": "[format('{0}-identity', resourceGroup().name)]",
"location": "[parameters('location')]"
},
{
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2021-05-01",
"name": "[variables('networkInterfaceName')]",
"location": "[parameters('location')]",
"properties": {
"ipConfigurations": [
{
"name": "[variables('ipConfigName')]",
"properties": {
"subnet": {
"id": "[format('/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/virtualNetworks/{2}/subnets/{3}', subscription().subscriptionId, resourceGroup().name, variables('virtualNetworkName_var'), variables('subnetName'))]"
},
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": "[if(not(equals(parameters('cloudInit'), 'tailscale-private')), createObject('id', resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))), null())]"
}
}
],
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', format('{0}-nsg', resourceGroup().name))]"
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkSecurityGroups', format('{0}-nsg', resourceGroup().name))]",
"[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
]
},
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2021-05-01",
"name": "[format('{0}-nsg', resourceGroup().name)]",
"location": "[parameters('location')]",
"properties": {
"securityRules": "[variables('nsgSecurityRules')]"
}
},
{
"condition": "[equals(parameters('virtualNetworkName'), '')]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2021-05-01",
"name": "[variables('virtualNetworkName_var')]",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetAddressPrefix')]",
"privateEndpointNetworkPolicies": "Enabled",
"privateLinkServiceNetworkPolicies": "Enabled"
}
},
{
"name": "AzureBastionSubnet",
"properties": {
"addressPrefix": "10.1.1.0/26",
"privateEndpointNetworkPolicies": "Enabled",
"privateLinkServiceNetworkPolicies": "Enabled"
}
}
]
}
},
{
"condition": "[not(equals(parameters('cloudInit'), 'tailscale-private'))]",
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2021-05-01",
"name": "[variables('publicIPAddressName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard"
},
"properties": {
"publicIPAllocationMethod": "Static",
"dnsSettings": {
"domainNameLabel": "[toLower(format('{0}-{1}', parameters('vmName'), variables('rand')))]"
}
}
},
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2021-11-01",
"name": "[parameters('vmName')]",
"location": "[parameters('location')]",
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"[format('{0}', resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-identity', resourceGroup().name)))]": {}
}
},
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"storageProfile": {
"osDisk": {
"managedDisk": {
"storageAccountType": "[parameters('diskAccountType')]"
},
"name": "[format('{0}-osdisk1', parameters('vmName'))]",
"diskSizeGB": "[parameters('osDiskSize')]",
"createOption": "FromImage"
},
"imageReference": "[variables('kvImageReference')[parameters('osImage')]]"
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
}
]
},
"osProfile": {
"computerName": "[parameters('vmName')]",
"customData": "[variables('kvCloudInit')[parameters('cloudInit')]]",
"adminUsername": "[parameters('adminUsername')]",
"linuxConfiguration": {
"disablePasswordAuthentication": true,
"ssh": {
"publicKeys": [
{
"keyData": "[variables('keyData_var')]",
"path": "[format('/home/{0}/.ssh/authorized_keys', parameters('adminUsername'))]"
}
]
}
}
}
},
"dependsOn": [
"[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-identity', resourceGroup().name))]",
"[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
]
}
],
"outputs": {
"adminUsername": {
"type": "string",
"value": "[parameters('adminUsername')]"
},
"hostname": {
"type": "string",
"value": "[if(not(equals(parameters('cloudInit'), 'tailscale-private')), reference(resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName')), '2021-05-01').dnsSettings.fqdn, parameters('vmName'))]"
},
"sshCommand": {
"type": "string",
"value": "[format('ssh {0}@{1}', parameters('adminUsername'), if(not(equals(parameters('cloudInit'), 'tailscale-private')), reference(resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName')), '2021-05-01').dnsSettings.fqdn, parameters('vmName')))]"
}
}
}