Deploying VMs in Virtualbox using Terraform Provider terrafarm/virtualbox

Today I simply wanted to deploy 1-2 VMs in virtualbox using the Terraform provider terrafarm/virtualbox. What should have been very easy turned out to take me around 15-20 minutes to get it finally working. Here is what I had to do on my Windows 10 machine to get it working. Of couse you need to have Virtualbox and Terraform installed for this.
TL;DR
If you just want a solution, you can download my working Terraform files on Github.
We create a folder called tf-vbox, you can call it whatever you like. In this folder we need two additional files:
- main.tf
- user_data
The user_data file is just empty for now. It is used for cloudinit. You can use it to run scripts add users, install packages etc.
First problem was that the version listed in the official Terraform documentation for the Virtuabox provider was no longer on the Github repo. So we received an error of:
"Error: Failed to query available provider packages".
PS C:\Users\user1\Documents\Code\Terraform\tf-vbox> terraform init
Initializing the backend...
Initializing provider plugins...
- Finding terra-farm/virtualbox versions matching "0.2.1"...
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider terra-farm/virtualbox: no available releases match the given constraints 0.2.1
Ok no big problem. We check the Github project page and lookup the available tags. Turns out that 0.2.2-alpha.1 is latest version. So we simply change version in out main.tf file
Next problem is that you need to have the vboxmanage binary in your PATH:
Error: [ERROR] Unable to set UUID: exec: "VBoxManage": executable file not found in %PATH%
On my machine this was not the case so I temporarily adjusted the PATH variable in PowerShell:
$env:PATH = $env:PATH + ";C:\Program Files\Oracle\VirtualBox"
The VMs where somewhat deployed and so I first had to delete them manully in Virtualbox.

Now things started to look better. But wait... another error:
╷
│ Error: [ERROR] Starting VM: exit status 1
│
│ with virtualbox_vm.node[0],
│ on main.tf line 12, in resource "virtualbox_vm" "node":
│
╵
╷
│
│ with virtualbox_vm.node[1],
│ on main.tf line 12, in resource "virtualbox_vm" "node":
│ 12: resource "virtualbox_vm" "node" {
turns out that the network adapter "vboxnet1" set in the main.tf file did not exist on my system. The default name is "VirtualBox Host-Only Ethernet Adapter". You can either edit the network settings of the VMs and change the adapter name Or you can simply change the name in the main.tf file. The complete file looks like this:
terraform {
required_providers {
virtualbox = {
source = "terra-farm/virtualbox"
version = "0.2.2-alpha.1"
}
}
}
# There are currently no configuration options for the provider itself.
resource "virtualbox_vm" "node" {
count = 2
name = format("node-%02d", count.index + 1)
image = "https://app.vagrantup.com/ubuntu/boxes/bionic64/versions/20180903.0.0/providers/virtualbox.box"
cpus = 2
memory = "512 mib"
user_data = file("${path.module}/user_data")
network_adapter {
type = "hostonly"
host_interface = "VirtualBox Host-Only Ethernet Adapter"
}
}
output "IPAddr" {
value = element(virtualbox_vm.node.*.network_adapter.0.ipv4_address, 1)
}
output "IPAddr_2" {
value = element(virtualbox_vm.node.*.network_adapter.0.ipv4_address, 2)
}
Now when applying the manifest it works right out of the box and we have two small Ubuntu VMs with host-internal networking deployed:
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
Outputs:
IPAddr = "192.168.56.103"
IPAddr_2 = "192.168.56.104"
So you see even some simple thing like deploying VMs in Virtualbox is not always that simple. But I'm glad this finally worked out and I can use this as a base for my projects.