-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathvm.bicep
More file actions
424 lines (378 loc) · 10.7 KB
/
vm.bicep
File metadata and controls
424 lines (378 loc) · 10.7 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
@description('The name of your Virtual Machine.')
param vmName string = 'vm1'
@description('The Virtual Machine size.')
@allowed([
'Standard_B1ls'
'Standard_B1s'
'Standard_B2s'
'Standard_B1ms'
'Standard_B2ms'
'Standard_B4ms'
'Standard_D2s_v5'
'Standard_D4s_v5'
'Standard_D2ps_v5'
'Standard_D4ps_v5'
])
param vmSize string = 'Standard_B2s'
@description('The Storage Account Type for OS and Data disks.')
@allowed([
'Standard_LRS'
'Premium_LRS'
'UltraSSD_LRS'
])
param diskAccountType string = 'Premium_LRS'
@description('The OS Disk size.')
@allowed([
1024
512
256
128
64
32
])
param osDiskSize int = 256
@description('The OS image for the VM.')
@allowed([
'Ubuntu 20.04-LTS'
'Ubuntu 20.04-LTS (arm64)'
'Ubuntu 18.04-LTS'
])
param osImage string = 'Ubuntu 20.04-LTS'
@description('Location for all resources.')
param location string = resourceGroup().location
@description('Name of the VNET.')
param virtualNetworkName string = ''
@description('Default IP to allow Port 22 (SSH). Set to your own IP Address')
param allowIpPort22 string = '127.0.0.1'
@description('Username for the Virtual Machine.')
param adminUsername string = 'azureuser'
@secure()
@description('SSH Key for the Virtual Machine.')
param adminPasswordOrKey string = ''
@description('Deploy with cloud-init.')
@allowed([
'none'
'docker'
'tailscale'
'tailscale-private'
'tailscale-postgres'
'url'
])
param cloudInit string = 'none'
@description('Environment variables as JSON object.')
@secure()
param env object = {}
var rand = substring(uniqueString(resourceGroup().id), 0, 6)
var virtualNetworkName_var = virtualNetworkName != '' ? virtualNetworkName : '${resourceGroup().name}-vnet'
var subnetName = 'default'
var keyData_var = adminPasswordOrKey != '' ? adminPasswordOrKey : 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3gkRpKwprN00sT7yekr0xO0F+uTllDua02puhu1v0zGu3aENvUsygBHJiTy+flgrO2q3mY9F5/D67+WHDeSpr5s71UtnbzMxTams89qmo+raTm+IqjzdNujaWf0/pbT6JUkQq0fR0BfIvg3/7NTXhlzjmCOP2EpD91LzN6b5jAm/5hXr0V5mcpERo8kk2GWxjKmwmDOV+huH1DIFDpMxT3WzR2qvZp1DZbNSYmKkrite3FHlPGLXA1I3bRQT+iTj8vRGpxOPSiMdPK4RNMEZVXSGQ3OZbSl2FBCbd/tdJ1idKo8/ZCkHxdh9/em28/yfPUK0D164shgiEdIkdOQJv'
var publicIPAddressName = '${vmName}-ip'
var networkInterfaceName = '${vmName}-nic'
var ipConfigName = '${vmName}-ipconfig'
var subnetAddressPrefix = '10.1.0.0/24'
var addressPrefix = '10.1.0.0/16'
var cloudInitDocker = '''
#cloud-config
# vim: syntax=yaml
packages:
- docker.io
- jq
# create the docker group
groups:
- docker
# Add default auto created user to docker group
system_info:
default_user:
groups: [docker]
'''
var cloudInitTailscale = '''
#cloud-config
# vim: syntax=yaml
packages:
- docker.io
- jq
# create the docker group
groups:
- docker
# Add default auto created user to docker group
system_info:
default_user:
groups: [docker]
write_files:
- path: /home/azureuser/env.json
content: {0}
encoding: b64
- path: /home/azureuser/tailscale.sh
content: |
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.tailscale-keyring.list | sudo tee /etc/apt/sources.list.d/tailscale.list
sudo apt-get update
sudo apt-get install -y tailscale
sudo tailscale up --advertise-routes=10.1.0.0/24,168.63.129.16/32 --accept-dns=false --ssh --authkey "$1"
runcmd:
- cd /home/azureuser/
- bash tailscale.sh "$(jq -r '.tskey' env.json)"
- echo $(date) > hello.txt
- chown -R azureuser:azureuser /home/azureuser/
'''
var cloudInitTailscaleFormat = format(cloudInitTailscale, base64(string(env)))
var cloudInitTailscalePostgres = '''
#cloud-config
# vim: syntax=yaml
packages:
- docker.io
- jq
# create the docker group
groups:
- docker
# Add default auto created user to docker group
system_info:
default_user:
groups: [docker]
write_files:
- path: /home/azureuser/env.json
content: {0}
encoding: b64
- path: /home/azureuser/tailscale.sh
content: |
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.tailscale-keyring.list | sudo tee /etc/apt/sources.list.d/tailscale.list
sudo apt-get update
sudo apt-get install -y tailscale
sudo tailscale up --advertise-routes=10.1.0.0/24,168.63.129.16/32 --accept-dns=false --ssh --authkey "$1"
runcmd:
- cd /home/azureuser/
- bash tailscale.sh "$(jq -r '.tskey' env.json)"
- 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
- echo $(date) > hello.txt
- chown -R azureuser:azureuser /home/azureuser/
'''
var cloudInitTailscalePostgresFormat = format(cloudInitTailscalePostgres, base64(string(env)))
var cloudInitUrl = '''
#include
https://raw.githubusercontent.com/Azure-Samples/azure-opensource-labs/main/linux/vm/cloud-init/cloud-init.sh
'''
var kvCloudInit = {
none: null
docker: base64(cloudInitDocker)
tailscale: base64(cloudInitTailscaleFormat)
'tailscale-private': base64(cloudInitTailscaleFormat)
'tailscale-postgres': base64(cloudInitTailscalePostgresFormat)
url: base64(cloudInitUrl)
}
var 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'
}
}
// Base network security group rules
var nsgSecurityRulesBase = [
{
name: 'Port_22'
properties: {
priority: 100
protocol: 'Tcp'
access: 'Allow'
direction: 'Inbound'
sourceAddressPrefix: 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'
}
}
]
// If the cloudInit option is set to 'tailscale-private', then only use the 'Port_41641' rule which is the last rule in the base array
var nsgSecurityRules = (cloudInit == 'tailscale-private' ? [ last(nsgSecurityRulesBase) ] : nsgSecurityRulesBase)
resource identityName 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = {
name: '${resourceGroup().name}-identity'
location: location
}
resource nic 'Microsoft.Network/networkInterfaces@2021-05-01' = {
name: networkInterfaceName
location: location
properties: {
ipConfigurations: [
{
name: ipConfigName
properties: {
subnet: {
id: '/subscriptions/${subscription().subscriptionId}/resourceGroups/${resourceGroup().name}/providers/Microsoft.Network/virtualNetworks/${virtualNetworkName_var}/subnets/${subnetName}'
}
privateIPAllocationMethod: 'Dynamic'
publicIPAddress: (cloudInit != 'tailscale-private' ? { id: publicIP.id } : null)
}
}
]
networkSecurityGroup: {
id: nsg.id
}
}
}
resource nsg 'Microsoft.Network/networkSecurityGroups@2021-05-01' = {
name: '${resourceGroup().name}-nsg'
location: location
properties: {
securityRules: nsgSecurityRules
}
}
resource vnet 'Microsoft.Network/virtualNetworks@2021-05-01' = if (virtualNetworkName == '') {
name: virtualNetworkName_var
location: location
properties: {
addressSpace: {
addressPrefixes: [
addressPrefix
]
}
subnets: [
{
name: subnetName
properties: {
addressPrefix: subnetAddressPrefix
privateEndpointNetworkPolicies: 'Enabled'
privateLinkServiceNetworkPolicies: 'Enabled'
}
}
{
name: 'AzureBastionSubnet'
properties: {
addressPrefix: '10.1.1.0/26'
privateEndpointNetworkPolicies: 'Enabled'
privateLinkServiceNetworkPolicies: 'Enabled'
}
}
]
}
}
resource publicIP 'Microsoft.Network/publicIPAddresses@2021-05-01' = if (cloudInit != 'tailscale-private') {
name: publicIPAddressName
location: location
sku: {
name: 'Standard'
}
properties: {
publicIPAllocationMethod: 'Static'
dnsSettings: {
domainNameLabel: toLower('${vmName}-${rand}')
}
}
}
resource vm 'Microsoft.Compute/virtualMachines@2021-11-01' = {
name: vmName
location: location
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${identityName.id}': {}
}
}
properties: {
hardwareProfile: {
vmSize: vmSize
}
storageProfile: {
osDisk: {
managedDisk: {
storageAccountType: diskAccountType
}
name: '${vmName}-osdisk1'
diskSizeGB: osDiskSize
createOption: 'FromImage'
}
imageReference: kvImageReference[osImage]
}
networkProfile: {
networkInterfaces: [
{
id: nic.id
}
]
}
osProfile: {
computerName: vmName
customData: kvCloudInit[cloudInit]
adminUsername: adminUsername
linuxConfiguration: {
disablePasswordAuthentication: true
ssh: {
publicKeys: [
{
keyData: keyData_var
path: '/home/${adminUsername}/.ssh/authorized_keys'
}
]
}
}
}
}
}
output adminUsername string = adminUsername
output hostname string = (cloudInit != 'tailscale-private' ? publicIP.properties.dnsSettings.fqdn : vmName)
output sshCommand string = 'ssh ${adminUsername}@${(cloudInit != 'tailscale-private' ? publicIP.properties.dnsSettings.fqdn : vmName)}'