Tutorial: Deploy virtual automobile extensions with ARM templates

Learn how to use Azure virtual machine extensions to perform post-deployment configuration and automation tasks on Azure VMs. Many different VM extensions are available for utilise with Azure VMs. In this tutorial, you deploy a Custom Script extension from an Azure Resource Manager template (ARM template) to run a PowerShell script on a Windows VM. The script installs Web Server on the VM.

This tutorial covers the following tasks:

  • Prepare a PowerShell script
  • Open up a quickstart template
  • Edit the template
  • Deploy the template

If you don't have an Azure subscription, create a gratuitous account before you lot begin.

Prerequisites

To complete this article, you need:

  • Visual Studio Code with Resource Manager Tools extension. See Quickstart: Create ARM templates with Visual Studio Lawmaking.

  • To increase security, utilize a generated countersign for the virtual machine administrator account. You can apply Azure Cloud Shell to run the following control in PowerShell or Bash:

                      openssl rand -base64 32                                  

    To learn more than, run human being openssl rand to open the manual folio.

    Azure Primal Vault is designed to safeguard cryptographic keys and other secrets. For more information, encounter Tutorial: Integrate Azure Fundamental Vault in your ARM template deployment. We also recommend that you update your countersign every three months.

Prepare a PowerShell script

Y'all tin use an inline PowerShell script or a script file. This tutorial shows how to utilise a script file. A PowerShell script with the post-obit content is shared from GitHub:

              Install-WindowsFeature -Name Web-Server -IncludeManagementTools                          

If you lot choose to publish the file to your ain location, update the fileUri element in the template later in the tutorial.

Open a quickstart template

Azure Quickstart Templates is a repository for ARM templates. Instead of creating a template from scratch, you lot can notice a sample template and customize it. The template used in this tutorial is chosen Deploy a simple Windows VM.

  1. In Visual Studio Code, select File > Open up File.

  2. In the File proper name box, paste the following URL:

                      https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.compute/vm-simple-windows/azuredeploy.json                                  
  3. To open the file, select Open up. The template defines 5 resources:

    • Microsoft.Storage/storageAccounts.

    • Microsoft.Network/publicIPAddresses.

    • Microsoft.Network/networkSecurityGroups.

    • Microsoft.Network/virtualNetworks.

    • Microsoft.Network/networkInterfaces.

    • Microsoft.Compute/virtualMachines.

      Information technology's helpful to get some basic understanding of the template before you customize information technology.

  4. Save a copy of the file to your local reckoner with the name azuredeploy.json by selecting File > Save As.

Edit the template

Add a virtual auto extension resource to the existing template with the following content:

              {   "type": "Microsoft.Compute/virtualMachines/extensions",   "apiVersion": "2021-04-01",   "name": "[concat(variables('vmName'),'/', 'InstallWebServer')]",   "location": "[parameters('location')]",   "dependsOn": [     "[concat('Microsoft.Compute/virtualMachines/',variables('vmName'))]"   ],   "properties": {     "publisher": "Microsoft.Compute",     "type": "CustomScriptExtension",     "typeHandlerVersion": "one.seven",     "autoUpgradeMinorVersion": truthful,     "settings": {       "fileUris": [         "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/tutorial-vm-extension/installWebServer.ps1"       ],       "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File installWebServer.ps1"     }   } }                          

For more than data about this resources definition, encounter the extension reference. The following are some important elements:

  • name: Because the extension resources is a child resource of the virtual machine object, the proper name must have the virtual auto name prefix. See Set proper noun and type for kid resources.
  • dependsOn: Create the extension resource later on you've created the virtual automobile.
  • fileUris: The locations where the script files are stored. If y'all choose non to use the provided location, you need to update the values.
  • commandToExecute: This command invokes the script.

To utilize an inline script, remove fileUris, and update commandToExecute to:

              powershell.exe Install-WindowsFeature -name Web-Server -IncludeManagementTools && powershell.exe remove-detail 'C:\\inetpub\\wwwroot\\iisstart.htm' && powershell.exe Add-Content -Path 'C:\\inetpub\\wwwroot\\iisstart.htm' -Value $('Hello World from ' + $env:computername)                          

This inline script as well updates the iisstart.html content.

Yous must besides open the HTTP port so that you can access the spider web server.

  1. Detect securityRules in the template.

  2. Add the following rule next to default-allow-3389.

                      {   "name": "AllowHTTPInBound",   "backdrop": {     "priority": 1010,     "access": "Allow",     "direction": "Entering",     "destinationPortRange": "80",     "protocol": "Tcp",     "sourcePortRange": "*",     "sourceAddressPrefix": "*",     "destinationAddressPrefix": "*"   } }                                  

Deploy the template

For the deployment procedure, come across the Deploy the template section of Tutorial: Create ARM templates with dependent resources. We recommended that you apply a generated password for the virtual machine administrator account. Meet this article's Prerequisites section.

From the Deject Beat out, run the following command to remember the public IP accost of the VM:

              (Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName).IpAddress                          

Paste the IP address into a Web browser. The default Internet Data Services (IIS) welcome page opens:

The Internet Information Services welcome page

Clean upwards resources

When y'all no longer need the Azure resource you deployed, clean them up by deleting the resource group.

  1. In the Azure portal, in the left pane, select Resource group.
  2. In the Filter by name box, enter the resources group name.
  3. Select the resources group name. Half-dozen resources are displayed in the resource group.
  4. In the top menu, select Delete resources group.

Adjacent steps

In this tutorial, you deployed a virtual machine and a virtual automobile extension. The extension installed the IIS web server on the virtual machine. To acquire how to use the Azure SQL Database extension to import a BACPAC file, see: