Create a VM cluster with Terraform using the Module Registry

This article walks you through creating a small VM cluster with the Terraform Azure compute module. In this tutorial you learn how to:

  • Set up authentication with Azure
  • Create the Terraform template
  • Visualize the changes with plan
  • Apply the configuration to create the VM cluster

For more information on Terraform, see the Terraform documentation.

Set up authentication with Azure


If you use Terraform environment variables or run this tutorial in the Azure Cloud Shell, skip this step.

Review Install Terraform and configure access to Azure to create an Azure service principal. Use this service principal to populate a new file in an empty directory with the following code:

variable subscription_id {}
variable tenant_id {}
variable client_id {}
variable client_secret {}

provider "azurerm" {
    subscription_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    tenant_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    client_secret = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Create the template

Create a new Terraform template named with the following code:

module mycompute {
    source = "Azure/compute/azurerm"
    resource_group_name = "myResourceGroup"
    location = "East US 2"
    admin_password = "ComplxP@assw0rd!"
    vm_os_simple = "WindowsServer"
    remote_port = "3389"
    nb_instances = 2
    public_ip_dns = ["unique_dns_name"]
    vnet_subnet_id = "${[0]}"

module "network" {
    source = "Azure/network/azurerm"
    location = "East US 2"
    resource_group_name = "myResourceGroup"

output "vm_public_name" {
    value = "${module.mycompute.public_ip_dns_name}"

output "vm_public_ip" {
    value = "${module.mycompute.public_ip_address}"

output "vm_private_ips" {
    value = "${module.mycompute.network_interface_private_ip}"

Run terraform init in your configuration directory. Using a Terraform version of at least 0.10.6 shows the following output:

Terraform Init

Visualize the changes with plan

Run terraform plan to preview the virtual machine infrastructure created by the template.

Terraform Plan

Create the virtual machines with apply

Run terraform apply to provision the VMs on Azure.

Terraform Apply

Next steps