When you think of Infrastructure-As-Code, you think Can I create my infrastructure faster with IaS? and the answer is always yes. However, creating infrastructure is only one piece of the puzzle. The next piece is scaling that infrastructure to make it not only highly available, but to give it the ability to manage any application load that you throw at it.
In this blog post you will see just that. Let's take a hands-on approach and learn how to to scale virtual machines in Azure with Terraform!
The goal of the blog post is to give the knowledge and ability to scale out virtual machines in Azure. When looking at the
azurerm_virtual_machine Terraform resource, you won't find the
count parameter (shown in the next section Scaling a Virtual Machine). The reason why is because
count is built into Terraform by default for scaling resources. The purpose of this blog post is to show you how to use the
To follow along with this blog post you will need:
- A beginner to more of an intermediate level knowledge of Terraform.
- Terraform installed which you can find here.
- An Azure subscription. If you don't have one, you can sign up for a 30-day free trial here.
- A text editor. This blog post will be using Visual Studio Code (VS Code) 1.44.2. You can install VS Code from here. Please note that the version may be different and that's okay.
Scaling a Virtual Machine
Scaling virtual machines with Infrastructure-as-code is great from both a time-saving perspective and an automation perspective. We've all built and deployed virtual machines manually, which is cumbersome. If you have to create, for example 10 virtual machines, it's also cumbersome to run the same code over and over again.
In this section you will learn that running the code, say 10 times, isn't necessary. Instead, you can create and scale virtual machines by running the Terraform code once.
The Terraform Code
Open up a web browser and go to this link to clone the Terraform code.
You will need the code to test the multi-vm creation and to understand the
count parameter. Open up a terminal and change directory
cd to the desktop. Although the location isn't mandatory, that's what this blog post will follow. Clone the GitHub repo to the desktop by running the following command.
git clone https://github.com/AdminTurnedDevOps/CloudDev.Engineering_Repo.git
Next you will need to open up VS Code so you can view the Terraform configurations. Open up VS Code then click file --> open and open the
main.tf configuration is what holds the
count parameter to scale virtual machines. Open up the
main.tf configuration file and take a look at lines 9 and 10. Lines 9 and 10 are all you need to add outside of a standard
azurerm_virtual_machine resource configuration. A standard configuration can be something that you can find on the Terraform docs, for example, here.
countparameter allows you to tell Terraform how many virtual machines you want created.
- Within the name parameter, you have an interpolation to add the
count.index. Doing so allows you to not have to specify multiple virtual machine names. Instead, you just concatenate an index, which Terraform handles for you.
You will notice that on lines 30-34 there is an
os_profileblock with a plain-text password. Using a plain text password in the Terraform configuration is, of course, not ideal. However, because every environment is different, for example, some use passwords and others use SSH keys, I wanted to set up the
main.tfas straight-forward as possible for the demonstration. Don't use this exact configuration in production before changing the authentication method.
The Terraform variables that are used are shown below. You will be using a
terraform.tfvars configuration file to pass in the values at runtime.
Ensure to fill in the proper values for the variables per the environment that you are creating the VMs in.
Running the Code
It's time to run the Terraform module to create the two virtual machines.
Open up the VS Code terminal,
Scale-VMs and run
terraform init to initial the staging directory and to download the
terraform plan to confirm no syntax errors or resource issues. Notice the
plan where it states that Terraform is creating two resources.
To create the virtual machines, run
As the resources are being created, you'll notice the index that two are creating.
After the resources are created, go to the Virtual Machine blade in the Azure portal and you will see two virtual machines created.
To clean up/delete the newly created Terraform resources, from the terminal in VS Code, run the following command.
In this blog post, you learned how easy Terraform makes it to create multiple resources. Creating multiple resources at the same time not only automates the process, but protects the environment. With only one virtual machine, for example, there is zero scalability and redundancy to ensure the application on the virtual machine is running as expected. This blog post showed how to create multiple virtual machines, but the same method works for any resource being created with Terraform.