Now that the title is out of the way, I'll get on with explaining how I got this working. I tried several ways of getting the WinRM service configured to use HTTPS in Azure Resource Manager using Terraform.
I explained in a previous post how to get the basics up and running.
There appear to be a few ways to do this in Terraform but I've only found one that works. I attempted to use the built in WinRM configuration option, but this requires creating a certificate locally and then uploading it to an Azure Key Vault. This sounds like a good option, but it can't yet be done purely in Terraform (I think!)
I've also tried creating a self signed certificate on the fresh build VM using the Windows FirstLogonCommands. This proved difficult due to all sorts of timing and character interpolation issues.
The working option is to create a PowerShell script, add in some variables from Terraform and then inject that script into the VM at creation time using the custom_data field as part of the os_profile section.
First, I created the Deploy.ps1 with no parameters which will create the local self-signed certificate and setup WinRM and a firewall rule.
Second, I created the FirstLogonCommands.xml which gets inserted into the Windows unattend.xml and runs the commands at first logon.
Third, in the Virtual Machine configuration of the .tf file, the vm is configured to inject the Deploy.ps1 data into the VM with parameters from Terraform. The VM is configured to automatically log on once which runs the FirstLogonCommands. This should then rename the custom_data blob back to Deploy.ps1, run it and configure WinRM!
The full example can be downloaded from my GitHub.
Part 3, Configuring an Azure RM load balancer is here.
I was really looking for something like for a whole day and it works! Just some terraform syntax that has changed since your wrote this like "azurerm_virtual_machine" is now "azurerm_windows_virtual_machine" and the unattended config now looks like this (one of the two blocks)
ReplyDeleteadditional_unattend_content {
setting = "FirstLogonCommands"
content = file("FirstLogonCommands.xml")
}
Thank you!