Search
StarWind is a hyperconverged (HCI) vendor with focus on Enterprise ROBO, SMB & Edge

[Azure] Execute commands in a VM through Terraform

  • September 12, 2019
  • 4 min read
Cloud and Virtualization Architect. Florent is specializing in public, hybrid, and private cloud technologies. He is a Microsoft MVP in Cloud and Datacenter Management and an MCSE in Private Cloud.
Cloud and Virtualization Architect. Florent is specializing in public, hybrid, and private cloud technologies. He is a Microsoft MVP in Cloud and Datacenter Management and an MCSE in Private Cloud.

Terraform - Azure

After my first two articles about deploying resources in Azure and using a keyvault to store passwords, we’ll see how to execute commands directly when deploying the resource.

To make VM configuration, you can for example use tools like Ansible, DSC, etc. With Terraform, you can use Provisioners: https://www.terraform.io/docs/provisioners/index.html

Some examples are available here: https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples/virtual-machines/provisioners/windows

To get started, create a Files folder with 2 files inside, FirstLogonCommands.xml and winrm.ps1. These 2 files will run at the first launch of the machine, to configure the WinRM, to connect remotely via the Provider.

Then modify the file 1-virtual-machine.tf and modify the section os_profile_windows_config by adding the winrm part:

Then you must add the remote-exec provisioner to remotely execute a script or commands. Here in PowerShell I will install the Server Web role:

Run your Terraform, and once the VM is deployed, the startup scripts run, the provider is able to connect and run the PowerShell command:

PowerShell command

If you access the IP of the Terraform output, you should see your IIS web server:

Terraform output

The complete code is here:

https://github.com/Flodu31/Terraform/tree/master/Deploy_New_Environment_Provisioners

That’s the end of these few articles on Terraform, if you have any questions, do not hesitate 🙂

Hey! Found Florent’s article helpful? Looking to deploy a new, easy-to-manage, and cost-effective hyperconverged infrastructure?
Alex Bykovskyi
Alex Bykovskyi StarWind Virtual HCI Appliance Product Manager
Well, we can help you with this one! Building a new hyperconverged environment is a breeze with StarWind Virtual HCI Appliance (VHCA). It’s a complete hyperconverged infrastructure solution that combines hypervisor (vSphere, Hyper-V, Proxmox, or our custom version of KVM), software-defined storage (StarWind VSAN), and streamlined management tools. Interested in diving deeper into VHCA’s capabilities and features? Book your StarWind Virtual HCI Appliance demo today!