Terraform: Azure VM with IIS pre-installed.

I have been setting up Web server for long and as part of the requirement is to have everything being done as Infrastructure as a Code, additionally i have been using Azure Extension to setup IIS web server instead of first setting up the VM and then installing IIS web server separately using PowerShell.

There’s more then one way to do that but in this post i will be showing how to run the Powershell commands using Azure Extension. The Terraform template will have other components too, for example setting up VM and all it’s dependencies but for this post we will specifically focus on installing IIS web server using Azure Extension while we setup Virtual Machine through Terraform.

In this post we will start getting little more organized in terms of how to have Variables properly separated, how to use terraform.tfvars, in general how to structure the templates so that not only they look beautiful but also makes sense when you hand it over to someone.

Variables: We have divided our variables in to different segments so that they do not over lap or they are not all over the place. We will not setting up any values in Variables.tf but the actual values would be derived from terraform.tfvars.

Setting General Variables:

Virtual Network Variables:

Windows Virtual Machine Variables:

terraform.tfvars:We will set all the values for the variables defined above, this is much more cleaner way to setup variables and it’s values.

Setting General Variables Values:

Virtual Network Variables:

Windows Virtual Machine Variables:

Alright so we have setup everything that we need in terms of the variable and values, let’s quickly see some screenshot for the Virtual Machine and then we will focus on Azure Extension. We will not discuss about how to setup VM’s so these screenshots are more for background knowledge and for transparency.

Setting Virtual Network:

Setting Subnet inside VNet:

Creating Network Interface Card for VM:

Setting up Virtual Machine:

Creating Public IP Address:

Finally we have reached to state we will setup Azure Extension as show in below screenshot.

Some of the arguments are self explanatory and doesn’t need much of explanation, but there are few arguments that you need to understand.

a. Publisher: The VMImage Publisher, there are about 1291 publishers in Azure as of writing this blog, we need to focus on Microsoft.Compute publisher.

b. Type: With type we intend to say what type of extension we want to use, the other types are BGInfo, JsonADDDomainExtension, VMAccessAgent, in this blog we are selecting CustomScriptExtension.

c. Type_handler_version: Every type will have a version associated with it, so you need to choose the right version, but how would i know which version to select, which type, version is available in which region, so the easiest way is to use the PowerShell.

since there more then 1000 publisher we cannot write a generic PowerShell otherwise you will be lost in the heap, so we are going to do some filtering, below PowerShell script should help you find out which publisher, type and version to use.

Alright, when you decided which Publisher, Type, Version to use the only thing that you are left is to write the command or the script within the SETTINGS block.

The SETTINGS block would have a begining and ending with same name so that the block knows where to start and where it ends, Within the block we tell the block that we want to execute PowerShell followed by the cmdlet to install IIS web server, in this case we are installing all the sub-features with management tools, but you can customize the way you want to.

Final Result:

Web Server Running on Port 80:

Template is available here: I have removed password from the template, i just don’t want people to know how bad i am with passwords, but hey it’s about quickly showing people how to do things.

https://github.com/Hardeepbhamra/cloud-infrastructure/tree/master/05-Terraform-Virtual-Machine-Extension

Leave a Reply

Your email address will not be published. Required fields are marked *