-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvirtualMachine.bicep
190 lines (167 loc) · 5.13 KB
/
virtualMachine.bicep
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
// Parameters
@description('Specifies the name of the virtual machine.')
param vmName string = 'TestVm'
@description('Specifies the size of the virtual machine.')
param vmSize string = 'Standard_DS3_v2'
@description('Specifies the resource id of the subnet hosting the virtual machine.')
param vmSubnetId string
@description('Specifies the name of the storage account where the bootstrap diagnostic logs of the virtual machine are stored.')
param storageAccountName string
@description('Specifies the image publisher of the disk image used to create the virtual machine.')
param imagePublisher string = 'Canonical'
@description('Specifies the offer of the platform image or marketplace image used to create the virtual machine.')
param imageOffer string = '0001-com-ubuntu-server-jammy'
@description('Specifies the Ubuntu version for the VM. This will pick a fully patched image of this given Ubuntu version.')
param imageSku string = '22_04-lts-gen2'
@description('Specifies the type of authentication when accessing the Virtual Machine. SSH key is recommended.')
@allowed([
'sshPublicKey'
'password'
])
param authenticationType string = 'password'
@description('Specifies the name of the administrator account of the virtual machine.')
param vmAdminUsername string
@description('Specifies the SSH Key or password for the virtual machine. SSH key is recommended.')
@secure()
param vmAdminPasswordOrKey string
@description('Specifies the storage account type for OS and data disk.')
@allowed([
'Premium_LRS'
'StandardSSD_LRS'
'Standard_LRS'
'UltraSSD_LRS'
])
param diskStorageAccountType string = 'Premium_LRS'
@description('Specifies the number of data disks of the virtual machine.')
@minValue(0)
@maxValue(64)
param numDataDisks int = 1
@description('Specifies the size in GB of the OS disk of the VM.')
param osDiskSize int = 50
@description('Specifies the size in GB of the OS disk of the virtual machine.')
param dataDiskSize int = 50
@description('Specifies the caching requirements for the data disks.')
param dataDiskCaching string = 'ReadWrite'
@description('Specifies the name of the user-defined managed identity used by the Azure Monitor Agent.')
param managedIdentityName string
@description('Specifies the location.')
param location string = resourceGroup().location
@description('Specifies the resource tags.')
param tags object
// Variables
var vmNicName = '${vmName}Nic'
var linuxConfiguration = {
disablePasswordAuthentication: true
ssh: {
publicKeys: [
{
path: '/home/${vmAdminUsername}/.ssh/authorized_keys'
keyData: vmAdminPasswordOrKey
}
]
}
provisionVMAgent: true
}
// Resources
resource virtualMachineNic 'Microsoft.Network/networkInterfaces@2021-08-01' = {
name: vmNicName
location: location
tags: tags
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
privateIPAllocationMethod: 'Dynamic'
subnet: {
id: vmSubnetId
}
}
}
]
}
}
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' existing = {
name: storageAccountName
}
resource virtualMachine 'Microsoft.Compute/virtualMachines@2024-07-01' = {
name: vmName
location: location
tags: tags
properties: {
hardwareProfile: {
vmSize: vmSize
}
osProfile: {
computerName: vmName
adminUsername: vmAdminUsername
adminPassword: vmAdminPasswordOrKey
linuxConfiguration: (authenticationType == 'password') ? null : linuxConfiguration
}
storageProfile: {
imageReference: {
publisher: imagePublisher
offer: imageOffer
sku: imageSku
version: 'latest'
}
osDisk: {
name: '${vmName}_OSDisk'
caching: 'ReadWrite'
createOption: 'FromImage'
diskSizeGB: osDiskSize
managedDisk: {
storageAccountType: diskStorageAccountType
}
}
dataDisks: [for j in range(0, numDataDisks): {
caching: dataDiskCaching
diskSizeGB: dataDiskSize
lun: j
name: '${vmName}-DataDisk${j}'
createOption: 'Empty'
managedDisk: {
storageAccountType: diskStorageAccountType
}
}]
}
networkProfile: {
networkInterfaces: [
{
id: virtualMachineNic.id
}
]
}
diagnosticsProfile: {
bootDiagnostics: {
enabled: true
storageUri: storageAccount.properties.primaryEndpoints.blob
}
}
}
}
resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-07-31-preview' = {
name: managedIdentityName
location: location
tags: tags
}
resource linuxAgent 'Microsoft.Compute/virtualMachines/extensions@2024-07-01' = {
name: 'AzureMonitorLinuxAgent'
parent: virtualMachine
location: location
properties: {
publisher: 'Microsoft.Azure.Monitor'
type: 'AzureMonitorLinuxAgent'
typeHandlerVersion: '1.21'
autoUpgradeMinorVersion: true
enableAutomaticUpgrade: true
settings: {
authentication: {
managedIdentity: {
'identifier-name': 'mi_res_id'
'identifier-value': managedIdentity.id
}
}
}
}
}