Skip to content

Instantly share code, notes, and snippets.

@alexs77
Created June 17, 2021 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexs77/7ee29c299167f37ed39caf3136992441 to your computer and use it in GitHub Desktop.
Save alexs77/7ee29c299167f37ed39caf3136992441 to your computer and use it in GitHub Desktop.
Simple Bicep file creating a Virtual Machine, including Virtual Network and NIC

Bicep 💪

Simple Bicep file, which creates a Virtual Machine (Ubuntu 18.04) on Azure.

Invocation

resource_group_name="something"
az deployment group create -c --mode Complete -g "$resource_group_name" --template-file vm.bicep --parameters params.json
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"namePrefix": {
"value": "shr"
},
"vmUsername": {
"value": "test_username"
},
"vmPassword": {
"value": "$1test_password"
}
}
}
targetScope = 'resourceGroup' // 'tenant', 'managementGroup', 'subscription', 'resourceGroup'
param vmUsername string
param vmPassword string
param namePrefix string = 'not set'
resource vnet 'Microsoft.Network/virtualNetworks@2020-11-01' = {
name: '${namePrefix}-vnet'
location: resourceGroup().location
properties: {
addressSpace: {
addressPrefixes: [
'10.99.0.0/25'
]
}
subnets: [
{
name: '${namePrefix}-subnet'
properties: {
addressPrefix: '10.99.0.0/25'
delegations: []
privateEndpointNetworkPolicies: 'Enabled'
privateLinkServiceNetworkPolicies: 'Enabled'
}
}
]
virtualNetworkPeerings: []
enableDdosProtection: false
}
}
resource nic 'Microsoft.Network/networkInterfaces@2020-08-01' = {
name: '${namePrefix}-nic'
location: resourceGroup().location
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
privateIPAddress: '10.99.0.4' // Value is ignored, but makes az cli shut up
privateIPAllocationMethod: 'Dynamic'
subnet: {
id: '${vnet.id}/subnets/${namePrefix}-subnet'
}
primary: true
privateIPAddressVersion: 'IPv4'
}
}
]
dnsSettings: {
dnsServers: []
}
enableAcceleratedNetworking: false
enableIPForwarding: false
}
}
resource vm 'Microsoft.Compute/virtualMachines@2020-12-01' = {
name: '${namePrefix}-vm'
location: resourceGroup().location
properties: {
hardwareProfile: {
vmSize: 'Standard_B1ms'
}
storageProfile: {
osDisk: {
createOption: 'FromImage'
managedDisk: {
storageAccountType: 'StandardSSD_LRS'
}
}
imageReference: {
publisher: 'Canonical'
offer: 'UbuntuServer'
sku: '18.04-LTS'
version: 'latest'
}
}
osProfile: {
computerName: '${namePrefix}-cn'
adminUsername: vmUsername
adminPassword: vmPassword
}
networkProfile: {
networkInterfaces: [
{
id: nic.id
}
]
}
}
}
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.4.63.48766",
"templateHash": "14576519551725798596"
}
},
"parameters": {
"vmUsername": {
"type": "string"
},
"vmPassword": {
"type": "string"
},
"namePrefix": {
"type": "string",
"defaultValue": "not set"
}
},
"functions": [],
"resources": [
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-11-01",
"name": "[format('{0}-vnet', parameters('namePrefix'))]",
"location": "[resourceGroup().location]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"10.99.0.0/25"
]
},
"subnets": [
{
"name": "[format('{0}-subnet', parameters('namePrefix'))]",
"properties": {
"addressPrefix": "10.99.0.0/25",
"delegations": [],
"privateEndpointNetworkPolicies": "Enabled",
"privateLinkServiceNetworkPolicies": "Enabled"
}
}
],
"virtualNetworkPeerings": [],
"enableDdosProtection": false
}
},
{
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2020-08-01",
"name": "[format('{0}-nic', parameters('namePrefix'))]",
"location": "[resourceGroup().location]",
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAddress": "10.99.0.4",
"privateIPAllocationMethod": "Dynamic",
"subnet": {
"id": "[format('{0}/subnets/{1}-subnet', resourceId('Microsoft.Network/virtualNetworks', format('{0}-vnet', parameters('namePrefix'))), parameters('namePrefix'))]"
},
"primary": true,
"privateIPAddressVersion": "IPv4"
}
}
],
"dnsSettings": {
"dnsServers": []
},
"enableAcceleratedNetworking": false,
"enableIPForwarding": false
},
"dependsOn": [
"[resourceId('Microsoft.Network/virtualNetworks', format('{0}-vnet', parameters('namePrefix')))]"
]
},
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2020-12-01",
"name": "[format('{0}-vm', parameters('namePrefix'))]",
"location": "[resourceGroup().location]",
"properties": {
"hardwareProfile": {
"vmSize": "Standard_B1ms"
},
"storageProfile": {
"osDisk": {
"createOption": "FromImage",
"managedDisk": {
"storageAccountType": "StandardSSD_LRS"
}
},
"imageReference": {
"publisher": "Canonical",
"offer": "UbuntuServer",
"sku": "18.04-LTS",
"version": "latest"
}
},
"osProfile": {
"computerName": "[format('{0}-cn', parameters('namePrefix'))]",
"adminUsername": "[parameters('vmUsername')]",
"adminPassword": "[parameters('vmPassword')]"
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', format('{0}-nic', parameters('namePrefix')))]"
}
]
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces', format('{0}-nic', parameters('namePrefix')))]"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment