DEV Community

Cover image for Using Bicep to setup IIS into a VM
Massimo Bonanni
Massimo Bonanni

Posted on

Using Bicep to setup IIS into a VM

In this post, I would like to show you how you can use a Bicep template to setup IIS and home page into an existing VM.
In a Bicep template, you can reference an existing resource using the existing keyword, as following

@description('Location for the deployment')
param location string = resourceGroup().location

@description('name of the VM to setup')
param vmName string

resource vm 'Microsoft.Compute/virtualMachines@2021-03-01' existing = {
  name: vmName
}
Enter fullscreen mode Exit fullscreen mode

Using the keyword existing, Bicep checks if the resource exists instead of creates the resource. Of course, if the resource doesn't exist, you receive an error. More info about existing keyword in the official documentation.

After the declaration, you can use the symbolic name vm in other resource declaration in the same template (in the same way you use a symbolic name for a resource you create).
So, you can execute a command script using runCommands provider as following:

resource vmFEIISEnabled 'Microsoft.Compute/virtualMachines/runCommands@2022-03-01' = {
  name: 'vm-EnableIIS-Script'
  location: location
  parent: vm
  properties: {
    asyncExecution: false
    source: {
      script: '''
        Install-WindowsFeature -name Web-Server -IncludeManagementTools
        Remove-Item C:\\inetpub\\wwwroot\\iisstart.htm
        Add-Content -Path "C:\\inetpub\\wwwroot\\iisstart.htm" -Value $("Hello from " + $env:computername)  
      '''
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The parent property tells Bicep that the script will be runned on the virtual machine called vm (the existing one), and the script that will be executed is in the script property:

Install-WindowsFeature -name Web-Server -IncludeManagementTools
Remove-Item C:\\inetpub\\wwwroot\\iisstart.htm
Add-Content -Path "C:\\inetpub\\wwwroot\\iisstart.htm" -Value $("Hello from " + $env:computername)
Enter fullscreen mode Exit fullscreen mode

The script makes these tasks:

  • installs IIS
  • removes the current home page for IIS (iisstart.html)
  • adds a new home page that shows the name of the vm.

You can notice that you can write a multiline script in the script property using the ''' chars as delimeter.

If your script is more complex than the one shown in the example, you can create a blob in a storage with the extension .ps1, and reference it using the scriptUri property instead of the script property.

resource vmFEIISEnabled 'Microsoft.Compute/virtualMachines/runCommands@2022-03-01' = {
  name: 'vm-EnableIIS-Script'
  location: location
  parent: vm
  properties: {
    asyncExecution: false
    source: {
      scriptUri: '<script blob location>'
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

In this way it is possible to perform any operation you want inside a vm.
You can also pass parameters to the script, save the output and errors, impersonate a user, and do other things with the runCommands. For more info, read the official documentation.

Top comments (0)