Last active
August 31, 2021 17:32
-
-
Save bmoore-msft/811cb09050896ebc15473bea8ccb481f to your computer and use it in GitHub Desktop.
ARM Template using MSI for custom script extension download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", | |
"contentVersion": "1.0.0.0", | |
"metadata": { | |
"_generator": { | |
"name": "bicep", | |
"version": "0.4.451.19169", | |
"templateHash": "16656980055364544710" | |
} | |
}, | |
"parameters": { | |
"adminUsername": { | |
"type": "string", | |
"metadata": { | |
"description": "Username for the Virtual Machine." | |
} | |
}, | |
"adminPassword": { | |
"type": "secureString", | |
"metadata": { | |
"description": "Password for the Virtual Machine." | |
} | |
}, | |
"vmSize": { | |
"type": "string", | |
"defaultValue": "Standard_A4_v2", | |
"metadata": { | |
"description": "Size of the virtual machine" | |
} | |
}, | |
"location": { | |
"type": "string", | |
"defaultValue": "[resourceGroup().location]", | |
"metadata": { | |
"description": "Location for all resources." | |
} | |
}, | |
"scriptUri": { | |
"type": "string", | |
"defaultValue": "https://stageceaedbb7b8274195b55.blob.core.windows.net/000/hello.ps1", | |
"metadata": { | |
"description": "Uri for the custom script" | |
} | |
}, | |
"identityName": { | |
"type": "string", | |
"metadata": { | |
"description": "name for the managedIdenity to use for custom script download, the principal must have at least storage blob reader permission on the storage account, roleDef: 2a2b9908-6ea1-4ae2-8e65-a410df84e7d1" | |
} | |
}, | |
"identityResourceGroup": { | |
"type": "string", | |
"metadata": { | |
"description": "resourceGroup for the managedIdenity to use for custom script download" | |
} | |
}, | |
"alwaysRun": { | |
"type": "string", | |
"defaultValue": "[newGuid()]" | |
} | |
}, | |
"functions": [], | |
"variables": { | |
"imagePublisher": "MicrosoftWindowsServer", | |
"imageOffer": "WindowsServer", | |
"windowsOSVersion": "2019-Datacenter", | |
"nicName": "myVMNic", | |
"addressPrefix": "10.0.0.0/16", | |
"subnetName": "Subnet", | |
"subnetPrefix": "10.0.0.0/24", | |
"publicIPAddressName": "myPublicIP", | |
"publicIPAddressType": "Dynamic", | |
"vmName": "MyWindowsVM", | |
"virtualNetworkName": "MyVNET" | |
}, | |
"resources": [ | |
{ | |
"type": "Microsoft.Network/publicIPAddresses", | |
"apiVersion": "2021-02-01", | |
"name": "[variables('publicIPAddressName')]", | |
"location": "[parameters('location')]", | |
"properties": { | |
"publicIPAllocationMethod": "[variables('publicIPAddressType')]" | |
} | |
}, | |
{ | |
"type": "Microsoft.Network/virtualNetworks", | |
"apiVersion": "2021-02-01", | |
"name": "[variables('virtualNetworkName')]", | |
"location": "[parameters('location')]", | |
"properties": { | |
"addressSpace": { | |
"addressPrefixes": [ | |
"[variables('addressPrefix')]" | |
] | |
}, | |
"subnets": [ | |
{ | |
"name": "[variables('subnetName')]", | |
"properties": { | |
"addressPrefix": "[variables('subnetPrefix')]" | |
} | |
} | |
] | |
} | |
}, | |
{ | |
"type": "Microsoft.Network/networkInterfaces", | |
"apiVersion": "2021-02-01", | |
"name": "[variables('nicName')]", | |
"location": "[parameters('location')]", | |
"properties": { | |
"ipConfigurations": [ | |
{ | |
"name": "ipconfig1", | |
"properties": { | |
"privateIPAllocationMethod": "Dynamic", | |
"publicIPAddress": { | |
"id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]" | |
}, | |
"subnet": { | |
"id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]" | |
} | |
} | |
} | |
] | |
}, | |
"dependsOn": [ | |
"[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]" | |
] | |
}, | |
{ | |
"type": "Microsoft.Compute/virtualMachines", | |
"apiVersion": "2021-04-01", | |
"name": "[variables('vmName')]", | |
"location": "[parameters('location')]", | |
"identity": { | |
"type": "UserAssigned", | |
"userAssignedIdentities": { | |
"[extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, parameters('identityResourceGroup')), 'Microsoft.ManagedIdentity/userAssignedIdentities', parameters('identityName'))]": {} | |
} | |
}, | |
"properties": { | |
"hardwareProfile": { | |
"vmSize": "[parameters('vmSize')]" | |
}, | |
"osProfile": { | |
"computerName": "[variables('vmName')]", | |
"adminUsername": "[parameters('adminUsername')]", | |
"adminPassword": "[parameters('adminPassword')]" | |
}, | |
"storageProfile": { | |
"imageReference": { | |
"publisher": "[variables('imagePublisher')]", | |
"offer": "[variables('imageOffer')]", | |
"sku": "[variables('windowsOSVersion')]", | |
"version": "latest" | |
}, | |
"osDisk": { | |
"createOption": "FromImage", | |
"managedDisk": { | |
"storageAccountType": "StandardSSD_LRS" | |
} | |
} | |
}, | |
"networkProfile": { | |
"networkInterfaces": [ | |
{ | |
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]" | |
} | |
] | |
} | |
}, | |
"dependsOn": [ | |
"[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]" | |
] | |
}, | |
{ | |
"type": "Microsoft.Compute/virtualMachines/extensions", | |
"apiVersion": "2021-04-01", | |
"name": "[format('{0}/{1}', variables('vmName'), 'cse')]", | |
"location": "[parameters('location')]", | |
"properties": { | |
"publisher": "Microsoft.Compute", | |
"type": "CustomScriptExtension", | |
"typeHandlerVersion": "1.4", | |
"autoUpgradeMinorVersion": true, | |
"forceUpdateTag": "[parameters('alwaysRun')]", | |
"settings": { | |
"fileUris": [ | |
"[parameters('scriptUri')]" | |
], | |
"commandToExecute": "[format('powershell -ExecutionPolicy Unrestricted -File {0}', last(split(parameters('scriptUri'), '/')))]" | |
}, | |
"protectedSettings": { | |
"managedIdentity": { | |
"object": "[reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, parameters('identityResourceGroup')), 'Microsoft.ManagedIdentity/userAssignedIdentities', parameters('identityName')), '2018-11-30').principalId]" | |
} | |
} | |
}, | |
"dependsOn": [ | |
"[resourceId('Microsoft.Compute/virtualMachines', variables('vmName'))]" | |
] | |
} | |
], | |
"outputs": { | |
"scriptFileName": { | |
"type": "string", | |
"value": "[last(split(parameters('scriptUri'), '/'))]" | |
}, | |
"msiObjectId": { | |
"type": "string", | |
"value": "[reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, parameters('identityResourceGroup')), 'Microsoft.ManagedIdentity/userAssignedIdentities', parameters('identityName')), '2018-11-30').principalId]" | |
}, | |
"instanceView": { | |
"type": "object", | |
"value": "[reference(resourceId('Microsoft.Compute/virtualMachines/extensions', variables('vmName'), 'cse')).instanceView]" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment