Create a VM Cluster in Azure with Terraform

45 minutes
  • 4 Learning Objectives

About this Hands-on Lab

In this lab, you are asked to start building out an app server cluster for a comic reader that you are working on for your employer for digital comics. To do this, you will configure Cloud Shell and run the lab setup script; import the resource group; deploy the Azure Storage account configuration; and add the VM cluster configuration and deploy your cluster.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Set Up Cloud Shell and the Lab Environment

In the Portal

  1. Click the Cloud Shell icon next to the search bar in the portal.
  2. Select Bash at the prompt.
  3. Click Show advanced settings.
  4. Set the Cloud Shell region to the same location as the resource group.
  5. Select the existing resource group, and select Use existing for the Storage account.
  6. In the File share section, choose Create new and enter terraform.
  7. Click Create storage.
  8. Download the lab_5_setup.sh script: https://raw.githubusercontent.com/ACloudGuru/advanced-terraform-with-azure/main/lab_vm_cluster_resources/lab_5_setup.sh
  9. Add execute permissions to the script.
  10. Run the lab_5_setup.sh script.
Import the Resource Group

In the Cloud Shell

  1. In the Cloud Shell, review the networking.tf file.

  2. Make note of the resource group and label, and then close the file.

  3. Run the az group list command to get the subscription ID.

  4. Import your resource group into Terraform using the resource name, label, and subscription ID.

  5. After the import, add the name and location of your resource group to the networking.tf file so it looks like the code below (fill in with your resource group and location):

    resource "azurerm_resource_group" "comics" {
        name     = "<RESOURCE_GROUP>"
        location = "<LOCATION>"
    }
  6. Save the file.

Define Your Cluster Config and Output Variables

In the Cloud Shell

  1. Create a file called vms.tf.
  2. Define the VM configuration so it will deploy a two-VM cluster using count.
  3. Make sure you associate your VMs with your subnet and load balancer backend pool in the networking.tf file, and adjust for the multiple resources in your configuration.
  4. Save the changes.
  5. Create a file called output.tf.
  6. Define the these outputs:
    • Resource group
    • Private IP of the first VM
    • Private IP of the second VM
    • Public IP
    • Resource group location
  7. Save the changes.
Add the VM Cluster Terraform Config and Deploy

In the Cloud Shell

  1. Apply your configuration.
  2. Confirm you have successfully deployed your VM cluster.

Additional Resources

You are asked to start building out an app server cluster for a comic reader that you are working on for your employer for digital comics. You will need to add a storage account for VM diagnostics and the cluster configuration. You will use the Terraform count argument to deploy multiple resources for the app server cluster. You will also use a resource group that's already been deployed. From there, you will import your resource group and build out your Terraform configuration, and then deploy your VM cluster resources with Terraform to Azure.

To get started, log in to the Azure portal using the credentials provided. Be sure to use an incognito or private browser window to ensure you’re using the lab account rather than your own.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?