Using Terraform with VMware

Published 31.10.2020

Author Fabian Peter

Categories Engineering

Tags

Terraform has been the talk of the town lately with its amazing integration and capabilities towards cloud infrastructure. A true Infrastructure as Code solution which still provides full-fledged functionalities despite being an underdog. Yes, it’s true. They are not even at version 1.0.0 yet but still deliver amazing solutions to major cloud providers as well as custom in house solutions.

To increase the value of our Infrastructure as Code solution – Dash1 – we decided to tackle a cool edge-case: we want Windows Servers on VMWare (or any other Cloud-Provider – we just like challenges and started with the most popular solution for on-premise Windows virtualization) to run hybrid Kubernetes-Clusters!

The Job

We’re trying to create a Windows Server 2019 Virtual Machine on a VMWare vSphere cluster from a template and then configure SSH and WinRM so our Platform – apollo – can be provisioned on the server.

We will talk more in-depth about issues and their solutions we came across during the article.

Terraform Structure

Terraform uses HCL for its configuration. HCL is a great language, easy to use and standardized across platforms. This makes it easier and fun to learn.

In our case, Terraform-Code will be structured and categorized into four parts.:

Main.tf

Consists of providers, data and resource objects. It’s the heart of the language and where you put all the objects necessary to run your code. It basically defines the deliverable.

Variables.tf

Contains needed variables and their description. Used to define variable types like String, Maps, Lists, Integer and the defaults a variable should have. You can find a list of terraform variables and their behavior in this Docs article.

Terraform.tfvars

You are now familiar with Terraform variables. Although you can define their values in varibales.tf, it is recommended to use a file with a .tfvars extension to store the actual values of your variables. This is really important in terms of design and usability.

Output.tf

In output.tf we define the output values after the code in main.tf has finished. This file contains attributes that you want to show in the console once the instance is created. You can output attributes like IP address, server name and much more.

You can find all of the code in the vsphere module of dash1.

Coding Lessons

Since we now have a basic understanding of the Terraform file types we’re using, let’s look at a few code snippets to gain more understanding about the language. For the following code snippet, we have used this variable file. Please investigate it to understand what below variables represent.

We used the vsphere provider and got the connection to the API of a testing vCenter working pretty easily. For your own testing, you’ll have to adjust your variables to match the resources you have access to. Here we defined DC, Datastore cluster, Datastore, resource pools and much more sources to be used in the code.

Check the files to use here: dash1 vSphere module.

A little Background

count

The count attribute describes the number of servers we want the code to provision. We have given it an IF condition to dynamically assign a number based on var.instances. Basically, all you must know is, if the count value is zero, then terraform will not provision any resource type named windows.

Check the code!

name

The name attribute should be changed according to the number of instances since the name is a unique value in vcenter. Therefore, we check if the variable vmnameliteral is not set, then it means there’s more than one instance and vmname should be used along with a suffix defined by the user (VMNAME+SUFFIX). Take a look at this section of the Terraform article to understand about if else conditions.

Check the code!

clone

We use the clone configuration to tell vcenter to use “clone the VM from this template” option (HINT: this means you need an existing VM template in your vCenter! If you need advice on how to manage this, reach out!). It contains attributes like join_domain and admin_password to let you customize the windows image as you would normally do in vcenter.

Check the code!

As you can see from the above code, there’s a lot of customizations available in the clone configuration and how I have used conditions like IF ELSE and FOREACH to achieve dynamic assignments of variables. You can check this article from Terraform to learn more about such conditions and how you can apply them in your code. It will be a challenge, but once you master them, Terraform would be the best thing that ever happened to you.

Helpful TIPS

The next part of this article describes an issue we faced after provisioning the VM. It’s regarding configuring SSH and WinRM in the newly created Windows Server 2019 machine. Initially we thought we could use the OpenSSH module along with a Powershell-Script for WinRM to configure both services.

We were trying to copy the required files and binaries over through the file object and used a remote execution command to run them inside the OS. However, due to this bug in Terraform, there’s a limitation of the size you can copy over the network. So, our OpenSSH folder, which contained about 2Mb of files, was not copying over fully.

After some research we learned that there is a new in-built feature in Windows Server 2019 that lets you configure the server as a SSH server.

we created two Powershell scripts that will configure WinRM and SSH and copied them over, using the file provisioner. Since the two file sizes were limited to 10KB, they were not affected by the bug. However, you need to make sure the machine you are running Terraform code from, and the newly built Windows Server 2019 VM, can communicate with each other over network.

Check the code!

After lots of time and effort, we were finally able to successfully create a Windows Server 2019 VM through Terraform on VMWare vSphere. We’re now one step closer to hybrid Kubernetes-Clusters.

The code was well designed, reusable and can be used to create ‘n’ number of windows servers with SSH and WinRM configured.

We urge you to try out Terraform and enter the vast world of Infrastructure as Code to improve your IT infrastructure. The time where system users, developers and stakeholders had to wait for the infrastructure team to create servers for them is over. Now everyone can make their own servers with the configuration they want. Get on with the trend and better yourselves to face the Future of IT.

Join 100+ cloud native enthusiasts

and stay in the loop on modern software development.

Sign up to receive exclusive content around cloud native software development right into your inbox.

We don’t spam! Read our privacy policy for more info.

More stories from our blog

How To Install Docker on Ubuntu 20.04?

How To Install Docker on Ubuntu 20.04?

Docker is an open-source tool that makes managing application processes in containers much easier. Containers allow you to run your programmes in separate processes with their own resources. Containers are comparable to virtual machines, except they're more portable,...

Answer to Everything isn’t 42, it’s Family

Answer to Everything isn’t 42, it’s Family

We’re experiencing digitisation. An era where every person has a voice, and it doesn’t matter if he’s wise. There’s more motivation circulating the vast stretches of the internet than it’s required. This would be good in a theoretical world, but if you seek the truth,...

What’s new in Gitlab 14? 🦊

What’s new in Gitlab 14? 🦊

GitLab 14 is out and fans must be thrilled to know about all the new features along with all the fixes and removals. In this post, we will go through the many changes and improvements, bug fixes, and some remarkable deprecations. We will see all of that here. So,...

k8s vs k3s: The Comprehensive Difference

k8s vs k3s: The Comprehensive Difference

Kubernetes is undoubtedly a champion in the container orchestration world. But currently, we see that K3s or a lightweight Kubernetes distribution which is light, efficient and fast with a drastically small footprint levelling up. Businesses nowadays scratch their...

What’s new in Fluentbit v1.8.1?

What’s new in Fluentbit v1.8.1?

Fluentbit is a lightweight and fast data processor and forwarder for Linux, BSD and OSX. And, for Fluentbit fans, there is good news as they have released their new update with lots of new features and fixes. We will have a look at all of them below. New Metrics...

What’s new in Envoy v1.19.0?

What’s new in Envoy v1.19.0?

Envoyproxy introduced its new version, 1.19.0, recently, and it came with many changes and improvements from the previous ones. We can see more stability in this version, along with specific bug fixes. So, without waiting any further, let’s see what the new version...

What’s new in Vitess 10?

What’s new in Vitess 10?

Vitess 10 is released with many excellent features and also many bug fixes that were bothering the user base. We are going to see all the features and exciting announcements. So, Let's roll! Major Themes in Vitess In this release, we can see that Vitess Maintainers...

What’s new in Contour 1.17.0?

What’s new in Contour 1.17.0?

Contour 1.17.0 is out with a layer seven HTTP reverse proxy for Kubernetes clusters. The new version has arrived with many new features and several fixes, which will make the functioning of the ingress controller smoother. More activities within the community came...

What’s new in Prometheus 2.28?

What’s new in Prometheus 2.28?

Prometheus 2.28 is out. If you don't know, Prometheus is an excellent open-source system monitoring and alerting toolkit. Let's have a look at those features and have a look at the changelog. Displaying Trace Examplers in the Graphic Interface From the previous...

GPT-J: GPT-3 Democratized

GPT-J: GPT-3 Democratized

GPT-J is the open source cousin of GPT-3 that everyone can use. The open source transformer is all about democratizing transformers and with 6b parameters it’s the largest transformer available. Read more here

Interested in what we do? Looking for help? Wanna talk about software strategy?