Virtual desktop infrastructure for remote desktops and applications can be difficult to manage and confusing for those unfamiliar.
Terraform is a tool that allows you to provision cloud resources using code and has it automatically deployed in your workspace.
In this blog post, we will show a tutorial on how you can use Terraform to create and configure an Azure Virtual Desktop solution through the following steps:
- Write code using Terraform’s HashiCorp Language Syntax to specify our cloud provider and cloud resources
- Create an execution plan for previewing infrastructure changes
- Apply the execution plan and deploy the infrastructure
As prerequisites to this tutorial, please ensure that you have the following before starting:
- A Microsoft Azure account subscription
- An appropriate Microsoft Azure landing zone/foundation setup
We will be running Terraform v1.2.4 on a local instance of Windows Terminal.
First, you’ll need to configure Terraform to be associated with your Microsoft Azure account.
Install the Azure CLI using Microsoft’s Azure CLI installer, and then download Terraform from the HashiCorp website.
Extract the Terraform executable to a directory of your choice, then update your system’s global path to the executable.
Finally, open a terminal window and try running the “terraform -version” to verify that you have successfully installed Terraform.
Next, you’ll need to connect your Microsoft Azure account to Terraform. Because you’ve installed the Azure CLI, you can open a terminal window and type “az login” to be prompted with instructions to sign into Azure. Upon success, you will see your account information.
Now that you’ve logged in with Microsoft Azure, your Terraform executable is ready to communicate with your Microsoft Azure landing zone.
Writing Terraform Code using HCL
Next, we’ll need to create the modules that Terraform will read to create a plan for deploying our infrastructure resources.
Create a directory to host the Terraform code. In that folder, create four files: “main.tf”, “output.tf”, “providers.tf”, and “variables.tf”.
Using your text editor of choice (graphical or CLI), open “providers.tf”. The provider’s file allows Terraform to interact with cloud providers and other APIs. Place the following code in it:
In this case, “azurerm” and “azuread” refers to Azure Resource Manager and Azure Active Directory, respectively. “hashicorp/azurerm” and “hashicorp/azuread” use HashiCorp’s repositories for necessary files for both elements.
Next, open “main.tf”. Main.tf will contain the main set of configurations for your environment. Place the following code inside of it.
As we mentioned earlier, Terraform uses code to deploy infrastructure. Here, we can see resources by name being provisioned with configurations that we can make via code. For example, the resource “azurerm_virtual_desktop_application_group” or VDAG has
- a “resource group name,” referring to the container that will hold the new VDAG,
- a “host pool ID,” which is how the VDAG will associate with a Virtual Desktop Host Pool,
- a “location,” specifying in what location/region the VDAG will be located,
- a “type,” which specifies the type of VDAG as either RemoteApp or Desktop,
- a “friendly_name” for ease of recall for administrators,
- a “description,” for clarity,
- and “depends_on,” a meta-argument to handle resource and module dependencies that Terraform cannot automatically infer.
All resources in Terraform are constructed like this. More information on each of the specific resource arguments can be found on the HashiCorp Terraform documentation database.
Next, open “variables.tf”. Variables.tf contains the variable definitions for your module, and when the module is used by other modules, the variable will be configured as arguments in a “module” block.
Place the following code inside of “variables.tf”:
Variables in HCL have types, descriptions, and defaults. The type specifies what kind of variable is being described. In our case, we have a bunch of strings. The description is for high-level understanding. The default is a failsafe in the case that where you’re trying to use your variable does not provide as input a parameter to take the place of the variable.
Next, open “output.tf”. Output.tf contains the output definitions for our module, and they are often used to pass information about the parts of your infrastructure defined by this module to other parts of configurations.
Insert the following code into “output.tf”:
Our “output” resources have descriptions and values. The description is for a high-level understanding of what the resource intends to do, written by the developer. The value argument takes an expression whose result will be returned to the user. For example, “azure_virtual_desktop_compute_resource_group” will return a “sh.name” attribute exposed by an “azurerm_resource_group” defined elsewhere in the module.
We are now finally finished configuring our Terraform module. Next, we will use Terraform commands to stage our configuration for execution and deploying the plan we designed.
Writing Terraform Code using HCL
In your Windows Terminal, run “terraform init” while inside of the directory you created that hosts your four .tf files. These commands download the Azure modules required to manage the Azure resources we defined in the four .tf files from the previous step. It initializes a working directory containing Terraform configuration files.
Next, run the “terraform plan.” This will evaluate our Terraform configuration to determine the desired state of all the resources it declares. Then it compares that desired state to the actual infrastructure objects being managed within the current working directory and workspace. It creates an execution plan but doesn’t execute it.
Running this command should present you with a list of the resources specified in your main.tf file and how Terraform has interpreted them. You can inspect that and verify that they match your intended result.
Terraform plan outputs can be lengthy, but checking them diligently will ensure that you don’t commit to provisioning any resources you don’t intend to.
Finally, run “terraform apply.” Terraform apply applies the changes required to reach the desired state of the configuration, i.e., the set of actions generated by our run of “terraform plan.”
When you run “terraform apply,” you’ll see a review of the plan that Terraform intends to apply one last time and be prompted to confirm execution by typing “yes.”
When you type “yes”, you’ll see how Terraform automatically deploys your resources in real-time.
If your “terraform apply” completes, then you’re almost done.
Go to your Azure Portal, and select “Azure Virtual Desktop.” Select “Host Pools,” and the “Name of the pool created” resource. Finally, select “Session hosts” and then verify that the session host is listed. If you see a session host, then congratulations! You have successfully applied Terraform to create an Azure Virtual Desktop environment.
How ISmile Technologies Help:
- Simplifies Provisioning Process:
ISmile Technologies provides a simplified provisioning process for Azure Virtual Desktop using Terraform, which makes it easy to automate the deployment of the virtual desktop infrastructure. This reduces the workload for IT teams and speeds up the provisioning process.
The Terraform templates created by ISmile Technologies are highly customizable, allowing organizations to configure their virtual desktop infrastructure according to their specific requirements. This ensures that the virtual desktop environment is tailored to meet the needs of the business.
- Enhanced Security:
With Terraform, ISmile Technologies helps ensure that the Azure Virtual Desktop environment is set up with enhanced security. This includes configuring role-based access control and network security groups, among other measures.
- Cost Optimization:
ISmile Technologies’ Terraform templates also include options to optimize costs by selecting the appropriate VM sizes and storage types. This helps organizations to minimize unnecessary expenses while still meeting their virtual desktop infrastructure requirements.
- Ongoing Support:
ISmile Technologies provides ongoing support for their Terraform templates, ensuring that we remain up-to-date with the latest Azure Virtual Desktop features and security updates.
Using Terraform, you can create configuration files that use your Cloud provider of choice’s resources and automatically deploy infrastructure as code. This greatly speeds up management and allows for system configuration and management as your business grows. For more information, Get Your Free Consultation.
A technology enthusiast passionate about automation, Gabriel Chutuape is a Cloud Engineer at ISmile Technologies. He’s part of the ISmile Technologies Cloud enablement team that help customers to design/solution/project engineering, integrating and implementing infrastructure technologies & services.
A Cloud Engineer at ISmile Technologies, he had extensive experience working on Microsoft Azure which involves activities like Implementation, Managing and troubleshooting the User related issues. With automation tools like Azure ARM Template, Terraform, and Azure DevOps, he helps our client to automate deployment of IaaS and PaaS services.